Closed
Description
MyPy does not show an error for the following code:
import typing
def foo(s: set[int]) -> int:
return len(s)
def goo(l: list[int]) -> int:
return len(l)
However, this code is not actually valid: it throws the following error at runtime:
Traceback (most recent call last):
File "error.py", line 3, in <module>
def foo(s: set[int]) -> int:
TypeError: 'type' object is not subscriptable
The first step to fixing this code is to change set
to Set
and list
to List
. This, however, leads to another runtime error:
Traceback (most recent call last):
File "error.py", line 3, in <module>
def foo(s: Set[int]) -> int:
NameError: name 'Set' is not defined
Finally, we need to add the following imports:
from typing import Set, List
and the program finally runs without an error.
So there are two issues here:
- mypy should catch when a "not subscriptable" thing (whatever that means) is written instead of an actual type
- mypy should detect when types are used without being imported