Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests
  • Loading branch information
forest-benchling committed Feb 9, 2021
commit 9ec739493631784560ba470cd6b371a933df5b12
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ def _parse_a_property(data: Union[Unset, str, int]) -> Union[Unset, AnEnum, AnIn
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
if not isinstance(data, int):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if _a_property is not None and _a_property is not UNSET: # type: ignore
a_property = AnEnum(_a_property)

return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
if not isinstance(data, int):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if _a_property is not None and _a_property is not UNSET: # type: ignore
a_property = AnIntEnum(_a_property)

return a_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ def _parse_a_property(data: Union[Unset, str, int]) -> Union[Unset, AnEnum, AnIn
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
if not isinstance(data, int):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if _a_property is not None and _a_property is not UNSET: # type: ignore
a_property = AnEnum(_a_property)

return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
if not isinstance(data, int):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if _a_property is not None and _a_property is not UNSET: # type: ignore
a_property = AnIntEnum(_a_property)

return a_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if _{{ property.python_name }} is not None:
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(source) %}isinstance({{ source }}, str){% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% if property.required %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if _{{ property.python_name }} is not None:
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(source) %}isinstance({{ source }}, str){% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% if property.required %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required %}
{% if property.required and not property.nullable %}
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
if _{{ property.python_name }} is not None and _{{ property.python_name }} is not UNSET:
if _{{ property.python_name }} is not None and _{{ property.python_name }} is not UNSET: # type: ignore
{{ property.python_name }} = {{ property.reference.class_name }}(_{{ property.python_name }})
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(source) %}(isinstance({{ source }}, int) or isinstance({{ source }}, str)){% endmacro %}
{% macro check_type_for_construct(property, source) %}{% if property.value_type == str %}isinstance({{ source }}, str){% else %}isinstance({{ source }}, int){% endif %}{% endmacro %}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
{% macro check_type_for_construct(property, source) %}{% if property.value_type == str %}isinstance({{ source }}, str){% else %}isinstance({{ source }}, int){% endif %}{% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, {{ property.value_type }}){% endmacro %}


{% macro transform(property, source, destination, declare_type=True) %}
{% if property.required %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
)
{% endmacro %}

{% macro check_type_for_construct(source) %}isinstance({{ source }}, bytes){% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, bytes){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% if property.required %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ for {{ inner_source }} in {{ source }}:
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(source) %}isinstance({{ source }}, list){% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% set inner_property = property.inner_property %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if {% if property.nullable %}_{{ property.python_name }} is not None{% endif %}{
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(source) %}isinstance({{ source }}, dict){% endmacro %}
{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably need a better name for this macro than "transform" at some point 😅even I can't remember what this one is for sometimes. Not a note on this PR, just a note on how I need to put some design thought into the templates.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Haha, yeah...maybe serialize and deserialize?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

or to_json and from_json...

{% if property.required %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{{ property.python_name }} = {{ initial_value }}
{% endmacro %}

{% macro check_type_for_construct(source) %}{{ source }} is None{% endmacro %}
{% macro check_type_for_construct(property, source) %}{{ source }} is None{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{{ destination }} = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def _parse_{{ property.python_name }}(data: {{ property.get_type_string(json=Tru
{% if not loop.last or property.has_properties_without_templates %}
try:
{% from "property_templates/" + inner_property.template import construct, check_type_for_construct %}
if not {{ check_type_for_construct("data") }}:
if not {{ check_type_for_construct(inner_property, "data") }}:
Copy link
Collaborator

Choose a reason for hiding this comment

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

In theory we could start doing:

if {{ check_type_for_construct(inner_property, "data") }}:
    {{ construct(inner_property, "data", initial_value="UNSET") }}
    return {{ inner_property.python_name }}

and avoid the try/except stuff, yeah?

Copy link
Collaborator Author

@forest-benchling forest-benchling Mar 3, 2021

Choose a reason for hiding this comment

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

I don't think it would work out-of-the-box, because we're also catching non-type errors, like parsing dates (isoparse("foo")). Agreed that ideally I think it would be more robust for us eventually move when possible away from the try/except (especially with the generic exception catching).

raise TypeError()
{{ construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
return {{ property.python_name }}
except: # noqa: E722
pass
{% else %}{# Don't do try/except for the last one #}
{% from "property_templates/" + inner_property.template import construct, check_type_for_construct %}
if not {{ check_type_for_construct("data") }}:
if not {{ check_type_for_construct(inner_property, "data") }}:
raise TypeError()
{{ construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
return {{ property.python_name }}
Expand All @@ -36,7 +36,7 @@ def _parse_{{ property.python_name }}(data: {{ property.get_type_string(json=Tru
{% endmacro %}

{# For now we assume there will be no unions of unions #}
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the future we should probably be unrolling unions while building the properties. I don't imagine that would be too difficult, just a check on each inner_property to see if it's a UnionProperty.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah, yeah, that makes sense.

{% macro check_type_for_construct(source) %}True{% endmacro %}
{% macro check_type_for_construct(property, source) %}True{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% if not property.required or property.nullable %}
Expand Down