Skip to content

Avoid erasing type objects when checking runtime cover #19320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,8 @@ def covers_at_runtime(item: Type, supertype: Type) -> bool:
supertype = get_proper_type(supertype)

# Since runtime type checks will ignore type arguments, erase the types.
supertype = erase_type(supertype)
if not (isinstance(supertype, CallableType) and supertype.is_type_obj()):
supertype = erase_type(supertype)
if is_proper_subtype(
erase_type(item), supertype, ignore_promotions=True, erase_instances=True
):
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,28 @@ def f(t: T) -> None:
reveal_type(k) # N: Revealed type is "tuple[builtins.int, fallback=__main__.K]"
[builtins fixtures/tuple.pyi]

[case testMatchTypeObjectTypeVar]
# flags: --warn-unreachable
from typing import TypeVar
import b

T_Choice = TypeVar("T_Choice", bound=b.One | b.Two)

def switch(choice: type[T_Choice]) -> None:
match choice:
case b.One:
reveal_type(choice) # N: Revealed type is "def () -> b.One"
case b.Two:
reveal_type(choice) # N: Revealed type is "def () -> b.Two"
case _:
reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]"

[file b.py]
class One: ...
class Two: ...

[builtins fixtures/tuple.pyi]

[case testNewRedefineMatchBasics]
# flags: --allow-redefinition-new --local-partial-types

Expand Down