Skip to content

Commit aa82bd5

Browse files
authored
Remove an assert triggered by UnboundType leaking from semanal.py (#5258)
Fixes #4543 This is also a temporary solution for #4987. A permanent one would be to turn UnboundTypes into Anys directly in semanal.py.
1 parent bd9e93b commit aa82bd5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

mypy/erasetype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def erase_type(typ: Type) -> Type:
2727
class EraseTypeVisitor(TypeVisitor[Type]):
2828

2929
def visit_unbound_type(self, t: UnboundType) -> Type:
30-
assert False, 'Not supported'
30+
# TODO: replace with an assert after UnboundType can't leak from semantic analysis.
31+
return AnyType(TypeOfAny.from_error)
3132

3233
def visit_any(self, t: AnyType) -> Type:
3334
return t

test-data/unit/check-basic.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,35 @@ import c
332332
import e
333333
[file e.py]
334334
1+'no' # E: Unsupported operand types for + ("int" and "str")
335+
336+
[case testModuleAsTypeNoCrash]
337+
import mock
338+
from typing import Union
339+
340+
class A: ...
341+
class B: ...
342+
343+
x: Union[mock, A] # E: Invalid type "mock"
344+
345+
if isinstance(x, B):
346+
pass
347+
[file mock.py]
348+
[builtins fixtures/isinstance.pyi]
349+
[out]
350+
351+
[case testModuleAsTypeNoCrash2]
352+
import mock
353+
from typing import overload, Any, Union
354+
355+
@overload
356+
def f(x: int) -> int: ...
357+
@overload
358+
def f(x: str) -> Union[mock, str]: ... # E: Invalid type "mock"
359+
def f(x):
360+
pass
361+
362+
x: Any
363+
f(x)
364+
[file mock.py]
365+
[builtins fixtures/isinstance.pyi]
366+
[out]

0 commit comments

Comments
 (0)