Resolve parametrized type aliases before checking - #587
Open
afonsojanu wants to merge 1 commit into
Open
Conversation
) A subscripted PEP 695 alias like Boxed[int], for type Boxed[T] = list[T], has Boxed as its origin rather than being a TypeAliasType itself, so the existing type(annotation) in type_alias_types check missed it entirely. The annotation just fell through unresolved and nothing checked the value against it, so any value passed silently, which is worse than a crash since nothing signals that the check never ran. Added resolve_type_alias(), which handles both the bare alias case that already worked and the parametrized one, substituting the alias's type arguments into its __value__ the same way subscripting list[T] with an int would. Used it at both call sites that were special-casing bare aliases before (check_class and check_type_internal). Extended dummymodule_py312.py with a function using a parametrized alias and added matching success/failure tests to test_instrumentation.py's TestTypeAlias class, covering both the typechecked and importhook instrumentation paths. Confirmed the new failure test fails against the unpatched code (with a confusing, unrelated error rather than a clean rejection) and passes with the fix. Full suite otherwise unaffected: same 9 pre-existing environment-only failures before and after, everything else green.
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 #579.
A subscripted PEP 695 alias like
Boxed[int], fortype Boxed[T] = list[T], hasBoxedas its origin rather than being aTypeAliasTypeitself, so the existingtype(annotation) in type_alias_typescheck missed it entirely. The annotation fell through unresolved and nothing checked the value against it, so any value passed silently. That's a worse failure mode than a crash, since nothing tells you the check never ran.Added
resolve_type_alias(), which handles both the bare alias case that already worked and the parametrized one, substituting the alias's type arguments into its__value__the same way subscriptinglist[T]withintwould. Used it at both call sites that were special-casing bare aliases before,check_classandcheck_type_internal.Extended
dummymodule_py312.pywith a function using a parametrized alias and added matching success/failure tests totest_instrumentation.py'sTestTypeAliasclass, covering both thetypecheckedandimporthookinstrumentation paths. Confirmed the new failure test fails against the unpatched code (with a confusing, unrelated error about the return value rather than a clean rejection of the argument) and passes with the fix.Ran the full suite before and after: same 9 pre-existing failures both times, all environment-only (subprocess tests that need the package actually installed, an unregistered pytest-plugin option, a local mypy version mismatch) and unrelated to this change. Everything else green, 530 passing.