Skip to content

Resolve parametrized type aliases before checking - #587

Open
afonsojanu wants to merge 1 commit into
agronholm:masterfrom
afonsojanu:fix/parametrized-type-alias-skips-check
Open

Resolve parametrized type aliases before checking#587
afonsojanu wants to merge 1 commit into
agronholm:masterfrom
afonsojanu:fix/parametrized-type-alias-skips-check

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #579.

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 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 subscripting list[T] with 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 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.

)

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.
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.873% (+0.02%) from 94.853% — afonsojanu:fix/parametrized-type-alias-skips-check into agronholm:master

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.

Subscripted PEP 695 generic type alias skips the check entirely, so any value passes

2 participants