`typing` classes instantiation is considered valid by mypy
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Reproducer in mypy-instance.py und führe ihn mit mypy 0.910 aus. Vergleiche anschließend sein Ergebnis mit dem Python-Laufzeitfehler von Liststr. Verfolge, wie mypy die Instanziierung von typing.List behandelt, und sorge dafür, dass das fertige Verhalten dieses Konstrukt als nicht instanziierbar meldet, mit einem Regressionstest für das Beispiel.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100