Repository navigation
Keep unresolvable deferred annotations in inferred signatures on Python 3.14 - #154
Merged
Merged
Conversation
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.
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.
Fixes #153.
On Python 3.14,
inspect.signatureevaluates deferred annotations (PEP 649, PEP 749) inannotationlib.Format.VALUE. A callable whose annotation names a type imported only underTYPE_CHECKINGthen raisesNameError. The standard library's_colorizedoes this withIO, so a run whose module imports anything that reachestracebackstopped inTypeSystem.infer_signaturewithout writing tests.infer_signaturenow passesannotation_format=annotationlib.Format.FORWARDREFon 3.14 and later. The unresolvable name stays aForwardRef, the parameters keep their names and kinds, and the type hint provider treats the annotation as unknown, astype_hints_provideralready does whentyping.get_type_hintsraisesNameError. Earlier versions are unchanged: thereinspect.signaturedoes 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 aTYPE_CHECKING-only import and checks that it keeps its parameter, typedAny.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 mypyreports no issues in 229 source files, and the new test passes.tests/analyses/test_typesystem.pyhas 2 to 3 failures intest_find_by_symbolswith or without this change; on unchangedmainthe same file gave 6, so those are independent of it.