Skip to content

Cannot instantiate type "Type[None]" #19660

@wyattscarpenter

Description

@wyattscarpenter

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: #19308

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions