-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: Allow instantiation of type[None] in analyze_type_type_callee #19782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
|
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]" |
|
Thanks for the feedback, @sterliakov! It seems this PR doesn't fix #19308. |
There was a problem hiding this comment.
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.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
|
@wyattscarpenter Thanks for the review! I've modified the test case to use assert_type to verify the return type, as you suggested. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
(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:
This ensures proper handling of NoneType instantiation and prevents spurious errors.