Skip to content

fix: Allow instantiation of type[None] in analyze_type_type_callee - #19782

Merged
hauntsaninja merged 7 commits into
python:masterfrom
codenamenam:fix/19660
Oct 21, 2026
Merged

hauntsaninja merged 7 commits into
python:masterfrom
codenamenam:fix/19660

Conversation

@codenamenam

@codenamenam codenamenam commented Sep 2, 2025 •

Copy link
Copy Markdown
Contributor

(Explain how this PR changes mypy.)

Fixes #19660

Allow instantiation of NoneType in type checker

This change fixes the error "Cannot instantiate type 'Type[None]'"
when calling NoneType() or type(None)().

By treating NoneType as a callable that returns None, mypy can now correctly
handle such calls without raising spurious errors.

Also, I added test case testTypeUsingTypeCNoneType covering:

  • direct calls to type(None)() and NoneType()
  • functions accepting type[None] and type[NoneType] parameters and invoking them

This ensures proper handling of NoneType instantiation and prevents spurious errors.

@github-actions

This comment has been minimized.

@sterliakov

Copy link
Copy Markdown
Collaborator

Note that this does not fix #19308 (but fortunately the magic comment only affects the first issue, so that's just a plain reference):

def get_class(x: int) -> type[str | None]:
    if x > 0:
        return str
    else:
        return type(None)

                                                                                                     
x = get_class(1)
if x is type(None):
    print("hi")
else:
    reveal_type(x)  # N: Revealed type is "type[builtins.str] | type[None]"

@codenamenam

codenamenam commented Sep 4, 2025 •

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, @sterliakov! It seems this PR doesn't fix #19308.
I've updated the PR description to only reference #19660.

@wyattscarpenter wyattscarpenter left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

Along with what's currently there, which ensures NoneType is callable, you should probably assert in your test case that the value produced by NoneType() and friends is None — um, or that its type is type[None], rather (or NoneType). Maybe you can use assert_type for this. Or possibly reveal_type plus expected notices, or some trick about narrowing, if assert_type doesn't work out for some reason.

@codenamenam

Copy link
Copy Markdown
Contributor Author

@wyattscarpenter Thanks for the review! I've modified the test case to use assert_type to verify the return type, as you suggested.

@github-actions

This comment has been minimized.

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just one nit on the test you added.

Comment thread test-data/unit/check-classes.test Outdated

def f(n: type[None]):
assert_type(n(), None)
def g(n: type[NoneType]): # type: ignore[valid-type]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use standard test syntax: remove the [out] at the bottom and replace this with a # E: whatever the error was. You can see some more examples around this.

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/builders/_epub_base.py:34:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
- sphinx/transforms/post_transforms/code.py:9:1: note: Hint: "python3 -m pip install types-Pygments"
- sphinx/transforms/post_transforms/code.py:9:1: note: (or run "mypy --install-types" to install all missing stub packages)
- sphinx/transforms/post_transforms/code.py:9:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
+ sphinx/cmd/build.py:460:1: note: Hint: "python3 -m pip install types-Pygments"
- sphinx/_cli/util/errors.py:102:1: note: Hint: "python3 -m pip install types-Pygments"
- sphinx/_cli/util/errors.py:102:1: note: (or run "mypy --install-types" to install all missing stub packages)
- sphinx/_cli/util/errors.py:102:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
+ sphinx/application.py:48:1: note: Hint: "python3 -m pip install types-Pygments"
+ sphinx/application.py:48:1: note: (or run "mypy --install-types" to install all missing stub packages)

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@hauntsaninja
hauntsaninja merged commit 73d7fb1 into python:master Oct 21, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot instantiate type "Type[None]"

5 participants