Skip to content

Commit

Permalink
[python] add circular reference import (#16940)
Browse files Browse the repository at this point in the history
* [python] add circular reference import

Signed-off-by: ふぁ <[email protected]>

* [python] update samples

Signed-off-by: ふぁ <[email protected]>

---------

Signed-off-by: ふぁ <[email protected]>
  • Loading branch information
fa0311 authored Nov 2, 2023
1 parent 7b8a515 commit 1092593
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}

{{#vendorExtensions.x-py-postponed-model-imports.size}}
{{#vendorExtensions.x-py-postponed-model-imports}}
from typing import TYPE_CHECKING
if TYPE_CHECKING:
{{{.}}}
# TODO: pydantic v2
{{{.}}}
{{/vendorExtensions.x-py-postponed-model-imports}}
# {{classname}}.model_rebuild()
# TODO: Rewrite to not use raise_errors
{{classname}}.model_rebuild(raise_errors=False)
{{/vendorExtensions.x-py-postponed-model-imports.size}}
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,6 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
{{#vendorExtensions.x-py-postponed-model-imports}}
{{{.}}}
{{/vendorExtensions.x-py-postponed-model-imports}}
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# {{classname}}.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
{{classname}}.model_rebuild(raise_errors=False)
{{/vendorExtensions.x-py-postponed-model-imports.size}}
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}

{{#vendorExtensions.x-py-postponed-model-imports.size}}
{{#vendorExtensions.x-py-postponed-model-imports}}
from typing import TYPE_CHECKING
if TYPE_CHECKING:
{{{.}}}
# TODO: pydantic v2
{{{.}}}
{{/vendorExtensions.x-py-postponed-model-imports}}
# {{classname}}.model_rebuild()
# TODO: Rewrite to not use raise_errors
{{classname}}.model_rebuild(raise_errors=False)
{{/vendorExtensions.x-py-postponed-model-imports.size}}
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ def from_dict(cls, obj: Dict) -> Union[Self, Self]:

from petstore_api.models.cat import Cat
from petstore_api.models.dog import Dog
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# Animal.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
Animal.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.first_ref import FirstRef
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# CircularReferenceModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
CircularReferenceModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.self_reference_model import SelfReferenceModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# DummyModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
DummyModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.second_ref import SecondRef
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# FirstRef.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
FirstRef.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.circular_reference_model import CircularReferenceModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# SecondRef.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
SecondRef.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.dummy_model import DummyModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# SelfReferenceModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
SelfReferenceModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ def from_dict(cls, obj: Dict) -> Union[Self, Self]:

from petstore_api.models.cat import Cat
from petstore_api.models.dog import Dog
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# Animal.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
Animal.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.first_ref import FirstRef
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# CircularReferenceModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
CircularReferenceModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.self_reference_model import SelfReferenceModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# DummyModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
DummyModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.second_ref import SecondRef
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# FirstRef.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
FirstRef.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.circular_reference_model import CircularReferenceModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# SecondRef.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
SecondRef.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ def from_dict(cls, obj: Dict) -> Self:
return _obj

from petstore_api.models.dummy_model import DummyModel
from typing import TYPE_CHECKING
if TYPE_CHECKING:
# TODO: pydantic v2
# SelfReferenceModel.model_rebuild()
pass
# TODO: Rewrite to not use raise_errors
SelfReferenceModel.model_rebuild(raise_errors=False)

Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ def test_object_with_additional_properties(self):
# should not throw the following errors:
# pydantic.errors.ConfigError: field "additional_properties" not yet prepared so type is still a ForwardRef, you might need to call ObjectToTestAdditionalProperties.update_forward_refs().

@unittest.skip("TODO: pydantic v2: fix circular dependencies between CircularReferenceModel, FirstRef, SecondRef")
def test_first_ref(self):
# shouldn't throw "still a ForwardRef" error
a = petstore_api.FirstRef.from_dict({})
Expand Down

0 comments on commit 1092593

Please sign in to comment.