-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
(🎁) Warn when NoneType
is used in a type
#11288
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
Labels
Comments
This was referenced Oct 7, 2021
5 tasks
Some more context from the duplicate issue #13139 below. Hints:
Example illustrating the problem:
|
NoneType
is used in a typeNoneType
is used in a type
@JukkaL I have partially improved this case in basedmypy. basedmypy: from types import NoneType
def foo(n: NoneType): # error: Variable "types.NoneType" is not valid as a type
print("hi")
nt: type[None] = NoneType # no error just by simply making |
ilevkivskyi
pushed a commit
that referenced
this issue
Jul 17, 2022
### Description Fixes #11288 Warns people to not use `NoneType` as a type annotation, as requested in #11288. ### Example **Before** ```python from types import NoneType def f(x: NoneType) -> None: pass f(None) # E: Argument 1 to "f" has incompatible type "None"; expected "NoneType" ``` **After** ```python from types import NoneType def f(x: NoneType) -> None: # E: NoneType should not be used as a type, please use None instead pass f(None) ``` Had to edit `test-data/unit/lib-stub/types.pyi` to allow NoneType type annotation.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NoneType
is a mangled up mess in the type realm and should never be used in a typing context.With the special case of
None
being a secret alias toNoneType
I think that many, many people are going to try to useNoneType
as an annotation. Adding a warning for the 99% of the time that it's invalidly used would be a great one.I understand that there are always edge cases, and disabling those warnings with a type comment would be the way to go IMO.
The text was updated successfully, but these errors were encountered: