diff --git a/mypy/typeanal.py b/mypy/typeanal.py index bd0f684653b2..38970db66747 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -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, diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 818981238b3b..0003ad2601e0 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -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] diff --git a/test-data/unit/lib-stub/types.pyi b/test-data/unit/lib-stub/types.pyi index 3ac4945ef5a7..4a6093f701cc 100644 --- a/test-data/unit/lib-stub/types.pyi +++ b/test-data/unit/lib-stub/types.pyi @@ -11,3 +11,6 @@ class ModuleType: if sys.version_info >= (3, 10): class Union: def __or__(self, x) -> Union: ... + + class NoneType: + ...