`typing` classes instantiation is considered valid by mypy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy fails to detect attempts to instantiate typing.List, which triggers runtime error
To Reproduce
Considering following example:
from typing import List
if __name__ == "__main__":
x = List[str]([])
print(x)
Mypy reports this code as valid, although it is guaranteed to fail at runtime:
(py_39) % python3 -mmypy mypy-instance.py
Success: no issues found in 1 source file
(py_39) % python3 mypy-instance.py
Traceback (most recent call last):
File "/Users/nshulga/test/mypy-instance.py", line 3, in <module>
x = List[str]([])
File "/Users/nshulga/miniconda3/envs/py_39/lib/python3.9/typing.py", line 668, in __call__
raise TypeError(f"Type {self._name} cannot be instantiated; "
TypeError: Type List cannot be instantiated; use list() instead
Expected Behavior
Mypy shoudl report abovementioned construct as abstract class/non-instantiatiable type
Your Environment
- Mypy version used: 0.910
- Python version used: 3.9.4
- Operating system and version: MacOs 12.0.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproducer in mypy-instance.py and run it with mypy 0.910, then compare its result with the Python runtime error from Liststr. Trace how mypy handles instantiation of typing.List and make the completed behavior report this construct as non-instantiable, with a regression test for the example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100