Skip to content

Fix PEP 563 string annotations in JSON schema generation#1670

Merged
bendichter merged 4 commits intomainfrom
fix-pep563-annotation-resolution
Mar 3, 2026
Merged

Fix PEP 563 string annotations in JSON schema generation#1670
bendichter merged 4 commits intomainfrom
fix-pep563-annotation-resolution

Conversation

@bendichter
Copy link
Contributor

Summary

  • When a module uses from __future__ import annotations (PEP 563), all type annotations are stored as strings at runtime
  • get_json_schema_from_method_signature() passes these string annotations to pydantic.create_model(), which fails because pydantic cannot resolve types like DirectoryPath from their string form
  • Uses typing.get_type_hints() to resolve string annotations back to their actual types before passing to pydantic
  • When a class is passed (rather than a method), targets __init__ for hint resolution since inspect.signature also uses __init__ in that case

Currently affects MiniscopeConverter, which is the only interface using from __future__ import annotations.

Test plan

  • MiniscopeConverter.get_source_schema() succeeds instead of raising PydanticUserError
  • All other interface schemas still generate correctly
  • Existing tests pass

🤖 Generated with Claude Code

When a module uses `from __future__ import annotations` (PEP 563),
all type annotations are stored as strings at runtime. This causes
pydantic to fail when creating models from method signatures, as it
cannot resolve types like `DirectoryPath` from their string form.

Use `typing.get_type_hints()` to resolve string annotations back to
their actual types before passing them to pydantic. Only apply this
resolution when the annotation is a string, preserving pydantic-specific
types like `DirectoryPath` and `FilePath` for non-PEP-563 modules.

When a class is passed (rather than a method), target `__init__` for
hint resolution since `inspect.signature` also uses `__init__`.

Fixes MiniscopeConverter schema generation failure.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bendichter bendichter force-pushed the fix-pep563-annotation-resolution branch from daedb0c to 444de5d Compare February 20, 2026 23:49
Copy link
Collaborator

@h-mayorquin h-mayorquin left a comment

Choose a reason for hiding this comment

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

LGTM.

I added a test and the changelog.

@bendichter bendichter merged commit 462e704 into main Mar 3, 2026
23 of 24 checks passed
@bendichter bendichter deleted the fix-pep563-annotation-resolution branch March 3, 2026 13:03
@codecov
Copy link

codecov bot commented Mar 3, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.23%. Comparing base (c7f0869) to head (a4b547b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/neuroconv/utils/json_schema.py 80.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1670      +/-   ##
==========================================
- Coverage   80.23%   80.23%   -0.01%     
==========================================
  Files         160      160              
  Lines       12663    12672       +9     
==========================================
+ Hits        10160    10167       +7     
- Misses       2503     2505       +2     
Flag Coverage Δ
unittests 80.23% <80.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/neuroconv/utils/json_schema.py 95.37% <80.00%> (-0.97%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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