From 46974ccdb937eb6b80d6935cb3fcf792733a1e8a Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Mon, 11 Jan 2021 16:37:38 -0500 Subject: [PATCH 1/7] Change endpoint macros --- openapi_python_client/templates/endpoint_macros.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openapi_python_client/templates/endpoint_macros.pyi b/openapi_python_client/templates/endpoint_macros.pyi index 5819714d8..d6baaf5d6 100644 --- a/openapi_python_client/templates/endpoint_macros.pyi +++ b/openapi_python_client/templates/endpoint_macros.pyi @@ -33,7 +33,7 @@ params: Dict[str, Any] = { } {% for property in endpoint.query_parameters %} {% if not property.required %} -if {{ property.python_name }} is not UNSET: +if {{ property.python_name }} is not UNSET and {{ property.python_name }} is not None: {% if property.template %} params["{{ property.name }}"] = {{ "json_" + property.python_name }} {% else %} From c4ec480d993bee50c0efee3ed0c169ce4b997bb3 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 14:05:03 -0500 Subject: [PATCH 2/7] Change type string --- .../parser/properties/property.py | 27 ++++++++++++++----- .../templates/endpoint_macros.pyi | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/openapi_python_client/parser/properties/property.py b/openapi_python_client/parser/properties/property.py index 0b7047551..c01677d59 100644 --- a/openapi_python_client/parser/properties/property.py +++ b/openapi_python_client/parser/properties/property.py @@ -32,12 +32,13 @@ class Property: def __attrs_post_init__(self) -> None: object.__setattr__(self, "python_name", utils.to_valid_python_identifier(utils.snake_case(self.name))) - def get_type_string(self, no_optional: bool = False) -> str: + def get_type_string(self, no_optional: bool = False, query_parameter: bool = False) -> str: """ Get a string representation of type that should be used when declaring this property Args: no_optional: Do not include Optional or Unset even if the value is optional (needed for isinstance checks) + query_arg: True if the property's type is being used for a query parameter """ type_string = self._type_string if no_optional: @@ -45,7 +46,11 @@ def get_type_string(self, no_optional: bool = False) -> str: if self.nullable: type_string = f"Optional[{type_string}]" if not self.required: - type_string = f"Union[Unset, {type_string}]" + if query_parameter: + # For query parameters, None has the same meaning as Unset + type_string = f"Union[Unset, None, {type_string}]" + else: + type_string = f"Union[Unset, {type_string}]" return type_string def get_instance_type_string(self) -> str: @@ -69,17 +74,25 @@ def get_imports(self, *, prefix: str) -> Set[str]: imports.add(f"from {prefix}types import UNSET, Unset") return imports - def to_string(self) -> str: - """ How this should be declared in a dataclass """ + def to_string(self, query_parameter: bool = False) -> str: + """ + How this should be declared in a dataclass + + Args: + query_arg: True if the property's type is being used for a query parameter + """ default: Optional[str] if self.default is not None: default = self.default elif not self.required: - default = "UNSET" + if query_parameter: + default = "None" + else: + default = "UNSET" else: default = None if default is not None: - return f"{self.python_name}: {self.get_type_string()} = {default}" + return f"{self.python_name}: {self.get_type_string(query_parameter=query_parameter)} = {default}" else: - return f"{self.python_name}: {self.get_type_string()}" + return f"{self.python_name}: {self.get_type_string(query_parameter=query_parameter)}" diff --git a/openapi_python_client/templates/endpoint_macros.pyi b/openapi_python_client/templates/endpoint_macros.pyi index d6baaf5d6..dac75ee41 100644 --- a/openapi_python_client/templates/endpoint_macros.pyi +++ b/openapi_python_client/templates/endpoint_macros.pyi @@ -96,7 +96,7 @@ json_body: {{ endpoint.json_body.get_type_string() }}, {% endif %} {# query parameters #} {% for parameter in endpoint.query_parameters %} -{{ parameter.to_string() }}, +{{ parameter.to_query_method_arg() }}, {% endfor %} {% for parameter in endpoint.header_parameters %} {{ parameter.to_string() }}, From 435a189ed7280f8c68a5d5b8f25c919af2c2f756 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 14:23:05 -0500 Subject: [PATCH 3/7] Regenerate tests --- .../api/tests/defaults_tests_defaults_post.py | 59 ++++--- .../api/tests/get_basic_list_of_booleans.py | 11 +- .../api/tests/get_basic_list_of_floats.py | 11 +- .../api/tests/get_basic_list_of_integers.py | 11 +- .../api/tests/get_basic_list_of_strings.py | 11 +- ...tional_value_tests_optional_query_param.py | 4 +- .../models/model_with_any_json_properties.py | 43 ++--- ...el_with_primitive_additional_properties.py | 4 +- .../models/model_with_union_property.py | 23 ++- .../custom_e2e/models/validation_error.py | 27 +-- .../api/tests/defaults_tests_defaults_post.py | 163 +++++++++++------- .../api/tests/get_basic_list_of_booleans.py | 40 +++-- .../api/tests/get_basic_list_of_floats.py | 40 +++-- .../api/tests/get_basic_list_of_integers.py | 40 +++-- .../api/tests/get_basic_list_of_strings.py | 40 +++-- ...tional_value_tests_optional_query_param.py | 12 +- .../models/model_with_any_json_properties.py | 43 ++--- ...el_with_primitive_additional_properties.py | 4 +- .../models/model_with_union_property.py | 23 ++- .../models/validation_error.py | 27 +-- .../parser/properties/__init__.py | 26 +-- .../parser/properties/enum_property.py | 13 +- .../parser/properties/model_property.py | 12 +- .../parser/properties/property.py | 5 +- .../templates/endpoint_macros.pyi | 2 +- 25 files changed, 396 insertions(+), 298 deletions(-) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py index 574d5018b..1c8324081 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py @@ -40,16 +40,22 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal def httpx_request( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: json_datetime_prop: Union[Unset, str] = UNSET @@ -68,16 +74,23 @@ def httpx_request( json_list_prop.append(list_prop_item) - json_union_prop: Union[Unset, float, str] + json_union_prop: Union[Unset, Union[float, str]] if isinstance(union_prop, Unset): json_union_prop = UNSET else: json_union_prop = union_prop - json_union_prop_with_ref: Union[Unset, float, AnEnum] + json_union_prop_with_ref: Union[ + Unset, + Union[ + float, + ], + ] if isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = UNSET - elif isinstance(union_prop_with_ref, AnEnum): + elif isinstance( + union_prop_with_ref, + ): json_union_prop_with_ref = UNSET if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref @@ -90,25 +103,25 @@ def httpx_request( json_enum_prop = enum_prop params: Dict[str, Any] = {} - if string_prop is not UNSET: + if string_prop is not UNSET and string_prop is not None: params["string_prop"] = string_prop - if datetime_prop is not UNSET: + if datetime_prop is not UNSET and datetime_prop is not None: params["datetime_prop"] = json_datetime_prop - if date_prop is not UNSET: + if date_prop is not UNSET and date_prop is not None: params["date_prop"] = json_date_prop - if float_prop is not UNSET: + if float_prop is not UNSET and float_prop is not None: params["float_prop"] = float_prop - if int_prop is not UNSET: + if int_prop is not UNSET and int_prop is not None: params["int_prop"] = int_prop - if boolean_prop is not UNSET: + if boolean_prop is not UNSET and boolean_prop is not None: params["boolean_prop"] = boolean_prop - if list_prop is not UNSET: + if list_prop is not UNSET and list_prop is not None: params["list_prop"] = json_list_prop - if union_prop is not UNSET: + if union_prop is not UNSET and union_prop is not None: params["union_prop"] = json_union_prop - if union_prop_with_ref is not UNSET: + if union_prop_with_ref is not UNSET and union_prop_with_ref is not None: params["union_prop_with_ref"] = json_union_prop_with_ref - if enum_prop is not UNSET: + if enum_prop is not UNSET and enum_prop is not None: params["enum_prop"] = json_enum_prop response = client.request( diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py index e048fb001..78a8d64cd 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py @@ -11,12 +11,13 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]: if response.status_code == 200: - response_200 = cast(List[bool], response.json()) + response_200 = cast(, response.json()) return response_200 return None + def _build_response(*, response: httpx.Response) -> Response[List[bool]]: return Response( status_code=response.status_code, @@ -26,14 +27,16 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]: ) -def httpx_request( - *, +def httpx_request(*, client: Client, ) -> Response[List[bool]]: + + + response = client.request( "get", "/tests/basic_lists/booleans", ) - return _build_response(response=response) + return _build_response(response=response) \ No newline at end of file diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py index a4e9f5fd4..fbf269728 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py @@ -11,12 +11,13 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[float]]: if response.status_code == 200: - response_200 = cast(List[float], response.json()) + response_200 = cast(, response.json()) return response_200 return None + def _build_response(*, response: httpx.Response) -> Response[List[float]]: return Response( status_code=response.status_code, @@ -26,14 +27,16 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]: ) -def httpx_request( - *, +def httpx_request(*, client: Client, ) -> Response[List[float]]: + + + response = client.request( "get", "/tests/basic_lists/floats", ) - return _build_response(response=response) + return _build_response(response=response) \ No newline at end of file diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py index 232a3c7bb..875850a67 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py @@ -11,12 +11,13 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[int]]: if response.status_code == 200: - response_200 = cast(List[int], response.json()) + response_200 = cast(, response.json()) return response_200 return None + def _build_response(*, response: httpx.Response) -> Response[List[int]]: return Response( status_code=response.status_code, @@ -26,14 +27,16 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]: ) -def httpx_request( - *, +def httpx_request(*, client: Client, ) -> Response[List[int]]: + + + response = client.request( "get", "/tests/basic_lists/integers", ) - return _build_response(response=response) + return _build_response(response=response) \ No newline at end of file diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py index a16a16932..adfb473c9 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py @@ -11,12 +11,13 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[str]]: if response.status_code == 200: - response_200 = cast(List[str], response.json()) + response_200 = cast(, response.json()) return response_200 return None + def _build_response(*, response: httpx.Response) -> Response[List[str]]: return Response( status_code=response.status_code, @@ -26,14 +27,16 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]: ) -def httpx_request( - *, +def httpx_request(*, client: Client, ) -> Response[List[str]]: + + + response = client.request( "get", "/tests/basic_lists/strings", ) - return _build_response(response=response) + return _build_response(response=response) \ No newline at end of file diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py index bff43cc10..501452435 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py @@ -36,7 +36,7 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal def httpx_request( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Response[Union[None, HTTPValidationError]]: json_query_param: Union[Unset, List[Any]] = UNSET @@ -44,7 +44,7 @@ def httpx_request( json_query_param = query_param params: Dict[str, Any] = {} - if query_param is not UNSET: + if query_param is not UNSET and query_param is not None: params["query_param"] = json_query_param response = client.request( diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py index 62481f913..79ca4cdf4 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py @@ -3,62 +3,61 @@ import attr from ..models.model_with_any_json_properties_additional_property import ModelWithAnyJsonPropertiesAdditionalProperty -from ..types import Unset +from ..types import UNSET, Unset @attr.s(auto_attribs=True) class ModelWithAnyJsonProperties: """ """ + additional_properties: Dict[str, Union[, , str, float, int, bool]] = attr.ib(init=False, factory=dict) - additional_properties: Dict[ - str, Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] - ] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: field_dict: Dict[str, Any] = {} for prop_name, prop in self.additional_properties.items(): - if isinstance(prop, ModelWithAnyJsonPropertiesAdditionalProperty): + if isinstance(prop, ): field_dict[prop_name] = prop.to_dict() elif isinstance(prop, list): field_dict[prop_name] = prop + + + else: field_dict[prop_name] = prop - field_dict.update({}) + + field_dict.update({ + }) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonProperties": d = src_dict.copy() - model_with_any_json_properties = ModelWithAnyJsonProperties() + model_with_any_json_properties = ModelWithAnyJsonProperties( + ) additional_properties = {} for prop_name, prop_dict in d.items(): - - def _parse_additional_property( - data: Any, - ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: + def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: data = None if isinstance(data, Unset) else data - additional_property: Union[ - ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool - ] + additional_property: Union[, , str, float, int, bool] try: additional_property = ModelWithAnyJsonPropertiesAdditionalProperty.from_dict(data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass try: - additional_property = cast(List[str], data) + additional_property = cast(, data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass - return cast(Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool], data) + return cast(Union[, , str, float, int, bool], data) additional_property = _parse_additional_property(prop_dict) @@ -71,14 +70,10 @@ def _parse_additional_property( def additional_keys(self) -> List[str]: return list(self.additional_properties.keys()) - def __getitem__( - self, key: str - ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: + def __getitem__(self, key: str) -> Union[, , str, float, int, bool]: return self.additional_properties[key] - def __setitem__( - self, key: str, value: Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] - ) -> None: + def __setitem__(self, key: str, value: Union[, , str, float, int, bool]) -> None: self.additional_properties[key] = value def __delitem__(self, key: str) -> None: diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py index 47d65d90b..feb30878d 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py @@ -12,7 +12,7 @@ class ModelWithPrimitiveAdditionalProperties: """ """ - a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET + a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET additional_properties: Dict[str, str] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: @@ -31,7 +31,7 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalProperties": d = src_dict.copy() - a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET + a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET _a_date_holder = d.pop("a_date_holder", UNSET) if _a_date_holder is not None and not isinstance(_a_date_holder, Unset): a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict( diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py index cbecc7c90..ee81c6bcb 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Union +from typing import Any, Dict, Union, cast import attr @@ -10,14 +10,14 @@ @attr.s(auto_attribs=True) class ModelWithUnionProperty: """ """ + a_property: Union[Unset, Union[, ]] = UNSET - a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET def to_dict(self) -> Dict[str, Any]: - a_property: Union[Unset, AnEnum, AnIntEnum] + a_property: Union[Unset, Union[, ]] if isinstance(self.a_property, Unset): a_property = UNSET - elif isinstance(self.a_property, AnEnum): + elif isinstance(self.a_property, ): a_property = UNSET if not isinstance(self.a_property, Unset): a_property = self.a_property @@ -27,8 +27,12 @@ def to_dict(self) -> Dict[str, Any]: if not isinstance(self.a_property, Unset): a_property = self.a_property + + + field_dict: Dict[str, Any] = {} - field_dict.update({}) + field_dict.update({ + }) if a_property is not UNSET: field_dict["a_property"] = a_property @@ -37,10 +41,9 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithUnionProperty": d = src_dict.copy() - - def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: + def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: data = None if isinstance(data, Unset) else data - a_property: Union[Unset, AnEnum, AnIntEnum] + a_property: Union[Unset, Union[, ]] try: a_property = UNSET _a_property = data @@ -48,7 +51,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: a_property = AnEnum(_a_property) return a_property - except: # noqa: E722 + except: # noqa: E722 pass a_property = UNSET _a_property = data @@ -59,8 +62,10 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: a_property = _parse_a_property(d.pop("a_property", UNSET)) + model_with_union_property = ModelWithUnionProperty( a_property=a_property, ) return model_with_union_property + diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py index 2cd2b8959..b6a4b5ad0 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py @@ -2,36 +2,40 @@ import attr +from ..types import UNSET, Unset + @attr.s(auto_attribs=True) class ValidationError: """ """ - loc: List[str] msg: str type: str + def to_dict(self) -> Dict[str, Any]: loc = self.loc - msg = self.msg - type = self.type + + + + msg = self.msg + type = self.type field_dict: Dict[str, Any] = {} - field_dict.update( - { - "loc": loc, - "msg": msg, - "type": type, - } - ) + field_dict.update({ + "loc": loc, + "msg": msg, + "type": type, + }) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": d = src_dict.copy() - loc = cast(List[str], d.pop("loc")) + loc = cast(, d.pop("loc")) + msg = d.pop("msg") @@ -44,3 +48,4 @@ def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": ) return validation_error + diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index 9242cddaa..fc73c9625 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -13,16 +13,22 @@ def _get_kwargs( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Dict[str, Any]: url = "{}/tests/defaults".format(client.base_url) @@ -44,16 +50,23 @@ def _get_kwargs( json_list_prop.append(list_prop_item) - json_union_prop: Union[Unset, float, str] + json_union_prop: Union[Unset, Union[float, str]] if isinstance(union_prop, Unset): json_union_prop = UNSET else: json_union_prop = union_prop - json_union_prop_with_ref: Union[Unset, float, AnEnum] + json_union_prop_with_ref: Union[ + Unset, + Union[ + float, + ], + ] if isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = UNSET - elif isinstance(union_prop_with_ref, AnEnum): + elif isinstance( + union_prop_with_ref, + ): json_union_prop_with_ref = UNSET if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref @@ -66,25 +79,25 @@ def _get_kwargs( json_enum_prop = enum_prop params: Dict[str, Any] = {} - if string_prop is not UNSET: + if string_prop is not UNSET and string_prop is not None: params["string_prop"] = string_prop - if datetime_prop is not UNSET: + if datetime_prop is not UNSET and datetime_prop is not None: params["datetime_prop"] = json_datetime_prop - if date_prop is not UNSET: + if date_prop is not UNSET and date_prop is not None: params["date_prop"] = json_date_prop - if float_prop is not UNSET: + if float_prop is not UNSET and float_prop is not None: params["float_prop"] = float_prop - if int_prop is not UNSET: + if int_prop is not UNSET and int_prop is not None: params["int_prop"] = int_prop - if boolean_prop is not UNSET: + if boolean_prop is not UNSET and boolean_prop is not None: params["boolean_prop"] = boolean_prop - if list_prop is not UNSET: + if list_prop is not UNSET and list_prop is not None: params["list_prop"] = json_list_prop - if union_prop is not UNSET: + if union_prop is not UNSET and union_prop is not None: params["union_prop"] = json_union_prop - if union_prop_with_ref is not UNSET: + if union_prop_with_ref is not UNSET and union_prop_with_ref is not None: params["union_prop_with_ref"] = json_union_prop_with_ref - if enum_prop is not UNSET: + if enum_prop is not UNSET and enum_prop is not None: params["enum_prop"] = json_enum_prop return { @@ -120,16 +133,22 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal def sync_detailed( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( client=client, @@ -155,16 +174,22 @@ def sync_detailed( def sync( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ """ @@ -186,16 +211,22 @@ def sync( async def asyncio_detailed( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( client=client, @@ -220,16 +251,22 @@ async def asyncio_detailed( async def asyncio( *, client: Client, - string_prop: Union[Unset, str] = "the default string", - datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"), - date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(), - float_prop: Union[Unset, float] = 3.14, - int_prop: Union[Unset, int] = 7, - boolean_prop: Union[Unset, bool] = False, - list_prop: Union[Unset, List[AnEnum]] = UNSET, - union_prop: Union[Unset, float, str] = "not a float", - union_prop_with_ref: Union[Unset, float, AnEnum] = 0.6, - enum_prop: Union[Unset, AnEnum] = UNSET, + string_prop: Union[Unset, None, str] = "the default string", + datetime_prop: Union[Unset, None, datetime.datetime] = isoparse("1010-10-10T00:00:00"), + date_prop: Union[Unset, None, datetime.date] = isoparse("1010-10-10").date(), + float_prop: Union[Unset, None, float] = 3.14, + int_prop: Union[Unset, None, int] = 7, + boolean_prop: Union[Unset, None, bool] = False, + list_prop: Union[Unset, None, List[AnEnum]] = None, + union_prop: Union[Unset, None, Union[float, str]] = "not a float", + union_prop_with_ref: Union[ + Unset, + None, + Union[ + float, + ], + ] = 0.6, + enum_prop: Union[Unset, None, AnEnum] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py index eeedd5337..44790e090 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py @@ -1,19 +1,28 @@ -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, Union, cast import httpx +from attr import asdict -from ...client import Client +from ...client import AuthenticatedClient, Client from ...types import Response def _get_kwargs( *, client: Client, + ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/booleans".format(client.base_url) + url = "{}/tests/basic_lists/booleans".format( + client.base_url) headers: Dict[str, Any] = client.get_headers() + + + + + + return { "url": url, "headers": headers, @@ -24,7 +33,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]: if response.status_code == 200: - response_200 = cast(List[bool], response.json()) + response_200 = cast(, response.json()) return response_200 return None @@ -42,9 +51,11 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]: def sync_detailed( *, client: Client, + ) -> Response[List[bool]]: kwargs = _get_kwargs( client=client, + ) response = httpx.get( @@ -53,40 +64,43 @@ def sync_detailed( return _build_response(response=response) - def sync( *, client: Client, + ) -> Optional[List[bool]]: """ Get a list of booleans """ return sync_detailed( client=client, - ).parsed + ).parsed async def asyncio_detailed( *, client: Client, + ) -> Response[List[bool]]: kwargs = _get_kwargs( client=client, + ) async with httpx.AsyncClient() as _client: - response = await _client.get(**kwargs) + response = await _client.get( + **kwargs + ) return _build_response(response=response) - async def asyncio( *, client: Client, + ) -> Optional[List[bool]]: """ Get a list of booleans """ - return ( - await asyncio_detailed( - client=client, - ) - ).parsed + return (await asyncio_detailed( + client=client, + + )).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py index 84735b823..f463b3470 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py @@ -1,19 +1,28 @@ -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, Union, cast import httpx +from attr import asdict -from ...client import Client +from ...client import AuthenticatedClient, Client from ...types import Response def _get_kwargs( *, client: Client, + ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/floats".format(client.base_url) + url = "{}/tests/basic_lists/floats".format( + client.base_url) headers: Dict[str, Any] = client.get_headers() + + + + + + return { "url": url, "headers": headers, @@ -24,7 +33,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[float]]: if response.status_code == 200: - response_200 = cast(List[float], response.json()) + response_200 = cast(, response.json()) return response_200 return None @@ -42,9 +51,11 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]: def sync_detailed( *, client: Client, + ) -> Response[List[float]]: kwargs = _get_kwargs( client=client, + ) response = httpx.get( @@ -53,40 +64,43 @@ def sync_detailed( return _build_response(response=response) - def sync( *, client: Client, + ) -> Optional[List[float]]: """ Get a list of floats """ return sync_detailed( client=client, - ).parsed + ).parsed async def asyncio_detailed( *, client: Client, + ) -> Response[List[float]]: kwargs = _get_kwargs( client=client, + ) async with httpx.AsyncClient() as _client: - response = await _client.get(**kwargs) + response = await _client.get( + **kwargs + ) return _build_response(response=response) - async def asyncio( *, client: Client, + ) -> Optional[List[float]]: """ Get a list of floats """ - return ( - await asyncio_detailed( - client=client, - ) - ).parsed + return (await asyncio_detailed( + client=client, + + )).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py index 56197de7c..47c3185c6 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py @@ -1,19 +1,28 @@ -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, Union, cast import httpx +from attr import asdict -from ...client import Client +from ...client import AuthenticatedClient, Client from ...types import Response def _get_kwargs( *, client: Client, + ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/integers".format(client.base_url) + url = "{}/tests/basic_lists/integers".format( + client.base_url) headers: Dict[str, Any] = client.get_headers() + + + + + + return { "url": url, "headers": headers, @@ -24,7 +33,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[int]]: if response.status_code == 200: - response_200 = cast(List[int], response.json()) + response_200 = cast(, response.json()) return response_200 return None @@ -42,9 +51,11 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]: def sync_detailed( *, client: Client, + ) -> Response[List[int]]: kwargs = _get_kwargs( client=client, + ) response = httpx.get( @@ -53,40 +64,43 @@ def sync_detailed( return _build_response(response=response) - def sync( *, client: Client, + ) -> Optional[List[int]]: """ Get a list of integers """ return sync_detailed( client=client, - ).parsed + ).parsed async def asyncio_detailed( *, client: Client, + ) -> Response[List[int]]: kwargs = _get_kwargs( client=client, + ) async with httpx.AsyncClient() as _client: - response = await _client.get(**kwargs) + response = await _client.get( + **kwargs + ) return _build_response(response=response) - async def asyncio( *, client: Client, + ) -> Optional[List[int]]: """ Get a list of integers """ - return ( - await asyncio_detailed( - client=client, - ) - ).parsed + return (await asyncio_detailed( + client=client, + + )).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py index d75f452fb..3c71ee57a 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py @@ -1,19 +1,28 @@ -from typing import Any, Dict, List, Optional, cast +from typing import Any, Dict, List, Optional, Union, cast import httpx +from attr import asdict -from ...client import Client +from ...client import AuthenticatedClient, Client from ...types import Response def _get_kwargs( *, client: Client, + ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/strings".format(client.base_url) + url = "{}/tests/basic_lists/strings".format( + client.base_url) headers: Dict[str, Any] = client.get_headers() + + + + + + return { "url": url, "headers": headers, @@ -24,7 +33,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[str]]: if response.status_code == 200: - response_200 = cast(List[str], response.json()) + response_200 = cast(, response.json()) return response_200 return None @@ -42,9 +51,11 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]: def sync_detailed( *, client: Client, + ) -> Response[List[str]]: kwargs = _get_kwargs( client=client, + ) response = httpx.get( @@ -53,40 +64,43 @@ def sync_detailed( return _build_response(response=response) - def sync( *, client: Client, + ) -> Optional[List[str]]: """ Get a list of strings """ return sync_detailed( client=client, - ).parsed + ).parsed async def asyncio_detailed( *, client: Client, + ) -> Response[List[str]]: kwargs = _get_kwargs( client=client, + ) async with httpx.AsyncClient() as _client: - response = await _client.get(**kwargs) + response = await _client.get( + **kwargs + ) return _build_response(response=response) - async def asyncio( *, client: Client, + ) -> Optional[List[str]]: """ Get a list of strings """ - return ( - await asyncio_detailed( - client=client, - ) - ).parsed + return (await asyncio_detailed( + client=client, + + )).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py index 751f48e03..1ceeec786 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py @@ -10,7 +10,7 @@ def _get_kwargs( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Dict[str, Any]: url = "{}/tests/optional_query_param/".format(client.base_url) @@ -21,7 +21,7 @@ def _get_kwargs( json_query_param = query_param params: Dict[str, Any] = {} - if query_param is not UNSET: + if query_param is not UNSET and query_param is not None: params["query_param"] = json_query_param return { @@ -57,7 +57,7 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal def sync_detailed( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( client=client, @@ -74,7 +74,7 @@ def sync_detailed( def sync( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ Test optional query parameters """ @@ -87,7 +87,7 @@ def sync( async def asyncio_detailed( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( client=client, @@ -103,7 +103,7 @@ async def asyncio_detailed( async def asyncio( *, client: Client, - query_param: Union[Unset, List[str]] = UNSET, + query_param: Union[Unset, None, List[str]] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ Test optional query parameters """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py index 62481f913..79ca4cdf4 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py @@ -3,62 +3,61 @@ import attr from ..models.model_with_any_json_properties_additional_property import ModelWithAnyJsonPropertiesAdditionalProperty -from ..types import Unset +from ..types import UNSET, Unset @attr.s(auto_attribs=True) class ModelWithAnyJsonProperties: """ """ + additional_properties: Dict[str, Union[, , str, float, int, bool]] = attr.ib(init=False, factory=dict) - additional_properties: Dict[ - str, Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] - ] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: field_dict: Dict[str, Any] = {} for prop_name, prop in self.additional_properties.items(): - if isinstance(prop, ModelWithAnyJsonPropertiesAdditionalProperty): + if isinstance(prop, ): field_dict[prop_name] = prop.to_dict() elif isinstance(prop, list): field_dict[prop_name] = prop + + + else: field_dict[prop_name] = prop - field_dict.update({}) + + field_dict.update({ + }) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonProperties": d = src_dict.copy() - model_with_any_json_properties = ModelWithAnyJsonProperties() + model_with_any_json_properties = ModelWithAnyJsonProperties( + ) additional_properties = {} for prop_name, prop_dict in d.items(): - - def _parse_additional_property( - data: Any, - ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: + def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: data = None if isinstance(data, Unset) else data - additional_property: Union[ - ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool - ] + additional_property: Union[, , str, float, int, bool] try: additional_property = ModelWithAnyJsonPropertiesAdditionalProperty.from_dict(data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass try: - additional_property = cast(List[str], data) + additional_property = cast(, data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass - return cast(Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool], data) + return cast(Union[, , str, float, int, bool], data) additional_property = _parse_additional_property(prop_dict) @@ -71,14 +70,10 @@ def _parse_additional_property( def additional_keys(self) -> List[str]: return list(self.additional_properties.keys()) - def __getitem__( - self, key: str - ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: + def __getitem__(self, key: str) -> Union[, , str, float, int, bool]: return self.additional_properties[key] - def __setitem__( - self, key: str, value: Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] - ) -> None: + def __setitem__(self, key: str, value: Union[, , str, float, int, bool]) -> None: self.additional_properties[key] = value def __delitem__(self, key: str) -> None: diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py index 47d65d90b..feb30878d 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py @@ -12,7 +12,7 @@ class ModelWithPrimitiveAdditionalProperties: """ """ - a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET + a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET additional_properties: Dict[str, str] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: @@ -31,7 +31,7 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalProperties": d = src_dict.copy() - a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET + a_date_holder: Union[Unset, ModelWithPrimitiveAdditionalPropertiesADateHolder] = UNSET _a_date_holder = d.pop("a_date_holder", UNSET) if _a_date_holder is not None and not isinstance(_a_date_holder, Unset): a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict( diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index cbecc7c90..ee81c6bcb 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Union +from typing import Any, Dict, Union, cast import attr @@ -10,14 +10,14 @@ @attr.s(auto_attribs=True) class ModelWithUnionProperty: """ """ + a_property: Union[Unset, Union[, ]] = UNSET - a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET def to_dict(self) -> Dict[str, Any]: - a_property: Union[Unset, AnEnum, AnIntEnum] + a_property: Union[Unset, Union[, ]] if isinstance(self.a_property, Unset): a_property = UNSET - elif isinstance(self.a_property, AnEnum): + elif isinstance(self.a_property, ): a_property = UNSET if not isinstance(self.a_property, Unset): a_property = self.a_property @@ -27,8 +27,12 @@ def to_dict(self) -> Dict[str, Any]: if not isinstance(self.a_property, Unset): a_property = self.a_property + + + field_dict: Dict[str, Any] = {} - field_dict.update({}) + field_dict.update({ + }) if a_property is not UNSET: field_dict["a_property"] = a_property @@ -37,10 +41,9 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithUnionProperty": d = src_dict.copy() - - def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: + def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: data = None if isinstance(data, Unset) else data - a_property: Union[Unset, AnEnum, AnIntEnum] + a_property: Union[Unset, Union[, ]] try: a_property = UNSET _a_property = data @@ -48,7 +51,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: a_property = AnEnum(_a_property) return a_property - except: # noqa: E722 + except: # noqa: E722 pass a_property = UNSET _a_property = data @@ -59,8 +62,10 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: a_property = _parse_a_property(d.pop("a_property", UNSET)) + model_with_union_property = ModelWithUnionProperty( a_property=a_property, ) return model_with_union_property + diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py index 2cd2b8959..b6a4b5ad0 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py @@ -2,36 +2,40 @@ import attr +from ..types import UNSET, Unset + @attr.s(auto_attribs=True) class ValidationError: """ """ - loc: List[str] msg: str type: str + def to_dict(self) -> Dict[str, Any]: loc = self.loc - msg = self.msg - type = self.type + + + + msg = self.msg + type = self.type field_dict: Dict[str, Any] = {} - field_dict.update( - { - "loc": loc, - "msg": msg, - "type": type, - } - ) + field_dict.update({ + "loc": loc, + "msg": msg, + "type": type, + }) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": d = src_dict.copy() - loc = cast(List[str], d.pop("loc")) + loc = cast(, d.pop("loc")) + msg = d.pop("msg") @@ -44,3 +48,4 @@ def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": ) return validation_error + diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index 487f5b464..a6e3c1c1c 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -124,16 +124,8 @@ class ListProperty(Property, Generic[InnerProp]): inner_property: InnerProp template: ClassVar[str] = "list_property.pyi" - def get_type_string(self, no_optional: bool = False) -> str: - """ Get a string representation of type that should be used when declaring this property """ - type_string = f"List[{self.inner_property.get_type_string()}]" - if no_optional: - return type_string - if self.nullable: - type_string = f"Optional[{type_string}]" - if not self.required: - type_string = f"Union[Unset, {type_string}]" - return type_string + def get_base_type_string(self) -> str: + return f"List[{self.inner_property.get_type_string()}]" def get_instance_type_string(self) -> str: """Get a string representation of runtime type that should be used for `isinstance` checks""" @@ -166,19 +158,11 @@ def __attrs_post_init__(self) -> None: object.__setattr__( self, "has_properties_without_templates", any(prop.template is None for prop in self.inner_properties) ) - - def get_type_string(self, no_optional: bool = False) -> str: - """ Get a string representation of type that should be used when declaring this property """ + + def get_base_type_string(self) -> str: inner_types = [p.get_type_string(no_optional=True) for p in self.inner_properties] inner_prop_string = ", ".join(inner_types) - type_string = f"Union[{inner_prop_string}]" - if no_optional: - return type_string - if not self.required: - type_string = f"Union[Unset, {inner_prop_string}]" - if self.nullable: - type_string = f"Optional[{type_string}]" - return type_string + return f"Union[{inner_prop_string}]" def get_imports(self, *, prefix: str) -> Set[str]: """ diff --git a/openapi_python_client/parser/properties/enum_property.py b/openapi_python_client/parser/properties/enum_property.py index 1217f23ee..08fce294d 100644 --- a/openapi_python_client/parser/properties/enum_property.py +++ b/openapi_python_client/parser/properties/enum_property.py @@ -22,17 +22,8 @@ class EnumProperty(Property): template: ClassVar[str] = "enum_property.pyi" - def get_type_string(self, no_optional: bool = False) -> str: - """ Get a string representation of type that should be used when declaring this property """ - - type_string = self.reference.class_name - if no_optional: - return type_string - if self.nullable: - type_string = f"Optional[{type_string}]" - if not self.required: - type_string = f"Union[Unset, {type_string}]" - return type_string + def get_base_type_string(self) -> str: + return self.reference.class_name def get_imports(self, *, prefix: str) -> Set[str]: """ diff --git a/openapi_python_client/parser/properties/model_property.py b/openapi_python_client/parser/properties/model_property.py index 40d1f930b..7ebc804b2 100644 --- a/openapi_python_client/parser/properties/model_property.py +++ b/openapi_python_client/parser/properties/model_property.py @@ -71,16 +71,8 @@ def resolve_references( return schemas - def get_type_string(self, no_optional: bool = False) -> str: - """ Get a string representation of type that should be used when declaring this property """ - type_string = self.reference.class_name - if no_optional: - return type_string - if self.nullable: - type_string = f"Optional[{type_string}]" - if not self.required: - type_string = f"Union[{type_string}, Unset]" - return type_string + def get_base_type_string(self) -> str: + return self.reference.class_name def get_imports(self, *, prefix: str) -> Set[str]: """ diff --git a/openapi_python_client/parser/properties/property.py b/openapi_python_client/parser/properties/property.py index c01677d59..4ec1c516a 100644 --- a/openapi_python_client/parser/properties/property.py +++ b/openapi_python_client/parser/properties/property.py @@ -31,6 +31,9 @@ class Property: def __attrs_post_init__(self) -> None: object.__setattr__(self, "python_name", utils.to_valid_python_identifier(utils.snake_case(self.name))) + + def get_base_type_string(self) -> str: + return self._type_string def get_type_string(self, no_optional: bool = False, query_parameter: bool = False) -> str: """ @@ -40,7 +43,7 @@ def get_type_string(self, no_optional: bool = False, query_parameter: bool = Fal no_optional: Do not include Optional or Unset even if the value is optional (needed for isinstance checks) query_arg: True if the property's type is being used for a query parameter """ - type_string = self._type_string + type_string = self.get_base_type_string() if no_optional: return self._type_string if self.nullable: diff --git a/openapi_python_client/templates/endpoint_macros.pyi b/openapi_python_client/templates/endpoint_macros.pyi index dac75ee41..c88f38e5f 100644 --- a/openapi_python_client/templates/endpoint_macros.pyi +++ b/openapi_python_client/templates/endpoint_macros.pyi @@ -96,7 +96,7 @@ json_body: {{ endpoint.json_body.get_type_string() }}, {% endif %} {# query parameters #} {% for parameter in endpoint.query_parameters %} -{{ parameter.to_query_method_arg() }}, +{{ parameter.to_string(query_parameter=True) }}, {% endfor %} {% for parameter in endpoint.header_parameters %} {{ parameter.to_string() }}, From 447bfa4218a47f12e9769b10029d732b0a940b71 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 14:46:15 -0500 Subject: [PATCH 4/7] Save WIP --- .../api/tests/defaults_tests_defaults_post.py | 23 ++----- .../api/tests/get_basic_list_of_booleans.py | 11 ++-- .../api/tests/get_basic_list_of_floats.py | 11 ++-- .../api/tests/get_basic_list_of_integers.py | 11 ++-- .../api/tests/get_basic_list_of_strings.py | 11 ++-- .../tests/upload_file_tests_upload_post.py | 2 +- .../models/model_with_any_json_properties.py | 43 +++++++------ .../models/model_with_union_property.py | 23 +++---- .../custom_e2e/models/validation_error.py | 27 ++++---- .../api/tests/defaults_tests_defaults_post.py | 63 ++++--------------- .../api/tests/get_basic_list_of_booleans.py | 40 ++++-------- .../api/tests/get_basic_list_of_floats.py | 40 ++++-------- .../api/tests/get_basic_list_of_integers.py | 40 ++++-------- .../api/tests/get_basic_list_of_strings.py | 40 ++++-------- .../tests/upload_file_tests_upload_post.py | 2 +- .../models/model_with_any_json_properties.py | 43 +++++++------ .../models/model_with_union_property.py | 23 +++---- .../models/validation_error.py | 27 ++++---- .../parser/properties/__init__.py | 23 ++++++- .../parser/properties/property.py | 2 +- 20 files changed, 197 insertions(+), 308 deletions(-) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py index 1c8324081..9d3425252 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py @@ -47,14 +47,8 @@ def httpx_request( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: @@ -74,23 +68,16 @@ def httpx_request( json_list_prop.append(list_prop_item) - json_union_prop: Union[Unset, Union[float, str]] + json_union_prop: Union[Unset, float, str] if isinstance(union_prop, Unset): json_union_prop = UNSET else: json_union_prop = union_prop - json_union_prop_with_ref: Union[ - Unset, - Union[ - float, - ], - ] + json_union_prop_with_ref: Union[Unset, float, AnEnum] if isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = UNSET - elif isinstance( - union_prop_with_ref, - ): + elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py index 78a8d64cd..e048fb001 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py @@ -11,13 +11,12 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[bool], response.json()) return response_200 return None - def _build_response(*, response: httpx.Response) -> Response[List[bool]]: return Response( status_code=response.status_code, @@ -27,16 +26,14 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]: ) -def httpx_request(*, +def httpx_request( + *, client: Client, ) -> Response[List[bool]]: - - - response = client.request( "get", "/tests/basic_lists/booleans", ) - return _build_response(response=response) \ No newline at end of file + return _build_response(response=response) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py index fbf269728..a4e9f5fd4 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py @@ -11,13 +11,12 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[float]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[float], response.json()) return response_200 return None - def _build_response(*, response: httpx.Response) -> Response[List[float]]: return Response( status_code=response.status_code, @@ -27,16 +26,14 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]: ) -def httpx_request(*, +def httpx_request( + *, client: Client, ) -> Response[List[float]]: - - - response = client.request( "get", "/tests/basic_lists/floats", ) - return _build_response(response=response) \ No newline at end of file + return _build_response(response=response) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py index 875850a67..232a3c7bb 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py @@ -11,13 +11,12 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[int]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[int], response.json()) return response_200 return None - def _build_response(*, response: httpx.Response) -> Response[List[int]]: return Response( status_code=response.status_code, @@ -27,16 +26,14 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]: ) -def httpx_request(*, +def httpx_request( + *, client: Client, ) -> Response[List[int]]: - - - response = client.request( "get", "/tests/basic_lists/integers", ) - return _build_response(response=response) \ No newline at end of file + return _build_response(response=response) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py index adfb473c9..a16a16932 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py @@ -11,13 +11,12 @@ def _parse_response(*, response: httpx.Response) -> Optional[List[str]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[str], response.json()) return response_200 return None - def _build_response(*, response: httpx.Response) -> Response[List[str]]: return Response( status_code=response.status_code, @@ -27,16 +26,14 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]: ) -def httpx_request(*, +def httpx_request( + *, client: Client, ) -> Response[List[str]]: - - - response = client.request( "get", "/tests/basic_lists/strings", ) - return _build_response(response=response) \ No newline at end of file + return _build_response(response=response) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py index 2d084a9e0..8e96ea276 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py @@ -49,7 +49,7 @@ def httpx_request(*, None, HTTPValidationError ]]: - if keep_alive is not UNSET: + if keep_alive is not UNSET and keep_alive is not None: headers["keep-alive"] = keep_alive diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py index 79ca4cdf4..62481f913 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py @@ -3,61 +3,62 @@ import attr from ..models.model_with_any_json_properties_additional_property import ModelWithAnyJsonPropertiesAdditionalProperty -from ..types import UNSET, Unset +from ..types import Unset @attr.s(auto_attribs=True) class ModelWithAnyJsonProperties: """ """ - additional_properties: Dict[str, Union[, , str, float, int, bool]] = attr.ib(init=False, factory=dict) + additional_properties: Dict[ + str, Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] + ] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: field_dict: Dict[str, Any] = {} for prop_name, prop in self.additional_properties.items(): - if isinstance(prop, ): + if isinstance(prop, ModelWithAnyJsonPropertiesAdditionalProperty): field_dict[prop_name] = prop.to_dict() elif isinstance(prop, list): field_dict[prop_name] = prop - - - else: field_dict[prop_name] = prop - - field_dict.update({ - }) + field_dict.update({}) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonProperties": d = src_dict.copy() - model_with_any_json_properties = ModelWithAnyJsonProperties( - ) + model_with_any_json_properties = ModelWithAnyJsonProperties() additional_properties = {} for prop_name, prop_dict in d.items(): - def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: + + def _parse_additional_property( + data: Any, + ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: data = None if isinstance(data, Unset) else data - additional_property: Union[, , str, float, int, bool] + additional_property: Union[ + ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool + ] try: additional_property = ModelWithAnyJsonPropertiesAdditionalProperty.from_dict(data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass try: - additional_property = cast(, data) + additional_property = cast(List[str], data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass - return cast(Union[, , str, float, int, bool], data) + return cast(Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool], data) additional_property = _parse_additional_property(prop_dict) @@ -70,10 +71,14 @@ def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: def additional_keys(self) -> List[str]: return list(self.additional_properties.keys()) - def __getitem__(self, key: str) -> Union[, , str, float, int, bool]: + def __getitem__( + self, key: str + ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: return self.additional_properties[key] - def __setitem__(self, key: str, value: Union[, , str, float, int, bool]) -> None: + def __setitem__( + self, key: str, value: Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] + ) -> None: self.additional_properties[key] = value def __delitem__(self, key: str) -> None: diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py index ee81c6bcb..cbecc7c90 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Union, cast +from typing import Any, Dict, Union import attr @@ -10,14 +10,14 @@ @attr.s(auto_attribs=True) class ModelWithUnionProperty: """ """ - a_property: Union[Unset, Union[, ]] = UNSET + a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET def to_dict(self) -> Dict[str, Any]: - a_property: Union[Unset, Union[, ]] + a_property: Union[Unset, AnEnum, AnIntEnum] if isinstance(self.a_property, Unset): a_property = UNSET - elif isinstance(self.a_property, ): + elif isinstance(self.a_property, AnEnum): a_property = UNSET if not isinstance(self.a_property, Unset): a_property = self.a_property @@ -27,12 +27,8 @@ def to_dict(self) -> Dict[str, Any]: if not isinstance(self.a_property, Unset): a_property = self.a_property - - - field_dict: Dict[str, Any] = {} - field_dict.update({ - }) + field_dict.update({}) if a_property is not UNSET: field_dict["a_property"] = a_property @@ -41,9 +37,10 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithUnionProperty": d = src_dict.copy() - def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: + + def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: data = None if isinstance(data, Unset) else data - a_property: Union[Unset, Union[, ]] + a_property: Union[Unset, AnEnum, AnIntEnum] try: a_property = UNSET _a_property = data @@ -51,7 +48,7 @@ def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: a_property = AnEnum(_a_property) return a_property - except: # noqa: E722 + except: # noqa: E722 pass a_property = UNSET _a_property = data @@ -62,10 +59,8 @@ def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: a_property = _parse_a_property(d.pop("a_property", UNSET)) - model_with_union_property = ModelWithUnionProperty( a_property=a_property, ) return model_with_union_property - diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py index b6a4b5ad0..2cd2b8959 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py @@ -2,40 +2,36 @@ import attr -from ..types import UNSET, Unset - @attr.s(auto_attribs=True) class ValidationError: """ """ + loc: List[str] msg: str type: str - def to_dict(self) -> Dict[str, Any]: loc = self.loc - - - - msg = self.msg - type = self.type + msg = self.msg + type = self.type field_dict: Dict[str, Any] = {} - field_dict.update({ - "loc": loc, - "msg": msg, - "type": type, - }) + field_dict.update( + { + "loc": loc, + "msg": msg, + "type": type, + } + ) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": d = src_dict.copy() - loc = cast(, d.pop("loc")) - + loc = cast(List[str], d.pop("loc")) msg = d.pop("msg") @@ -48,4 +44,3 @@ def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": ) return validation_error - diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index fc73c9625..8b7f1073e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -20,14 +20,8 @@ def _get_kwargs( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Dict[str, Any]: url = "{}/tests/defaults".format(client.base_url) @@ -50,23 +44,16 @@ def _get_kwargs( json_list_prop.append(list_prop_item) - json_union_prop: Union[Unset, Union[float, str]] + json_union_prop: Union[Unset, float, str] if isinstance(union_prop, Unset): json_union_prop = UNSET else: json_union_prop = union_prop - json_union_prop_with_ref: Union[ - Unset, - Union[ - float, - ], - ] + json_union_prop_with_ref: Union[Unset, float, AnEnum] if isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = UNSET - elif isinstance( - union_prop_with_ref, - ): + elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref @@ -140,14 +127,8 @@ def sync_detailed( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( @@ -181,14 +162,8 @@ def sync( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ """ @@ -218,14 +193,8 @@ async def asyncio_detailed( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Response[Union[None, HTTPValidationError]]: kwargs = _get_kwargs( @@ -258,14 +227,8 @@ async def asyncio( int_prop: Union[Unset, None, int] = 7, boolean_prop: Union[Unset, None, bool] = False, list_prop: Union[Unset, None, List[AnEnum]] = None, - union_prop: Union[Unset, None, Union[float, str]] = "not a float", - union_prop_with_ref: Union[ - Unset, - None, - Union[ - float, - ], - ] = 0.6, + union_prop: Union[Unset, None, float, str] = "not a float", + union_prop_with_ref: Union[Unset, None, float, AnEnum] = 0.6, enum_prop: Union[Unset, None, AnEnum] = None, ) -> Optional[Union[None, HTTPValidationError]]: """ """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py index 44790e090..eeedd5337 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_booleans.py @@ -1,28 +1,19 @@ -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Optional, cast import httpx -from attr import asdict -from ...client import AuthenticatedClient, Client +from ...client import Client from ...types import Response def _get_kwargs( *, client: Client, - ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/booleans".format( - client.base_url) + url = "{}/tests/basic_lists/booleans".format(client.base_url) headers: Dict[str, Any] = client.get_headers() - - - - - - return { "url": url, "headers": headers, @@ -33,7 +24,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[bool], response.json()) return response_200 return None @@ -51,11 +42,9 @@ def _build_response(*, response: httpx.Response) -> Response[List[bool]]: def sync_detailed( *, client: Client, - ) -> Response[List[bool]]: kwargs = _get_kwargs( client=client, - ) response = httpx.get( @@ -64,43 +53,40 @@ def sync_detailed( return _build_response(response=response) + def sync( *, client: Client, - ) -> Optional[List[bool]]: """ Get a list of booleans """ return sync_detailed( client=client, - ).parsed + async def asyncio_detailed( *, client: Client, - ) -> Response[List[bool]]: kwargs = _get_kwargs( client=client, - ) async with httpx.AsyncClient() as _client: - response = await _client.get( - **kwargs - ) + response = await _client.get(**kwargs) return _build_response(response=response) + async def asyncio( *, client: Client, - ) -> Optional[List[bool]]: """ Get a list of booleans """ - return (await asyncio_detailed( - client=client, - - )).parsed + return ( + await asyncio_detailed( + client=client, + ) + ).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py index f463b3470..84735b823 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_floats.py @@ -1,28 +1,19 @@ -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Optional, cast import httpx -from attr import asdict -from ...client import AuthenticatedClient, Client +from ...client import Client from ...types import Response def _get_kwargs( *, client: Client, - ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/floats".format( - client.base_url) + url = "{}/tests/basic_lists/floats".format(client.base_url) headers: Dict[str, Any] = client.get_headers() - - - - - - return { "url": url, "headers": headers, @@ -33,7 +24,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[float]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[float], response.json()) return response_200 return None @@ -51,11 +42,9 @@ def _build_response(*, response: httpx.Response) -> Response[List[float]]: def sync_detailed( *, client: Client, - ) -> Response[List[float]]: kwargs = _get_kwargs( client=client, - ) response = httpx.get( @@ -64,43 +53,40 @@ def sync_detailed( return _build_response(response=response) + def sync( *, client: Client, - ) -> Optional[List[float]]: """ Get a list of floats """ return sync_detailed( client=client, - ).parsed + async def asyncio_detailed( *, client: Client, - ) -> Response[List[float]]: kwargs = _get_kwargs( client=client, - ) async with httpx.AsyncClient() as _client: - response = await _client.get( - **kwargs - ) + response = await _client.get(**kwargs) return _build_response(response=response) + async def asyncio( *, client: Client, - ) -> Optional[List[float]]: """ Get a list of floats """ - return (await asyncio_detailed( - client=client, - - )).parsed + return ( + await asyncio_detailed( + client=client, + ) + ).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py index 47c3185c6..56197de7c 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_integers.py @@ -1,28 +1,19 @@ -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Optional, cast import httpx -from attr import asdict -from ...client import AuthenticatedClient, Client +from ...client import Client from ...types import Response def _get_kwargs( *, client: Client, - ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/integers".format( - client.base_url) + url = "{}/tests/basic_lists/integers".format(client.base_url) headers: Dict[str, Any] = client.get_headers() - - - - - - return { "url": url, "headers": headers, @@ -33,7 +24,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[int]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[int], response.json()) return response_200 return None @@ -51,11 +42,9 @@ def _build_response(*, response: httpx.Response) -> Response[List[int]]: def sync_detailed( *, client: Client, - ) -> Response[List[int]]: kwargs = _get_kwargs( client=client, - ) response = httpx.get( @@ -64,43 +53,40 @@ def sync_detailed( return _build_response(response=response) + def sync( *, client: Client, - ) -> Optional[List[int]]: """ Get a list of integers """ return sync_detailed( client=client, - ).parsed + async def asyncio_detailed( *, client: Client, - ) -> Response[List[int]]: kwargs = _get_kwargs( client=client, - ) async with httpx.AsyncClient() as _client: - response = await _client.get( - **kwargs - ) + response = await _client.get(**kwargs) return _build_response(response=response) + async def asyncio( *, client: Client, - ) -> Optional[List[int]]: """ Get a list of integers """ - return (await asyncio_detailed( - client=client, - - )).parsed + return ( + await asyncio_detailed( + client=client, + ) + ).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py index 3c71ee57a..d75f452fb 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/get_basic_list_of_strings.py @@ -1,28 +1,19 @@ -from typing import Any, Dict, List, Optional, Union, cast +from typing import Any, Dict, List, Optional, cast import httpx -from attr import asdict -from ...client import AuthenticatedClient, Client +from ...client import Client from ...types import Response def _get_kwargs( *, client: Client, - ) -> Dict[str, Any]: - url = "{}/tests/basic_lists/strings".format( - client.base_url) + url = "{}/tests/basic_lists/strings".format(client.base_url) headers: Dict[str, Any] = client.get_headers() - - - - - - return { "url": url, "headers": headers, @@ -33,7 +24,7 @@ def _get_kwargs( def _parse_response(*, response: httpx.Response) -> Optional[List[str]]: if response.status_code == 200: - response_200 = cast(, response.json()) + response_200 = cast(List[str], response.json()) return response_200 return None @@ -51,11 +42,9 @@ def _build_response(*, response: httpx.Response) -> Response[List[str]]: def sync_detailed( *, client: Client, - ) -> Response[List[str]]: kwargs = _get_kwargs( client=client, - ) response = httpx.get( @@ -64,43 +53,40 @@ def sync_detailed( return _build_response(response=response) + def sync( *, client: Client, - ) -> Optional[List[str]]: """ Get a list of strings """ return sync_detailed( client=client, - ).parsed + async def asyncio_detailed( *, client: Client, - ) -> Response[List[str]]: kwargs = _get_kwargs( client=client, - ) async with httpx.AsyncClient() as _client: - response = await _client.get( - **kwargs - ) + response = await _client.get(**kwargs) return _build_response(response=response) + async def asyncio( *, client: Client, - ) -> Optional[List[str]]: """ Get a list of strings """ - return (await asyncio_detailed( - client=client, - - )).parsed + return ( + await asyncio_detailed( + client=client, + ) + ).parsed diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py index f8a54ec77..c26fe2573 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py @@ -18,7 +18,7 @@ def _get_kwargs( headers: Dict[str, Any] = client.get_headers() - if keep_alive is not UNSET: + if keep_alive is not UNSET and keep_alive is not None: headers["keep-alive"] = keep_alive return { diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py index 79ca4cdf4..62481f913 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_any_json_properties.py @@ -3,61 +3,62 @@ import attr from ..models.model_with_any_json_properties_additional_property import ModelWithAnyJsonPropertiesAdditionalProperty -from ..types import UNSET, Unset +from ..types import Unset @attr.s(auto_attribs=True) class ModelWithAnyJsonProperties: """ """ - additional_properties: Dict[str, Union[, , str, float, int, bool]] = attr.ib(init=False, factory=dict) + additional_properties: Dict[ + str, Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] + ] = attr.ib(init=False, factory=dict) def to_dict(self) -> Dict[str, Any]: field_dict: Dict[str, Any] = {} for prop_name, prop in self.additional_properties.items(): - if isinstance(prop, ): + if isinstance(prop, ModelWithAnyJsonPropertiesAdditionalProperty): field_dict[prop_name] = prop.to_dict() elif isinstance(prop, list): field_dict[prop_name] = prop - - - else: field_dict[prop_name] = prop - - field_dict.update({ - }) + field_dict.update({}) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonProperties": d = src_dict.copy() - model_with_any_json_properties = ModelWithAnyJsonProperties( - ) + model_with_any_json_properties = ModelWithAnyJsonProperties() additional_properties = {} for prop_name, prop_dict in d.items(): - def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: + + def _parse_additional_property( + data: Any, + ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: data = None if isinstance(data, Unset) else data - additional_property: Union[, , str, float, int, bool] + additional_property: Union[ + ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool + ] try: additional_property = ModelWithAnyJsonPropertiesAdditionalProperty.from_dict(data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass try: - additional_property = cast(, data) + additional_property = cast(List[str], data) return additional_property - except: # noqa: E722 + except: # noqa: E722 pass - return cast(Union[, , str, float, int, bool], data) + return cast(Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool], data) additional_property = _parse_additional_property(prop_dict) @@ -70,10 +71,14 @@ def _parse_additional_property(data: Any) -> Union[, , str, float, int, bool]: def additional_keys(self) -> List[str]: return list(self.additional_properties.keys()) - def __getitem__(self, key: str) -> Union[, , str, float, int, bool]: + def __getitem__( + self, key: str + ) -> Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool]: return self.additional_properties[key] - def __setitem__(self, key: str, value: Union[, , str, float, int, bool]) -> None: + def __setitem__( + self, key: str, value: Union[ModelWithAnyJsonPropertiesAdditionalProperty, List[str], str, float, int, bool] + ) -> None: self.additional_properties[key] = value def __delitem__(self, key: str) -> None: diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index ee81c6bcb..cbecc7c90 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Union, cast +from typing import Any, Dict, Union import attr @@ -10,14 +10,14 @@ @attr.s(auto_attribs=True) class ModelWithUnionProperty: """ """ - a_property: Union[Unset, Union[, ]] = UNSET + a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET def to_dict(self) -> Dict[str, Any]: - a_property: Union[Unset, Union[, ]] + a_property: Union[Unset, AnEnum, AnIntEnum] if isinstance(self.a_property, Unset): a_property = UNSET - elif isinstance(self.a_property, ): + elif isinstance(self.a_property, AnEnum): a_property = UNSET if not isinstance(self.a_property, Unset): a_property = self.a_property @@ -27,12 +27,8 @@ def to_dict(self) -> Dict[str, Any]: if not isinstance(self.a_property, Unset): a_property = self.a_property - - - field_dict: Dict[str, Any] = {} - field_dict.update({ - }) + field_dict.update({}) if a_property is not UNSET: field_dict["a_property"] = a_property @@ -41,9 +37,10 @@ def to_dict(self) -> Dict[str, Any]: @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ModelWithUnionProperty": d = src_dict.copy() - def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: + + def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: data = None if isinstance(data, Unset) else data - a_property: Union[Unset, Union[, ]] + a_property: Union[Unset, AnEnum, AnIntEnum] try: a_property = UNSET _a_property = data @@ -51,7 +48,7 @@ def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: a_property = AnEnum(_a_property) return a_property - except: # noqa: E722 + except: # noqa: E722 pass a_property = UNSET _a_property = data @@ -62,10 +59,8 @@ def _parse_a_property(data: Any) -> Union[Unset, Union[, ]]: a_property = _parse_a_property(d.pop("a_property", UNSET)) - model_with_union_property = ModelWithUnionProperty( a_property=a_property, ) return model_with_union_property - diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py index b6a4b5ad0..2cd2b8959 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/validation_error.py @@ -2,40 +2,36 @@ import attr -from ..types import UNSET, Unset - @attr.s(auto_attribs=True) class ValidationError: """ """ + loc: List[str] msg: str type: str - def to_dict(self) -> Dict[str, Any]: loc = self.loc - - - - msg = self.msg - type = self.type + msg = self.msg + type = self.type field_dict: Dict[str, Any] = {} - field_dict.update({ - "loc": loc, - "msg": msg, - "type": type, - }) + field_dict.update( + { + "loc": loc, + "msg": msg, + "type": type, + } + ) return field_dict @staticmethod def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": d = src_dict.copy() - loc = cast(, d.pop("loc")) - + loc = cast(List[str], d.pop("loc")) msg = d.pop("msg") @@ -48,4 +44,3 @@ def from_dict(src_dict: Dict[str, Any]) -> "ValidationError": ) return validation_error - diff --git a/openapi_python_client/parser/properties/__init__.py b/openapi_python_client/parser/properties/__init__.py index a6e3c1c1c..4669769c8 100644 --- a/openapi_python_client/parser/properties/__init__.py +++ b/openapi_python_client/parser/properties/__init__.py @@ -158,11 +158,28 @@ def __attrs_post_init__(self) -> None: object.__setattr__( self, "has_properties_without_templates", any(prop.template is None for prop in self.inner_properties) ) + + def _get_inner_prop_string(self) -> List[str]: + inner_types = [p.get_type_string(no_optional=True) for p in self.inner_properties] + return ", ".join(inner_types) def get_base_type_string(self) -> str: - inner_types = [p.get_type_string(no_optional=True) for p in self.inner_properties] - inner_prop_string = ", ".join(inner_types) - return f"Union[{inner_prop_string}]" + return f"Union[{self._get_inner_prop_string()}]" + + def get_type_string(self, no_optional: bool = False, query_parameter: bool = False) -> str: + """ Get a string representation of type that should be used when declaring this property """ + type_string = self.get_base_type_string() + if no_optional: + return type_string + if self.nullable: + type_string = f"Optional[{type_string}]" + if not self.required: + if query_parameter: + # For query parameters, None has the same meaning as Unset + type_string = f"Union[Unset, None, {self._get_inner_prop_string()}]" + else: + type_string = f"Union[Unset, {self._get_inner_prop_string()}]" + return type_string def get_imports(self, *, prefix: str) -> Set[str]: """ diff --git a/openapi_python_client/parser/properties/property.py b/openapi_python_client/parser/properties/property.py index 4ec1c516a..41570004e 100644 --- a/openapi_python_client/parser/properties/property.py +++ b/openapi_python_client/parser/properties/property.py @@ -45,7 +45,7 @@ def get_type_string(self, no_optional: bool = False, query_parameter: bool = Fal """ type_string = self.get_base_type_string() if no_optional: - return self._type_string + return type_string if self.nullable: type_string = f"Optional[{type_string}]" if not self.required: From d61d7ff0e8db08b1621ea3cc92f61b50983ab4d4 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 14:49:10 -0500 Subject: [PATCH 5/7] Fix type errors --- .../api/tests/defaults_tests_defaults_post.py | 14 +++++++------- .../optional_value_tests_optional_query_param.py | 2 +- .../api/tests/upload_file_tests_upload_post.py | 2 +- .../custom_e2e/models/a_model.py | 2 +- .../custom_e2e/models/http_validation_error.py | 2 +- .../model_with_primitive_additional_properties.py | 2 +- .../custom_e2e/models/model_with_union_property.py | 6 +++--- .../api/tests/defaults_tests_defaults_post.py | 14 +++++++------- .../optional_value_tests_optional_query_param.py | 2 +- .../api/tests/upload_file_tests_upload_post.py | 2 +- .../my_test_api_client/models/a_model.py | 2 +- .../models/http_validation_error.py | 2 +- .../model_with_primitive_additional_properties.py | 2 +- .../models/model_with_union_property.py | 6 +++--- .../templates/property_templates/date_property.pyi | 2 +- .../property_templates/datetime_property.pyi | 2 +- .../templates/property_templates/enum_property.pyi | 2 +- .../templates/property_templates/file_property.pyi | 2 +- .../templates/property_templates/list_property.pyi | 2 +- .../property_templates/model_property.pyi | 2 +- .../property_templates/union_property.pyi | 2 +- 21 files changed, 37 insertions(+), 37 deletions(-) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py index 9d3425252..ecaf68a32 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py @@ -53,15 +53,15 @@ def httpx_request( ) -> Response[Union[None, HTTPValidationError]]: json_datetime_prop: Union[Unset, str] = UNSET - if not isinstance(datetime_prop, Unset): + if not isinstance(datetime_prop, Unset) and datetime_prop is not None: json_datetime_prop = datetime_prop.isoformat() json_date_prop: Union[Unset, str] = UNSET - if not isinstance(date_prop, Unset): + if not isinstance(date_prop, Unset) and date_prop is not None: json_date_prop = date_prop.isoformat() json_list_prop: Union[Unset, List[Any]] = UNSET - if not isinstance(list_prop, Unset): + if not isinstance(list_prop, Unset) and list_prop is not None: json_list_prop = [] for list_prop_item_data in list_prop: list_prop_item = list_prop_item_data.value @@ -69,24 +69,24 @@ def httpx_request( json_list_prop.append(list_prop_item) json_union_prop: Union[Unset, float, str] - if isinstance(union_prop, Unset): + if isinstance(union_prop, Unset) or union_prop is None: json_union_prop = UNSET else: json_union_prop = union_prop json_union_prop_with_ref: Union[Unset, float, AnEnum] - if isinstance(union_prop_with_ref, Unset): + if isinstance(union_prop_with_ref, Unset) or union_prop_with_ref is None: json_union_prop_with_ref = UNSET elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET - if not isinstance(union_prop_with_ref, Unset): + if not isinstance(union_prop_with_ref, Unset) and union_prop_with_ref is not None: json_union_prop_with_ref = union_prop_with_ref else: json_union_prop_with_ref = union_prop_with_ref json_enum_prop: Union[Unset, AnEnum] = UNSET - if not isinstance(enum_prop, Unset): + if not isinstance(enum_prop, Unset) and enum_prop is not None: json_enum_prop = enum_prop params: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py index 501452435..7b821fca6 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py @@ -40,7 +40,7 @@ def httpx_request( ) -> Response[Union[None, HTTPValidationError]]: json_query_param: Union[Unset, List[Any]] = UNSET - if not isinstance(query_param, Unset): + if not isinstance(query_param, Unset) and query_param is not None: json_query_param = query_param params: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py index 8e96ea276..2d084a9e0 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py @@ -49,7 +49,7 @@ def httpx_request(*, None, HTTPValidationError ]]: - if keep_alive is not UNSET and keep_alive is not None: + if keep_alive is not UNSET: headers["keep-alive"] = keep_alive diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py index 2bf3e140d..893d78ae4 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py @@ -36,7 +36,7 @@ def to_dict(self) -> Dict[str, Any]: a_date = self.a_date.isoformat() required_not_nullable = self.required_not_nullable nested_list_of_enums: Union[Unset, List[Any]] = UNSET - if not isinstance(self.nested_list_of_enums, Unset): + if not isinstance(self.nested_list_of_enums, Unset) and self.nested_list_of_enums is not None: nested_list_of_enums = [] for nested_list_of_enums_item_data in self.nested_list_of_enums: nested_list_of_enums_item = [] diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py index 1b25a6d98..d53b3b44e 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py @@ -14,7 +14,7 @@ class HTTPValidationError: def to_dict(self) -> Dict[str, Any]: detail: Union[Unset, List[Any]] = UNSET - if not isinstance(self.detail, Unset): + if not isinstance(self.detail, Unset) and self.detail is not None: detail = [] for detail_item_data in self.detail: detail_item = detail_item_data.to_dict() diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py index feb30878d..e4c1b1f8f 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py @@ -17,7 +17,7 @@ class ModelWithPrimitiveAdditionalProperties: def to_dict(self) -> Dict[str, Any]: a_date_holder: Union[Unset, Dict[str, Any]] = UNSET - if not isinstance(self.a_date_holder, Unset): + if not isinstance(self.a_date_holder, Unset) and self.a_date_holder is not None: a_date_holder = self.a_date_holder.to_dict() field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py index cbecc7c90..d3601c0a1 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py @@ -15,16 +15,16 @@ class ModelWithUnionProperty: def to_dict(self) -> Dict[str, Any]: a_property: Union[Unset, AnEnum, AnIntEnum] - if isinstance(self.a_property, Unset): + if isinstance(self.a_property, Unset) or self.a_property is None: a_property = UNSET elif isinstance(self.a_property, AnEnum): a_property = UNSET - if not isinstance(self.a_property, Unset): + if not isinstance(self.a_property, Unset) and self.a_property is not None: a_property = self.a_property else: a_property = UNSET - if not isinstance(self.a_property, Unset): + if not isinstance(self.a_property, Unset) and self.a_property is not None: a_property = self.a_property field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index 8b7f1073e..eeb690922 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -29,15 +29,15 @@ def _get_kwargs( headers: Dict[str, Any] = client.get_headers() json_datetime_prop: Union[Unset, str] = UNSET - if not isinstance(datetime_prop, Unset): + if not isinstance(datetime_prop, Unset) and datetime_prop is not None: json_datetime_prop = datetime_prop.isoformat() json_date_prop: Union[Unset, str] = UNSET - if not isinstance(date_prop, Unset): + if not isinstance(date_prop, Unset) and date_prop is not None: json_date_prop = date_prop.isoformat() json_list_prop: Union[Unset, List[Any]] = UNSET - if not isinstance(list_prop, Unset): + if not isinstance(list_prop, Unset) and list_prop is not None: json_list_prop = [] for list_prop_item_data in list_prop: list_prop_item = list_prop_item_data.value @@ -45,24 +45,24 @@ def _get_kwargs( json_list_prop.append(list_prop_item) json_union_prop: Union[Unset, float, str] - if isinstance(union_prop, Unset): + if isinstance(union_prop, Unset) or union_prop is None: json_union_prop = UNSET else: json_union_prop = union_prop json_union_prop_with_ref: Union[Unset, float, AnEnum] - if isinstance(union_prop_with_ref, Unset): + if isinstance(union_prop_with_ref, Unset) or union_prop_with_ref is None: json_union_prop_with_ref = UNSET elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET - if not isinstance(union_prop_with_ref, Unset): + if not isinstance(union_prop_with_ref, Unset) and union_prop_with_ref is not None: json_union_prop_with_ref = union_prop_with_ref else: json_union_prop_with_ref = union_prop_with_ref json_enum_prop: Union[Unset, AnEnum] = UNSET - if not isinstance(enum_prop, Unset): + if not isinstance(enum_prop, Unset) and enum_prop is not None: json_enum_prop = enum_prop params: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py index 1ceeec786..8058acb43 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/optional_value_tests_optional_query_param.py @@ -17,7 +17,7 @@ def _get_kwargs( headers: Dict[str, Any] = client.get_headers() json_query_param: Union[Unset, List[Any]] = UNSET - if not isinstance(query_param, Unset): + if not isinstance(query_param, Unset) and query_param is not None: json_query_param = query_param params: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py index c26fe2573..f8a54ec77 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/upload_file_tests_upload_post.py @@ -18,7 +18,7 @@ def _get_kwargs( headers: Dict[str, Any] = client.get_headers() - if keep_alive is not UNSET and keep_alive is not None: + if keep_alive is not UNSET: headers["keep-alive"] = keep_alive return { diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py index 2bf3e140d..893d78ae4 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py @@ -36,7 +36,7 @@ def to_dict(self) -> Dict[str, Any]: a_date = self.a_date.isoformat() required_not_nullable = self.required_not_nullable nested_list_of_enums: Union[Unset, List[Any]] = UNSET - if not isinstance(self.nested_list_of_enums, Unset): + if not isinstance(self.nested_list_of_enums, Unset) and self.nested_list_of_enums is not None: nested_list_of_enums = [] for nested_list_of_enums_item_data in self.nested_list_of_enums: nested_list_of_enums_item = [] diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py index 1b25a6d98..d53b3b44e 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py @@ -14,7 +14,7 @@ class HTTPValidationError: def to_dict(self) -> Dict[str, Any]: detail: Union[Unset, List[Any]] = UNSET - if not isinstance(self.detail, Unset): + if not isinstance(self.detail, Unset) and self.detail is not None: detail = [] for detail_item_data in self.detail: detail_item = detail_item_data.to_dict() diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py index feb30878d..e4c1b1f8f 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py @@ -17,7 +17,7 @@ class ModelWithPrimitiveAdditionalProperties: def to_dict(self) -> Dict[str, Any]: a_date_holder: Union[Unset, Dict[str, Any]] = UNSET - if not isinstance(self.a_date_holder, Unset): + if not isinstance(self.a_date_holder, Unset) and self.a_date_holder is not None: a_date_holder = self.a_date_holder.to_dict() field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index cbecc7c90..d3601c0a1 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -15,16 +15,16 @@ class ModelWithUnionProperty: def to_dict(self) -> Dict[str, Any]: a_property: Union[Unset, AnEnum, AnIntEnum] - if isinstance(self.a_property, Unset): + if isinstance(self.a_property, Unset) or self.a_property is None: a_property = UNSET elif isinstance(self.a_property, AnEnum): a_property = UNSET - if not isinstance(self.a_property, Unset): + if not isinstance(self.a_property, Unset) and self.a_property is not None: a_property = self.a_property else: a_property = UNSET - if not isinstance(self.a_property, Unset): + if not isinstance(self.a_property, Unset) and self.a_property is not None: a_property = self.a_property field_dict: Dict[str, Any] = {} diff --git a/openapi_python_client/templates/property_templates/date_property.pyi b/openapi_python_client/templates/property_templates/date_property.pyi index a3a980c8f..919d56eda 100644 --- a/openapi_python_client/templates/property_templates/date_property.pyi +++ b/openapi_python_client/templates/property_templates/date_property.pyi @@ -14,7 +14,7 @@ if _{{ property.python_name }} is not None: {{ destination }} = {{ source }}.isoformat() {% if property.nullable %}if {{ source }} else None {%endif%} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, str]{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} {{ destination }} = {{ source }}.isoformat() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/datetime_property.pyi b/openapi_python_client/templates/property_templates/datetime_property.pyi index b8e1b8ff0..ac31672a2 100644 --- a/openapi_python_client/templates/property_templates/datetime_property.pyi +++ b/openapi_python_client/templates/property_templates/datetime_property.pyi @@ -23,7 +23,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, str]{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} {{ destination }} = {{ source }}.isoformat() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/enum_property.pyi b/openapi_python_client/templates/property_templates/enum_property.pyi index 4765a6fd5..e4f8f52e5 100644 --- a/openapi_python_client/templates/property_templates/enum_property.pyi +++ b/openapi_python_client/templates/property_templates/enum_property.pyi @@ -18,7 +18,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% else %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} {{ destination }} = {{ source }} if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/file_property.pyi b/openapi_python_client/templates/property_templates/file_property.pyi index ffa3c20d9..95edf2e48 100644 --- a/openapi_python_client/templates/property_templates/file_property.pyi +++ b/openapi_python_client/templates/property_templates/file_property.pyi @@ -13,7 +13,7 @@ {% endif %} {% else %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} {{ destination }} = {{ source }}.to_tuple() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/list_property.pyi b/openapi_python_client/templates/property_templates/list_property.pyi index d05a13960..d6275878e 100644 --- a/openapi_python_client/templates/property_templates/list_property.pyi +++ b/openapi_python_client/templates/property_templates/list_property.pyi @@ -45,7 +45,7 @@ else: {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, List[Any]]{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} if {{ source }} is None: {{ destination }} = None diff --git a/openapi_python_client/templates/property_templates/model_property.pyi b/openapi_python_client/templates/property_templates/model_property.pyi index e6746cb24..b1d7b5bfb 100644 --- a/openapi_python_client/templates/property_templates/model_property.pyi +++ b/openapi_python_client/templates/property_templates/model_property.pyi @@ -24,7 +24,7 @@ if _{{ property.python_name }} is not None and not isinstance(_{{ property.pytho {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[{% if property.nullable %}None, {% endif %}Unset, Dict[str, Any]]{% endif %} = UNSET -if not isinstance({{ source }}, Unset): +if not isinstance({{ source }}, Unset) and {{ source }} is not None: {% if property.nullable %} {{ destination }} = {{ source }}.to_dict() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/union_property.pyi b/openapi_python_client/templates/property_templates/union_property.pyi index 4c632c60a..d6fc86b3a 100644 --- a/openapi_python_client/templates/property_templates/union_property.pyi +++ b/openapi_python_client/templates/property_templates/union_property.pyi @@ -28,7 +28,7 @@ def _parse_{{ property.python_name }}(data: Any) -> {{ property.get_type_string( {% if not property.required %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} -if isinstance({{ source }}, Unset): +if isinstance({{ source }}, Unset) or {{ source }} is None: {{ destination }} = UNSET {% endif %} {% if property.nullable %} From 669e7bb8145fd40e8ad77bfe868f2e178003f631 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 15:00:11 -0500 Subject: [PATCH 6/7] Only use in query parameters --- .../custom_e2e/api/tests/defaults_tests_defaults_post.py | 2 +- .../golden-record-custom/custom_e2e/models/a_model.py | 2 +- .../custom_e2e/models/http_validation_error.py | 2 +- .../models/model_with_primitive_additional_properties.py | 2 +- .../custom_e2e/models/model_with_union_property.py | 6 +++--- .../api/tests/defaults_tests_defaults_post.py | 2 +- .../golden-record/my_test_api_client/models/a_model.py | 2 +- .../my_test_api_client/models/http_validation_error.py | 2 +- .../models/model_with_primitive_additional_properties.py | 2 +- .../my_test_api_client/models/model_with_union_property.py | 6 +++--- openapi_python_client/templates/endpoint_macros.pyi | 2 +- .../templates/property_templates/date_property.pyi | 4 ++-- .../templates/property_templates/datetime_property.pyi | 4 ++-- .../templates/property_templates/enum_property.pyi | 4 ++-- .../templates/property_templates/file_property.pyi | 4 ++-- .../templates/property_templates/list_property.pyi | 4 ++-- .../templates/property_templates/model_property.pyi | 4 ++-- .../templates/property_templates/union_property.pyi | 4 ++-- 18 files changed, 29 insertions(+), 29 deletions(-) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py index ecaf68a32..fde6cb49b 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py @@ -79,7 +79,7 @@ def httpx_request( json_union_prop_with_ref = UNSET elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET - if not isinstance(union_prop_with_ref, Unset) and union_prop_with_ref is not None: + if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref else: diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py index 893d78ae4..2bf3e140d 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py @@ -36,7 +36,7 @@ def to_dict(self) -> Dict[str, Any]: a_date = self.a_date.isoformat() required_not_nullable = self.required_not_nullable nested_list_of_enums: Union[Unset, List[Any]] = UNSET - if not isinstance(self.nested_list_of_enums, Unset) and self.nested_list_of_enums is not None: + if not isinstance(self.nested_list_of_enums, Unset): nested_list_of_enums = [] for nested_list_of_enums_item_data in self.nested_list_of_enums: nested_list_of_enums_item = [] diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py index d53b3b44e..1b25a6d98 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py @@ -14,7 +14,7 @@ class HTTPValidationError: def to_dict(self) -> Dict[str, Any]: detail: Union[Unset, List[Any]] = UNSET - if not isinstance(self.detail, Unset) and self.detail is not None: + if not isinstance(self.detail, Unset): detail = [] for detail_item_data in self.detail: detail_item = detail_item_data.to_dict() diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py index e4c1b1f8f..feb30878d 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py @@ -17,7 +17,7 @@ class ModelWithPrimitiveAdditionalProperties: def to_dict(self) -> Dict[str, Any]: a_date_holder: Union[Unset, Dict[str, Any]] = UNSET - if not isinstance(self.a_date_holder, Unset) and self.a_date_holder is not None: + if not isinstance(self.a_date_holder, Unset): a_date_holder = self.a_date_holder.to_dict() field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py index d3601c0a1..cbecc7c90 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py @@ -15,16 +15,16 @@ class ModelWithUnionProperty: def to_dict(self) -> Dict[str, Any]: a_property: Union[Unset, AnEnum, AnIntEnum] - if isinstance(self.a_property, Unset) or self.a_property is None: + if isinstance(self.a_property, Unset): a_property = UNSET elif isinstance(self.a_property, AnEnum): a_property = UNSET - if not isinstance(self.a_property, Unset) and self.a_property is not None: + if not isinstance(self.a_property, Unset): a_property = self.a_property else: a_property = UNSET - if not isinstance(self.a_property, Unset) and self.a_property is not None: + if not isinstance(self.a_property, Unset): a_property = self.a_property field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py index eeb690922..77ee6cce1 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py @@ -55,7 +55,7 @@ def _get_kwargs( json_union_prop_with_ref = UNSET elif isinstance(union_prop_with_ref, AnEnum): json_union_prop_with_ref = UNSET - if not isinstance(union_prop_with_ref, Unset) and union_prop_with_ref is not None: + if not isinstance(union_prop_with_ref, Unset): json_union_prop_with_ref = union_prop_with_ref else: diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py index 893d78ae4..2bf3e140d 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/a_model.py @@ -36,7 +36,7 @@ def to_dict(self) -> Dict[str, Any]: a_date = self.a_date.isoformat() required_not_nullable = self.required_not_nullable nested_list_of_enums: Union[Unset, List[Any]] = UNSET - if not isinstance(self.nested_list_of_enums, Unset) and self.nested_list_of_enums is not None: + if not isinstance(self.nested_list_of_enums, Unset): nested_list_of_enums = [] for nested_list_of_enums_item_data in self.nested_list_of_enums: nested_list_of_enums_item = [] diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py index d53b3b44e..1b25a6d98 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/http_validation_error.py @@ -14,7 +14,7 @@ class HTTPValidationError: def to_dict(self) -> Dict[str, Any]: detail: Union[Unset, List[Any]] = UNSET - if not isinstance(self.detail, Unset) and self.detail is not None: + if not isinstance(self.detail, Unset): detail = [] for detail_item_data in self.detail: detail_item = detail_item_data.to_dict() diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py index e4c1b1f8f..feb30878d 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_primitive_additional_properties.py @@ -17,7 +17,7 @@ class ModelWithPrimitiveAdditionalProperties: def to_dict(self) -> Dict[str, Any]: a_date_holder: Union[Unset, Dict[str, Any]] = UNSET - if not isinstance(self.a_date_holder, Unset) and self.a_date_holder is not None: + if not isinstance(self.a_date_holder, Unset): a_date_holder = self.a_date_holder.to_dict() field_dict: Dict[str, Any] = {} diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index d3601c0a1..cbecc7c90 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -15,16 +15,16 @@ class ModelWithUnionProperty: def to_dict(self) -> Dict[str, Any]: a_property: Union[Unset, AnEnum, AnIntEnum] - if isinstance(self.a_property, Unset) or self.a_property is None: + if isinstance(self.a_property, Unset): a_property = UNSET elif isinstance(self.a_property, AnEnum): a_property = UNSET - if not isinstance(self.a_property, Unset) and self.a_property is not None: + if not isinstance(self.a_property, Unset): a_property = self.a_property else: a_property = UNSET - if not isinstance(self.a_property, Unset) and self.a_property is not None: + if not isinstance(self.a_property, Unset): a_property = self.a_property field_dict: Dict[str, Any] = {} diff --git a/openapi_python_client/templates/endpoint_macros.pyi b/openapi_python_client/templates/endpoint_macros.pyi index c88f38e5f..3e527b017 100644 --- a/openapi_python_client/templates/endpoint_macros.pyi +++ b/openapi_python_client/templates/endpoint_macros.pyi @@ -17,7 +17,7 @@ if {{ parameter.python_name }} is not UNSET: {% set destination = "json_" + property.python_name %} {% if property.template %} {% from "property_templates/" + property.template import transform %} -{{ transform(property, property.python_name, destination) }} +{{ transform(property, property.python_name, destination, query_parameter=True) }} {% endif %} {% endfor %} params: Dict[str, Any] = { diff --git a/openapi_python_client/templates/property_templates/date_property.pyi b/openapi_python_client/templates/property_templates/date_property.pyi index 919d56eda..18fbf7735 100644 --- a/openapi_python_client/templates/property_templates/date_property.pyi +++ b/openapi_python_client/templates/property_templates/date_property.pyi @@ -9,12 +9,12 @@ if _{{ property.python_name }} is not None: {% endif %} {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if property.required %} {{ destination }} = {{ source }}.isoformat() {% if property.nullable %}if {{ source }} else None {%endif%} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, str]{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} {{ destination }} = {{ source }}.isoformat() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/datetime_property.pyi b/openapi_python_client/templates/property_templates/datetime_property.pyi index ac31672a2..073db6822 100644 --- a/openapi_python_client/templates/property_templates/datetime_property.pyi +++ b/openapi_python_client/templates/property_templates/datetime_property.pyi @@ -14,7 +14,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if property.required %} {% if property.nullable %} {{ destination }} = {{ source }}.isoformat() if {{ source }} else None @@ -23,7 +23,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, str]{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} {{ destination }} = {{ source }}.isoformat() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/enum_property.pyi b/openapi_python_client/templates/property_templates/enum_property.pyi index e4f8f52e5..a5f07a8e4 100644 --- a/openapi_python_client/templates/property_templates/enum_property.pyi +++ b/openapi_python_client/templates/property_templates/enum_property.pyi @@ -9,7 +9,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if property.required %} {% if property.nullable %} {{ destination }} = {{ source }}.value if {{ source }} else None @@ -18,7 +18,7 @@ if _{{ property.python_name }} is not None: {% endif %} {% else %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} {{ destination }} = {{ source }} if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/file_property.pyi b/openapi_python_client/templates/property_templates/file_property.pyi index 95edf2e48..3833a93ea 100644 --- a/openapi_python_client/templates/property_templates/file_property.pyi +++ b/openapi_python_client/templates/property_templates/file_property.pyi @@ -4,7 +4,7 @@ ) {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if property.required %} {% if property.nullable %} {{ destination }} = {{ source }}.to_tuple() if {{ source }} else None @@ -13,7 +13,7 @@ {% endif %} {% else %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} {{ destination }} = {{ source }}.to_tuple() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/list_property.pyi b/openapi_python_client/templates/property_templates/list_property.pyi index d6275878e..317bb19e8 100644 --- a/openapi_python_client/templates/property_templates/list_property.pyi +++ b/openapi_python_client/templates/property_templates/list_property.pyi @@ -32,7 +32,7 @@ for {{ inner_source }} in {{ source }}: {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% set inner_property = property.inner_property %} {% if property.required %} {% if property.nullable %} @@ -45,7 +45,7 @@ else: {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[Unset, List[Any]]{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} if {{ source }} is None: {{ destination }} = None diff --git a/openapi_python_client/templates/property_templates/model_property.pyi b/openapi_python_client/templates/property_templates/model_property.pyi index b1d7b5bfb..0ad78c991 100644 --- a/openapi_python_client/templates/property_templates/model_property.pyi +++ b/openapi_python_client/templates/property_templates/model_property.pyi @@ -15,7 +15,7 @@ if _{{ property.python_name }} is not None and not isinstance(_{{ property.pytho {% endif %} {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if property.required %} {% if property.nullable %} {{ destination }} = {{ source }}.to_dict() if {{ source }} else None @@ -24,7 +24,7 @@ if _{{ property.python_name }} is not None and not isinstance(_{{ property.pytho {% endif %} {% else %} {{ destination }}{% if declare_type %}: Union[{% if property.nullable %}None, {% endif %}Unset, Dict[str, Any]]{% endif %} = UNSET -if not isinstance({{ source }}, Unset) and {{ source }} is not None: +if not isinstance({{ source }}, Unset){%if query_parameter %} and {{ source }} is not None{% endif %}: {% if property.nullable %} {{ destination }} = {{ source }}.to_dict() if {{ source }} else None {% else %} diff --git a/openapi_python_client/templates/property_templates/union_property.pyi b/openapi_python_client/templates/property_templates/union_property.pyi index d6fc86b3a..fadef57e5 100644 --- a/openapi_python_client/templates/property_templates/union_property.pyi +++ b/openapi_python_client/templates/property_templates/union_property.pyi @@ -24,11 +24,11 @@ def _parse_{{ property.python_name }}(data: Any) -> {{ property.get_type_string( {{ property.python_name }} = _parse_{{ property.python_name }}({{ source }}) {% endmacro %} -{% macro transform(property, source, destination, declare_type=True) %} +{% macro transform(property, source, destination, declare_type=True, query_parameter=False) %} {% if not property.required %} {{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} -if isinstance({{ source }}, Unset) or {{ source }} is None: +if isinstance({{ source }}, Unset){%if query_parameter %} or {{ source }} is None{% endif %}: {{ destination }} = UNSET {% endif %} {% if property.nullable %} From 89978b04cfcb87f8abbdc3524750901b1ce10854 Mon Sep 17 00:00:00 2001 From: Forest Tong Date: Tue, 12 Jan 2021 17:41:16 -0500 Subject: [PATCH 7/7] Fix docstring --- openapi_python_client/parser/properties/property.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openapi_python_client/parser/properties/property.py b/openapi_python_client/parser/properties/property.py index 41570004e..a75fc481f 100644 --- a/openapi_python_client/parser/properties/property.py +++ b/openapi_python_client/parser/properties/property.py @@ -41,7 +41,7 @@ def get_type_string(self, no_optional: bool = False, query_parameter: bool = Fal Args: no_optional: Do not include Optional or Unset even if the value is optional (needed for isinstance checks) - query_arg: True if the property's type is being used for a query parameter + query_parameter: True if the property's type is being used for a query parameter """ type_string = self.get_base_type_string() if no_optional: @@ -82,7 +82,7 @@ def to_string(self, query_parameter: bool = False) -> str: How this should be declared in a dataclass Args: - query_arg: True if the property's type is being used for a query parameter + query_parameter: True if the property's type is being used for a query parameter """ default: Optional[str] if self.default is not None: