Allow instantiating type[None] / types.NoneType - #21800
Open
mmaxjr wants to merge 1 commit into
Open
Conversation
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
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
analyze_type_type_callee()incheckexpr.pyhad no branch for mypy's internalNoneType. As a result, calling a callee of typetype[None]incorrectly producedCannot instantiate type "type[None]", even though this is valid at runtime (type(None)()andNoneType()both returnNone).Repro from the issue:
Before this change, mypy reported 4 spurious
Cannot instantiate type "type[None]"errors (plus the expectedNoneType should not be used as a typenote). After this change, only the expected note remains.Fix: add a case in
analyze_type_type_calleethat, forNoneTypeitems, returns a nullaryCallableTypewith return typeNoneType(), mirroring howtype(None)behaves at runtime.Test plan
testTypeCanInstantiateNoneTypetotest-data/unit/check-classes.testpytest mypy/test/testcheck.py -k "check-classes or check-python310"-> 810 passedpytest mypy/test/testcheck.py(full suite) -> 8175 passedmypy --config-file mypy_self_check.ini mypy/checkexpr.py-> no issuespre-commit runon changed files -> passedFixes #19660