Skip to content

Keep unresolvable deferred annotations in inferred signatures on Python 3.14 - #154

Merged
LuKrO2011 merged 1 commit into
se2p:mainfrom
ZacKienzle2:fix/py314-deferred-annotations
Oct 28, 2026
Merged

LuKrO2011 merged 1 commit into
se2p:mainfrom
ZacKienzle2:fix/py314-deferred-annotations

Conversation

@ZacKienzle2

Copy link
Copy Markdown
Contributor

Fixes #153.

On Python 3.14, inspect.signature evaluates deferred annotations (PEP 649, PEP 749) in annotationlib.Format.VALUE. A callable whose annotation names a type imported only under TYPE_CHECKING then raises NameError. The standard library's _colorize does this with IO, so a run whose module imports anything that reaches traceback stopped in TypeSystem.infer_signature without writing tests.

infer_signature now passes annotation_format=annotationlib.Format.FORWARDREF on 3.14 and later. The unresolvable name stays a ForwardRef, the parameters keep their names and kinds, and the type hint provider treats the annotation as unknown, as type_hints_provider already does when typing.get_type_hints raises NameError. Earlier versions are unchanged: there inspect.signature does not evaluate annotations.

Tests

test_infer_type_info_annotation_imported_for_type_checking (skipped before 3.14) infers the signature of a fixture function annotated with a TYPE_CHECKING-only import and checks that it keeps its parameter, typed Any.

On Python 3.14 with poetry install --with dev,test: the pre-commit hooks pass (ruff, ruff-format, reuse lint and the rest), poetry run mypy reports no issues in 229 source files, and the new test passes. tests/analyses/test_typesystem.py has 2 to 3 failures in test_find_by_symbols with or without this change; on unchanged main the same file gave 6, so those are independent of it.

On Python 3.14 inspect.signature evaluates deferred annotations (PEP 649) in the VALUE format, which raises NameError for a type imported only under TYPE_CHECKING, such as IO in the standard library _colorize. infer_signature now asks for the FORWARDREF format there, so the parameters keep their names and the type hint provider treats the annotation as unknown, as it already does for NameError.

Fixes se2p#153.
@LuKrO2011
LuKrO2011 merged commit 0864041 into se2p:main Oct 28, 2026
1 check passed
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.

Python 3.14: inspect.signature in infer_signature raises NameError on deferred annotations

2 participants