Skip to content
Open
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
22 changes: 22 additions & 0 deletions docs/source/error_code_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion mypy/typeanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down