diff --git a/docs/source/error_code_list.rst b/docs/source/error_code_list.rst index d4e2c83323ac..1aec6cf7dbde 100644 --- a/docs/source/error_code_list.rst +++ b/docs/source/error_code_list.rst @@ -215,6 +215,28 @@ You can use :py:class:`~collections.abc.Callable` as the type for callable objec for x in objs: f(x) +.. _code-nonetype-type: + +Check that NoneType is not used as a type (annotation) [nonetype-type] +---------------------------------------------------------------------- + +The preferred way to annotate the type of `None` is `None`. +`NoneType` is equivalent, but mypy won't allow it by default. + +.. code-block:: python + + from types import NoneType + def f(x: None) -> None: + reveal_type(x) # note: Revealed type is "None" + + # error: NoneType should not be used as a type, please use None instead [nonetype-type] + def g(x: NoneType) -> None: + reveal_type(x) # note: Revealed type is "None" + + # error: NoneType should not be used as a type, please use None instead [nonetype-type] + x1: NoneType = None + x2: None = None # OK + .. _code-metaclass: Check the validity of a class's metaclass [metaclass] diff --git a/mypy/errorcodes.py b/mypy/errorcodes.py index 785b6166b18b..3fadd7bc07e7 100644 --- a/mypy/errorcodes.py +++ b/mypy/errorcodes.py @@ -60,6 +60,9 @@ def __hash__(self) -> int: "call-overload", "Check that an overload variant matches arguments", "General" ) VALID_TYPE: Final = ErrorCode("valid-type", "Check that type (annotation) is valid", "General") +NONETYPE_TYPE: Final = ErrorCode( + "nonetype-type", "Check that type (annotation) is not NoneType", "General" +) VAR_ANNOTATED: Final = ErrorCode( "var-annotated", "Require variable annotation if type can't be inferred", "General" ) diff --git a/mypy/typeanal.py b/mypy/typeanal.py index 06fa847c5434..b96e0ac9ec82 100644 --- a/mypy/typeanal.py +++ b/mypy/typeanal.py @@ -920,7 +920,7 @@ def analyze_type_with_type_info( self.fail( "NoneType should not be used as a type, please use None instead", ctx, - code=codes.VALID_TYPE, + code=codes.NONETYPE_TYPE, ) return NoneType(ctx.line, ctx.column)