Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python] remove imports from models #7905

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import pprint
import re # noqa: F401

import six
{{#imports}}{{#-first}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the scenario where the discriminator is used?
The lack of import will result in invalid code due to the missing import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this discriminator work if modules can't be imported due to circular dependencies ?

Maybe there is another way to provide paths/data for the discriminator - I don't know, I don't use it. Such imports make more problems for standard usage of generated code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenjones-cisco I dig into it. Finally I've written a test and I also had to fix discriminator to make it working ;)

Now it works without these imports. Child models are created dynamically on the parsing step and models with discriminator don't refer to them - only as a string with name of class.

I still recommend to merge this PR.

Thanks.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the status of this PR? I have a similar fix and was about to submit it before stumbling into this PR.
We are seeing models created with circular imports too which makes the generated code useless unless they are manually commented out!

{{/-first}}
{{import}} # noqa: F401,E501
{{/imports}}


{{#models}}
Expand Down Expand Up @@ -169,7 +165,7 @@ class {{classname}}(object):
{{#discriminator}}
def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

{{/discriminator}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil"]
REQUIRES.append("aiohttp")
{{/asyncio}}
{{#tornado}}
REQUIRES.append("tornado")
REQUIRES.append("tornado>=4.2,<5")
{{/tornado}}

setup(
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/python-asyncio/docs/EnumTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**enum_string** | **str** | | [optional]
**enum_string_required** | **str** | |
**enum_integer** | **int** | | [optional]
**enum_number** | **float** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Cat(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Dog(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501


class EnumTest(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand All @@ -34,29 +32,33 @@ class EnumTest(object):
"""
swagger_types = {
'enum_string': 'str',
'enum_string_required': 'str',
'enum_integer': 'int',
'enum_number': 'float',
'outer_enum': 'OuterEnum'
}

attribute_map = {
'enum_string': 'enum_string',
'enum_string_required': 'enum_string_required',
'enum_integer': 'enum_integer',
'enum_number': 'enum_number',
'outer_enum': 'outerEnum'
}

def __init__(self, enum_string=None, enum_integer=None, enum_number=None, outer_enum=None): # noqa: E501
def __init__(self, enum_string=None, enum_string_required=None, enum_integer=None, enum_number=None, outer_enum=None): # noqa: E501
"""EnumTest - a model defined in Swagger""" # noqa: E501

self._enum_string = None
self._enum_string_required = None
self._enum_integer = None
self._enum_number = None
self._outer_enum = None
self.discriminator = None

if enum_string is not None:
self.enum_string = enum_string
self.enum_string_required = enum_string_required
if enum_integer is not None:
self.enum_integer = enum_integer
if enum_number is not None:
Expand Down Expand Up @@ -91,6 +93,35 @@ def enum_string(self, enum_string):

self._enum_string = enum_string

@property
def enum_string_required(self):
"""Gets the enum_string_required of this EnumTest. # noqa: E501


:return: The enum_string_required of this EnumTest. # noqa: E501
:rtype: str
"""
return self._enum_string_required

@enum_string_required.setter
def enum_string_required(self, enum_string_required):
"""Sets the enum_string_required of this EnumTest.


:param enum_string_required: The enum_string_required of this EnumTest. # noqa: E501
:type: str
"""
if enum_string_required is None:
raise ValueError("Invalid value for `enum_string_required`, must not be `None`") # noqa: E501
allowed_values = ["UPPER", "lower", ""] # noqa: E501
if enum_string_required not in allowed_values:
raise ValueError(
"Invalid value for `enum_string_required` ({0}), must be one of {1}" # noqa: E501
.format(enum_string_required, allowed_values)
)

self._enum_string_required = enum_string_required

@property
def enum_integer(self):
"""Gets the enum_integer of this EnumTest. # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class MixedPropertiesAndAdditionalPropertiesClass(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

import six

from petstore_api.models.outer_boolean import OuterBoolean # noqa: F401,E501
from petstore_api.models.outer_number import OuterNumber # noqa: F401,E501
from petstore_api.models.outer_string import OuterString # noqa: F401,E501


class OuterComposite(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import six

from petstore_api.models.category import Category # noqa: F401,E501
from petstore_api.models.tag import Tag # noqa: F401,E501


class Pet(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/python-tornado/docs/EnumTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**enum_string** | **str** | | [optional]
**enum_string_required** | **str** | |
**enum_integer** | **int** | | [optional]
**enum_number** | **float** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Cat(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class Dog(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.outer_enum import OuterEnum # noqa: F401,E501


class EnumTest(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand All @@ -34,29 +32,33 @@ class EnumTest(object):
"""
swagger_types = {
'enum_string': 'str',
'enum_string_required': 'str',
'enum_integer': 'int',
'enum_number': 'float',
'outer_enum': 'OuterEnum'
}

attribute_map = {
'enum_string': 'enum_string',
'enum_string_required': 'enum_string_required',
'enum_integer': 'enum_integer',
'enum_number': 'enum_number',
'outer_enum': 'outerEnum'
}

def __init__(self, enum_string=None, enum_integer=None, enum_number=None, outer_enum=None): # noqa: E501
def __init__(self, enum_string=None, enum_string_required=None, enum_integer=None, enum_number=None, outer_enum=None): # noqa: E501
"""EnumTest - a model defined in Swagger""" # noqa: E501

self._enum_string = None
self._enum_string_required = None
self._enum_integer = None
self._enum_number = None
self._outer_enum = None
self.discriminator = None

if enum_string is not None:
self.enum_string = enum_string
self.enum_string_required = enum_string_required
if enum_integer is not None:
self.enum_integer = enum_integer
if enum_number is not None:
Expand Down Expand Up @@ -91,6 +93,35 @@ def enum_string(self, enum_string):

self._enum_string = enum_string

@property
def enum_string_required(self):
"""Gets the enum_string_required of this EnumTest. # noqa: E501


:return: The enum_string_required of this EnumTest. # noqa: E501
:rtype: str
"""
return self._enum_string_required

@enum_string_required.setter
def enum_string_required(self, enum_string_required):
"""Sets the enum_string_required of this EnumTest.


:param enum_string_required: The enum_string_required of this EnumTest. # noqa: E501
:type: str
"""
if enum_string_required is None:
raise ValueError("Invalid value for `enum_string_required`, must not be `None`") # noqa: E501
allowed_values = ["UPPER", "lower", ""] # noqa: E501
if enum_string_required not in allowed_values:
raise ValueError(
"Invalid value for `enum_string_required` ({0}), must be one of {1}" # noqa: E501
.format(enum_string_required, allowed_values)
)

self._enum_string_required = enum_string_required

@property
def enum_integer(self):
"""Gets the enum_integer of this EnumTest. # noqa: E501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class MixedPropertiesAndAdditionalPropertiesClass(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

import six

from petstore_api.models.outer_boolean import OuterBoolean # noqa: F401,E501
from petstore_api.models.outer_number import OuterNumber # noqa: F401,E501
from petstore_api.models.outer_string import OuterString # noqa: F401,E501


class OuterComposite(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

import six

from petstore_api.models.category import Category # noqa: F401,E501
from petstore_api.models.tag import Tag # noqa: F401,E501


class Pet(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/python-tornado/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# http://pypi.python.org/pypi/setuptools

REQUIRES = ["urllib3 >= 1.15", "six >= 1.10", "certifi", "python-dateutil"]
REQUIRES.append("tornado")
REQUIRES.append("tornado>=4.2,<5")

setup(
name=NAME,
Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/python/docs/EnumTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**enum_string** | **str** | | [optional]
**enum_string_required** | **str** | |
**enum_integer** | **int** | | [optional]
**enum_number** | **float** | | [optional]
**outer_enum** | [**OuterEnum**](OuterEnum.md) | | [optional]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def color(self, color):

def get_real_child_model(self, data):
"""Returns the real base class specified by the discriminator"""
discriminator_value = data[self.discriminator].lower()
discriminator_value = data[self.discriminator]
return self.discriminator_value_class_map.get(discriminator_value)

def to_dict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.animal import Animal # noqa: F401,E501


class AnimalFarm(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

import six

from petstore_api.models.read_only_first import ReadOnlyFirst # noqa: F401,E501


class ArrayTest(object):
"""NOTE: This class is auto generated by the swagger code generator program.
Expand Down
Loading