Skip to content

Commit 08a9a22

Browse files
committed
Only erase instances when checking runtime cover
In particular, avoid erasing CallableType that represent type objects Helps with #19159
1 parent b18d3f8 commit 08a9a22

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/subtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2106,7 +2106,8 @@ def covers_at_runtime(item: Type, supertype: Type) -> bool:
21062106
supertype = get_proper_type(supertype)
21072107

21082108
# Since runtime type checks will ignore type arguments, erase the types.
2109-
supertype = erase_type(supertype)
2109+
if isinstance(supertype, Instance):
2110+
supertype = erase_type(supertype)
21102111
if is_proper_subtype(
21112112
erase_type(item), supertype, ignore_promotions=True, erase_instances=True
21122113
):

test-data/unit/check-python310.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,27 @@ def f(t: T) -> None:
26012601
reveal_type(k) # N: Revealed type is "tuple[builtins.int, fallback=__main__.K]"
26022602
[builtins fixtures/tuple.pyi]
26032603

2604+
[case testMatchTypeObjectTypeVar]
2605+
from typing import TypeVar
2606+
import b
2607+
2608+
T_Choice = TypeVar("T_Choice", bound=b.One | b.Two)
2609+
2610+
def switch(choice: type[T_Choice]) -> None:
2611+
match choice:
2612+
case b.One:
2613+
reveal_type(choice) # N: Revealed type is "def () -> b.One"
2614+
case b.Two:
2615+
reveal_type(choice) # N: Revealed type is "def () -> b.Two"
2616+
case _:
2617+
reveal_type(choice) # N: Revealed type is "type[T_Choice`-1]"
2618+
2619+
[file b.py]
2620+
class One: ...
2621+
class Two: ...
2622+
2623+
[builtins fixtures/tuple.pyi]
2624+
26042625
[case testNewRedefineMatchBasics]
26052626
# flags: --allow-redefinition-new --local-partial-types
26062627

0 commit comments

Comments
 (0)