Skip to content
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

Support for Union with ForwardRef #98

Closed
Czaki opened this issue Mar 22, 2022 · 5 comments
Closed

Support for Union with ForwardRef #98

Czaki opened this issue Mar 22, 2022 · 5 comments
Assignees
Labels
bug Something isn't working

Comments

@Czaki
Copy link

Czaki commented Mar 22, 2022

for code:

Node = ForwardRef('Node')

class Node(BaseModel):
    left: Union[Node, Leaf]
    op: str
    right: Union[Node, Leaf]

there was warning /home/czaki/Projekty/PartSeg/docs/autodoc_pydantic.rst: WARNING: JSON schema can't be generated for 'PartSegCore.analysis.measurement_base.Node' because the following pydantic fields can't be serialized properly: ['left', 'right'].

in this code

try:
field_args = (field.type_, field.default)
model = create_model("_", test_field=field_args, Config=Cfg)
model.schema()
return True
except Exception:
return False

there is no check for sub_fields:
https://github.com/samuelcolvin/pydantic/blob/fbf8002cefd1ba7b263f63545fff2e48a378189d/pydantic/typing.py#L445-L458

in my case field.type_ is typing.Union[ForwardRef('Node'), PartSegCore.analysis.measurement_base.Leaf] but field.sub_fields is [ModelField(name="left_ForwardRef('Node')", type=Node, required=True), ModelField(name='left_Leaf', type=Leaf, required=True)]

So all data are available in data structures.

@mansenfranzen mansenfranzen self-assigned this Apr 1, 2022
@mansenfranzen mansenfranzen added the bug Something isn't working label Apr 1, 2022
@mansenfranzen
Copy link
Owner

@Czaki Thanks for sharing this issue. It is definitely a bug. I can reproduce the behavior and it boils down to recursively check sub_fields as you have pointed it out. It will be fixed in the upcoming release.

@mansenfranzen
Copy link
Owner

@Czaki This should be fixed via #102.

Before merging the related PR, it would be great if you could test the bug fix on your site to confirm that my test case did indeed capture the incorrect behavior which you've encountered. To do so, please install the current dev release in your doc-building-environment via pip install git+https://github.com/mansenfranzen/[email protected] and rebuild your docs.

@mansenfranzen
Copy link
Owner

@all-contributors please add @Czaki for bug

@allcontributors
Copy link
Contributor

@mansenfranzen

I've put up a pull request to add @Czaki! 🎉

@mansenfranzen
Copy link
Owner

@Czaki Please feel free to reopen this ticket if the issue persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants