Skip to content

Allow instantiating type[None] / types.NoneType - #21800

Open
mmaxjr wants to merge 1 commit into
python:masterfrom
mmaxjr:fix-instantiate-nonetype
Open

Allow instantiating type[None] / types.NoneType#21800
mmaxjr wants to merge 1 commit into
python:masterfrom
mmaxjr:fix-instantiate-nonetype

Conversation

@mmaxjr

@mmaxjr mmaxjr commented Aug 2, 2026

Copy link
Copy Markdown

Summary

analyze_type_type_callee() in checkexpr.py had no branch for mypy's internal NoneType. As a result, calling a callee of type type[None] incorrectly produced Cannot instantiate type "type[None]", even though this is valid at runtime (type(None)() and NoneType() both return None).

Repro from the issue:

from types import NoneType

type(None)()
NoneType()

def f(n: type[None]):
    n()

def g(n: type[NoneType]):
    n()

f(NoneType)
g(NoneType)

Before this change, mypy reported 4 spurious Cannot instantiate type "type[None]" errors (plus the expected NoneType should not be used as a type note). After this change, only the expected note remains.

Fix: add a case in analyze_type_type_callee that, for NoneType items, returns a nullary CallableType with return type NoneType(), mirroring how type(None) behaves at runtime.

Test plan

  • Added testTypeCanInstantiateNoneType to test-data/unit/check-classes.test
  • pytest mypy/test/testcheck.py -k "check-classes or check-python310" -> 810 passed
  • pytest mypy/test/testcheck.py (full suite) -> 8175 passed
  • mypy --config-file mypy_self_check.ini mypy/checkexpr.py -> no issues
  • pre-commit run on changed files -> passed

Fixes #19660

analyze_type_type_callee() had no case for mypy's internal NoneType,
so calling a value of type type[None] (e.g. via type(None)() or a
type[None]/type[types.NoneType] annotated callee) incorrectly
produced a "Cannot instantiate type" error, even though this is valid
at runtime and returns None.

Fixes python#19660
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot instantiate type "Type[None]"

1 participant