Repository navigation
Fix PydanticUndefinedAnnotation - #16
Closed
ssiddhantsharma wants to merge 1 commit into
Closed
ssiddhantsharma wants to merge 1 commit into
ssiddhantsharma wants to merge 1 commit into
Conversation
…ntime
folder.py uses `from __future__ import annotations`, so the `created_at: datetime | None`
field annotation is a lazy string that pydantic resolves at runtime when the model is built.
The datetime import was under `if TYPE_CHECKING:`, so at runtime the name is undefined and
get_folder()/create_folder() raise PydanticUndefinedAnnotation ('Folder is not fully defined;
you should define datetime, then call Folder.model_rebuild()').
Move the datetime import to runtime, and set ruff's runtime-evaluated-base-classes to
pydantic.BaseModel so flake8-type-checking does not push field-type imports back into a
TYPE_CHECKING block (which would reintroduce the bug) for any model.
Contributor
|
hi @ssiddhantsharma, thanks for flagging this! We have a fix for this in our internal pipeline rolling out today, will close this pr. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On
rowan-python3.2.0,get_folder()/create_folder()raise before doing anything:(
Folderis not fully defined; "you should definedatetime, then callFolder.model_rebuild()")Minimal repro:
Cause
rowan/folder.pyusesfrom __future__ import annotations, so the field annotationcreated_at: datetime | Noneis a lazy string that pydantic evaluates at runtime when itbuilds the model. But
datetimewas imported only underif TYPE_CHECKING:, so the name isundefined at runtime and model building fails.
The import ended up there because ruff's flake8-type-checking (
TC) is enabled withoutruntime-evaluated-base-classes, soTC003moves annotation-only imports into theTYPE_CHECKINGblock — which is incorrect for pydantic field types.Fix
from datetime import datetimeto a runtime import infolder.py.runtime-evaluated-base-classes = ["pydantic.BaseModel"]in the ruff config soflake8-type-checking treats
BaseModelsubclasses' annotations as runtime-evaluated and does notreintroduce this for any model.
Verification
import rowan.folder; rowan.folder.Folder.model_rebuild(); rowan.folder.Folder(name="t", uuid="u")now succeeds.
ruff check rowan/passes.