Skip to content
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
15 changes: 10 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
from mypy.types import (
ANY_STRATEGY,
MYPYC_NATIVE_INT_NAMES,
NOT_IMPLEMENTED_TYPE_NAMES,
OVERLOAD_NAMES,
AnyType,
BoolTypeQuery,
Expand Down Expand Up @@ -4959,10 +4960,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
)
# Treat NotImplemented as having type Any, consistent with its
# definition in typeshed prior to python/typeshed#4222.
if (
isinstance(typ, Instance)
and typ.type.fullname == "builtins._NotImplementedType"
):
if isinstance(typ, Instance) and typ.type.fullname in NOT_IMPLEMENTED_TYPE_NAMES:
typ = AnyType(TypeOfAny.special_form)

if defn.is_async_generator:
Expand Down Expand Up @@ -5121,7 +5119,14 @@ def type_check_raise(self, e: Expression, s: RaiseStmt, optional: bool = False)
# https://github.com/python/mypy/issues/11089
self.expr_checker.check_call(typ, [], [], e)

if isinstance(typ, Instance) and typ.type.fullname == "builtins._NotImplementedType":
if (
isinstance(typ, Instance)
and typ.type.fullname in {"builtins._NotImplementedType", "types.NotImplementedType"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this set to types.py where other similar "dual" sources live?

) or (
isinstance(e, CallExpr)
and isinstance(e.callee, RefExpr)
and e.callee.fullname in {"builtins.NotImplemented"}
):
self.fail(
message_registry.INVALID_EXCEPTION.with_additional_msg(
'; did you mean "NotImplementedError"?'
Expand Down
2 changes: 2 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@

ELLIPSIS_TYPE_NAMES: Final = ("builtins.ellipsis", "types.EllipsisType")

NOT_IMPLEMENTED_TYPE_NAMES: Final = ("builtins._NotImplementedType", "types.NotImplementedType")

# A placeholder used for Bogus[...] parameters
_dummy: Final[Any] = object()

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ if object():
if object():
raise NotImplemented # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
if object():
raise NotImplemented() # E: NotImplemented? not callable
raise NotImplemented() # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
[builtins fixtures/notimplemented.pyi]

[case testTryFinallyStatement]
Expand Down
13 changes: 10 additions & 3 deletions test-data/unit/fixtures/notimplemented.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ class bool: pass
class int: pass
class str: pass
class dict: pass
class tuple: pass
class ellipsis: pass

class _NotImplementedType(Any):
__call__: NotImplemented # type: ignore
NotImplemented: _NotImplementedType
import sys

if sys.version_info >= (3, 10): # type: ignore
from types import NotImplementedType
NotImplemented: NotImplementedType
else:
class _NotImplementedType(Any): ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-link to #20114 - if we get there, this Any should also be removed

NotImplemented: _NotImplementedType

class BaseException: pass