Skip to content

Fix PydanticUndefinedAnnotation - #16

Closed
ssiddhantsharma wants to merge 1 commit into
rowansci:masterfrom
ssiddhantsharma:fix-folder-datetime-annotation
Closed

ssiddhantsharma wants to merge 1 commit into
rowansci:masterfrom
ssiddhantsharma:fix-folder-datetime-annotation

Conversation

@ssiddhantsharma

@ssiddhantsharma ssiddhantsharma commented Oct 1, 2026 •

Copy link
Copy Markdown

Problem

On rowan-python 3.2.0, get_folder() / create_folder() raise before doing anything:

PydanticUndefinedAnnotation: name 'datetime' is not defined

(Folder is not fully defined; "you should define datetime, then call Folder.model_rebuild()")

Minimal repro:

import rowan.folder
rowan.folder.Folder.model_rebuild()   # raises PydanticUndefinedAnnotation

Cause

rowan/folder.py uses from __future__ import annotations, so the field annotation
created_at: datetime | None is a lazy string that pydantic evaluates at runtime when it
builds the model. But datetime was imported only under if TYPE_CHECKING:, so the name is
undefined at runtime and model building fails.

The import ended up there because ruff's flake8-type-checking (TC) is enabled without
runtime-evaluated-base-classes, so TC003 moves annotation-only imports into the
TYPE_CHECKING block — which is incorrect for pydantic field types.

Fix

  • Move from datetime import datetime to a runtime import in folder.py.
  • Set runtime-evaluated-base-classes = ["pydantic.BaseModel"] in the ruff config so
    flake8-type-checking treats BaseModel subclasses' annotations as runtime-evaluated and does not
    reintroduce 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.

…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.
@ssiddhantsharma ssiddhantsharma changed the title Fix PydanticUndefinedAnnotation when building the Folder model Fix PydanticUndefinedAnnotation Oct 1, 2026
@eliaslmann

eliaslmann commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

hi @ssiddhantsharma, thanks for flagging this! We have a fix for this in our internal pipeline rolling out today, will close this pr.

0ed6f91

@eliaslmann eliaslmann closed this Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants