| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
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]" |
Sorry, something went wrong.
|
Thanks for the feedback, @sterliakov! It seems this PR doesn't fix #19308. |
Sorry, something went wrong.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
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. |
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks. Just one nit on the test you added.
Sorry, something went wrong.
|
|
||
| def f(n: type[None]): | ||
| assert_type(n(), None) | ||
| def g(n: type[NoneType]): # type: ignore[valid-type] |
There was a problem hiding this comment.
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.
Sorry, something went wrong.
|
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
|
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
(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.