-
-
Notifications
You must be signed in to change notification settings - Fork 228
Fixes to union properties #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0e0525b
eb45e92
072f8da
7223731
25f758c
15f1284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Any, Dict, Union | ||
|
||
import attr | ||
|
||
from ..models.an_enum import AnEnum | ||
from ..models.an_int_enum import AnIntEnum | ||
from ..types import UNSET, Unset | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class ModelWithUnionProperty: | ||
""" """ | ||
|
||
a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
a_property: Union[Unset, AnEnum, AnIntEnum] | ||
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): | ||
a_property = self.a_property | ||
|
||
else: | ||
a_property = UNSET | ||
if not isinstance(self.a_property, Unset): | ||
a_property = self.a_property | ||
|
||
field_dict = {} | ||
if a_property is not UNSET: | ||
field_dict["a_property"] = a_property | ||
|
||
return field_dict | ||
|
||
@staticmethod | ||
def from_dict(d: Dict[str, Any]) -> "ModelWithUnionProperty": | ||
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: | ||
data = None if isinstance(data, Unset) else data | ||
a_property: Union[Unset, AnEnum, AnIntEnum] | ||
try: | ||
a_property = UNSET | ||
if data is not None: | ||
a_property = AnEnum(data) | ||
|
||
return a_property | ||
except: # noqa: E722 | ||
pass | ||
a_property = UNSET | ||
if data is not None: | ||
a_property = AnIntEnum(data) | ||
|
||
return a_property | ||
|
||
a_property = _parse_a_property(d.get("a_property", UNSET)) | ||
|
||
return ModelWithUnionProperty( | ||
a_property=a_property, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Any, Dict, Union | ||
|
||
import attr | ||
|
||
from ..models.an_enum import AnEnum | ||
from ..models.an_int_enum import AnIntEnum | ||
from ..types import UNSET, Unset | ||
|
||
|
||
@attr.s(auto_attribs=True) | ||
class ModelWithUnionProperty: | ||
""" """ | ||
|
||
a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
a_property: Union[Unset, AnEnum, AnIntEnum] | ||
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): | ||
a_property = self.a_property | ||
|
||
else: | ||
a_property = UNSET | ||
if not isinstance(self.a_property, Unset): | ||
a_property = self.a_property | ||
|
||
field_dict = {} | ||
if a_property is not UNSET: | ||
field_dict["a_property"] = a_property | ||
|
||
return field_dict | ||
|
||
@staticmethod | ||
def from_dict(d: Dict[str, Any]) -> "ModelWithUnionProperty": | ||
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: | ||
data = None if isinstance(data, Unset) else data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this was to be able to reuse Most of the I'll look a bit more to see if there is a better way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's not an easy fix it's not a huge deal. If you want to just regen the custom template e2e test so the checks pass I'll merge this. |
||
a_property: Union[Unset, AnEnum, AnIntEnum] | ||
try: | ||
a_property = UNSET | ||
if data is not None: | ||
a_property = AnEnum(data) | ||
|
||
return a_property | ||
except: # noqa: E722 | ||
pass | ||
a_property = UNSET | ||
if data is not None: | ||
a_property = AnIntEnum(data) | ||
|
||
return a_property | ||
|
||
a_property = _parse_a_property(d.get("a_property", UNSET)) | ||
|
||
return ModelWithUnionProperty( | ||
a_property=a_property, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
{% macro construct(property, source) %} | ||
{% macro construct(property, source, initial_value="None") %} | ||
{% if property.required %} | ||
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }}) | ||
{% else %} | ||
{{ property.python_name }} = None | ||
{{ property.python_name }} = {{ initial_value }} | ||
if {{ source }} is not None: | ||
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }}) | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
{% macro transform(property, source, destination) %} | ||
{% macro transform(property, source, destination, declare_type=True) %} | ||
{% if property.required %} | ||
{% if property.nullable %} | ||
{{ destination }} = {{ source }}.value if {{ source }} else None | ||
{% else %} | ||
{{ destination }} = {{ source }}.value | ||
{% endif %} | ||
{% else %} | ||
{{ destination }}: {{ property.get_type_string() }} = UNSET | ||
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET | ||
if not isinstance({{ source }}, Unset): | ||
{% if property.nullable %} | ||
{{ destination }} = {{ source }}.value if {{ source }} else None | ||
{{ destination }} = {{ source }} if {{ source }} else None | ||
{% else %} | ||
{{ destination }} = {{ source }}.value | ||
{{ destination }} = {{ source }} | ||
{% endif %} | ||
Comment on lines
+22
to
25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a typing issue. Mypy complained when I generated the
There was a type error, saying I was providing a FWIW, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I forgot that we inherit from |
||
{% endif %} | ||
{% endmacro %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird, I'll try to find the place in the template that causes it. Obviously no need to double check
isinstance
on the same property, we already know it'sAnEnum
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where your changes would have caused this, so it was probably an existing issue with Unions/UNSET (I know there is at least one issue related to that). The inner_property is probably being set as not required which makes the inner constructor redo the check. I can handle that in a future version.