Skip to content

Commit a0f27b1

Browse files
Fix crash on ErasedType and covers_at_runtime (#11924)
Closes #11913 Refs #11273 Co-authored-by: Shantanu <[email protected]>
1 parent 55bee20 commit a0f27b1

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

mypy/erasetype.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ def visit_uninhabited_type(self, t: UninhabitedType) -> ProperType:
4141
return t
4242

4343
def visit_erased_type(self, t: ErasedType) -> ProperType:
44-
# Should not get here.
45-
raise RuntimeError()
44+
return t
4645

4746
def visit_partial_type(self, t: PartialType) -> ProperType:
4847
# Should not get here.

mypy/subtypes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ def restrict_subtype_away(t: Type, s: Type, *, ignore_promotions: bool = False)
11671167
def covers_at_runtime(item: Type, supertype: Type, ignore_promotions: bool) -> bool:
11681168
"""Will isinstance(item, supertype) always return True at runtime?"""
11691169
item = get_proper_type(item)
1170+
supertype = get_proper_type(supertype)
11701171

11711172
# Since runtime type checks will ignore type arguments, erase the types.
11721173
supertype = erase_type(supertype)

test-data/unit/check-inference.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3211,6 +3211,21 @@ def test(seq: List[Union[Iterable, Any]]) -> None:
32113211
reveal_type(k) # N: Revealed type is "builtins.list[Any]"
32123212
[builtins fixtures/list.pyi]
32133213

3214+
[case testErasedTypeRuntimeCoverage]
3215+
# https://github.com/python/mypy/issues/11913
3216+
from typing import TypeVar, Type, Generic, Callable, Iterable
3217+
3218+
class DataType: ...
3219+
3220+
T1 = TypeVar('T1')
3221+
T2 = TypeVar("T2", bound=DataType)
3222+
3223+
def map(__func: T1) -> None: ...
3224+
3225+
def collection_from_dict_value(model: Type[T2]) -> None:
3226+
map(lambda i: i if isinstance(i, model) else i)
3227+
[builtins fixtures/isinstancelist.pyi]
3228+
32143229
[case testRegression11705_Strict]
32153230
# flags: --strict-optional
32163231
# See: https://github.com/python/mypy/issues/11705

0 commit comments

Comments
 (0)