Skip to content

eng: fix forward type reference in Pydantic schemas BNCH-112697 #223

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

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ class Header(Parameter):
]
},
)


# Calling model_rebuild() here helps Pydantic to resolve the forward references that were used
# in defining Parameter and Encoding. Without this call, any subtle change to the loading order
# of schema submodules could result in an error like "Parameter is not fully defined".
# See: https://docs.pydantic.dev/latest/concepts/models/#rebuilding-model-schema
Parameter.model_rebuild()

Choose a reason for hiding this comment

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

Very nit: it looks like you could identify whether model rebuilding is necessary by inspecting the class's __pydantic_core_schema__. This could be used to conditionally calling Parameter.model_rebuild()

Copy link
Author

@eli-bl eli-bl Dec 2, 2024

Choose a reason for hiding this comment

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

I guess. I thought of that as a pydantic implementation detail, but I see that they did document it. However, I don't see a downside to just unconditionally calling model_rebuild, when we know for sure that there is a potential issue (due to the circular reference). This is a one-time action.