Skip to content

Treating NoneType as None and warning against using NoneType. #13153

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

Merged
merged 1 commit into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ def analyze_type_with_type_info(
# Create a named TypedDictType
return td.copy_modified(item_types=self.anal_array(list(td.items.values())),
fallback=instance)

if info.fullname == 'types.NoneType':
self.fail("NoneType should not be used as a type, please use None instead", ctx)
return NoneType(ctx.line, ctx.column)

return instance

def analyze_unbound_type_without_type_info(self, t: UnboundType, sym: SymbolTableNode,
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1591,3 +1591,12 @@ def test_func(test_str: str) -> str:
return "special"
case _:
return "other"


[case testNoneTypeWarning]
from types import NoneType

def foo(x: NoneType): # E: NoneType should not be used as a type, please use None instead
reveal_type(x) # N: Revealed type is "None"

[builtins fixtures/tuple.pyi]
3 changes: 3 additions & 0 deletions test-data/unit/lib-stub/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ class ModuleType:
if sys.version_info >= (3, 10):
class Union:
def __or__(self, x) -> Union: ...

class NoneType:
...