Skip to content

pydantic + sqlmodel namespace hooks for lazy forward-ref resolution - #12

Merged
adsharma merged 4 commits into
mainfrom
pydantic_namespaces
Sep 18, 2026
Merged

adsharma merged 4 commits into
mainfrom
pydantic_namespaces

Conversation

@adsharma

@adsharma adsharma commented Sep 18, 2026 •

Copy link
Copy Markdown
Owner

Adds namespace= to fquery's pydantic/model and sqlmodel/model decorators (static mapping or callable) so consumers with lazily-imported models can supply forward-ref targets on demand.

Commits:

  • pydantic: namespace hook with rebuild-on-first-validator()
  • sqlmodel: same hook threaded into get_type_hints() + post-build rebuild (table creation stays eager); also fixes get_field_type() to use the resolved hint target instead of the raw ForwardRef annotation
  • version bump to 0.5 (kept at top of stack)

@adsharma adsharma changed the title pydantic namespace hook for lazy forward-ref resolution + 0.5 pydantic + sqlmodel namespace hooks for lazy forward-ref resolution + 0.5 Sep 18, 2026
Mirror fquery.pydantic's hook: sqlmodel() and model() accept
namespace= (static mapping or callable taking the decorated class),
stored as __sqlmodel_namespace__ and inherited unless overridden.
Threaded into get_type_hints() call sites and a post-build rebuild
that prefers sqlmodel_rebuild (fork) with model_rebuild fallback.
Table creation itself stays eager; only name resolution is hooked.

Also fix get_field_type() to use the resolved hint target instead of
the raw annotation, which may still be a ForwardRef for string-form
fields like Optional["Widget"].
model()/pydantic() accept namespace= (static mapping or callable
taking the decorated class). validator() rebuilds incomplete models
via a shared helper seeded from the MRO plus module globals, merged
with the hook result, with bounded retries on NameError.

Lets consumers with hundreds of lazily-imported models (e.g.
schema-org-python) supply forward-ref targets on demand instead of
importing everything up front. The hook is inherited by subclasses
unless overridden.
Appending the package dir to sys.path made fquery/ladybug.py shadow the
real ladybug package, breaking 'import ladybug' in tox (relative import
error aborted collection). pytest.importorskip now skips cleanly when
the dependency is missing.
@adsharma adsharma changed the title pydantic + sqlmodel namespace hooks for lazy forward-ref resolution + 0.5 pydantic + sqlmodel namespace hooks for lazy forward-ref resolution Sep 18, 2026
@adsharma
adsharma merged commit 35a054f into main Sep 18, 2026
2 checks passed
@adsharma
adsharma deleted the pydantic_namespaces branch September 18, 2026 19:54
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.

1 participant