python / python/mypy

Cannot instantiate type "Type[None]"

Offen
#19,660 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug good-first-issue
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

Mypy claims Cannot instantiate type "Type[None]" when you try to do so. However, you actually can do this in python, and perhaps have been able to do this ever since python 3...

This affects both types.NoneType and type(None). Also, function signatures that get at those.

To Reproduce

https://mypy-play.net/?mypy=master&python=3.12&gist=26b0ab20fc6d45de0edff4f8a374b0a7

from types import NoneType
type(None)()
NoneType()
def f(n: type[None]):
    n()
def g(n: type[NoneType]):
    n()
f(NoneType)
g(NoneType)

Actual Behavior
main.py:2: error: Cannot instantiate type "type[None]" [misc]
main.py:3: error: Cannot instantiate type "type[None]" [misc]
main.py:5: error: Cannot instantiate type "type[None]" [misc]
main.py:6: error: NoneType should not be used as a type, please use None instead [valid-type]
main.py:7: error: Cannot instantiate type "type[None]" [misc]
Found 5 errors in 1 file (checked 1 source file)

Expected Behavior
Running this python file doesn't result in any runtime problems, so I would expect no errors. (Except perhaps main.py:6: error: NoneType should not be used as a type, please use None instead [valid-type], which seems like more of a style suggstion, and so might still be valid.)

Possibly related, perhaps even fixable by this issue: https://github.com/python/mypy/issues/19308

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die verlinkte mypy-play-Reproduktion aus main.py auszuführen und nachzuverfolgen, wo die fünf Diagnosen erzeugt werden. Fertig ist die Aufgabe, wenn die gültigen Instanziierungsfälle von NoneType und type(None) keine falschen Fehler mehr erzeugen, während die akzeptable NoneType-Typverwendungsdiagnose bestehen bleiben darf.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.