[python] add circular reference import (#16940)

* [python] add circular reference import

Signed-off-by: ふぁ <yuki@yuki0311.com>

* [python] update samples

Signed-off-by: ふぁ <yuki@yuki0311.com>

---------

Signed-off-by: ふぁ <yuki@yuki0311.com>
This commit is contained in:
ふぁ 2023-11-02 23:14:20 +09:00 committed by GitHub
parent 7b8a515dcb
commit 1092593841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 32 additions and 76 deletions

View File

@ -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}}

View File

@ -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}}

View File

@ -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}}

View File

@ -104,9 +104,6 @@ class Animal(BaseModel):
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)

View File

@ -90,9 +90,6 @@ class CircularReferenceModel(BaseModel):
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)

View File

@ -90,9 +90,6 @@ class DummyModel(BaseModel):
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)

View File

@ -90,9 +90,6 @@ class FirstRef(BaseModel):
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)

View File

@ -90,9 +90,6 @@ class SecondRef(BaseModel):
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)

View File

@ -90,9 +90,6 @@ class SelfReferenceModel(BaseModel):
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)

View File

@ -112,9 +112,6 @@ class Animal(BaseModel):
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)

View File

@ -103,9 +103,6 @@ class CircularReferenceModel(BaseModel):
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)

View File

@ -103,9 +103,6 @@ class DummyModel(BaseModel):
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)

View File

@ -103,9 +103,6 @@ class FirstRef(BaseModel):
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)

View File

@ -103,9 +103,6 @@ class SecondRef(BaseModel):
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)

View File

@ -103,9 +103,6 @@ class SelfReferenceModel(BaseModel):
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)

View File

@ -560,7 +560,6 @@ class ModelTests(unittest.TestCase):
# 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({})