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: 15 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4581,6 +4581,13 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
s.expr, return_type, allow_none_return=allow_none_func_call
)
)
# 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"
):
typ = AnyType(TypeOfAny.special_form)

if defn.is_async_generator:
self.fail(message_registry.RETURN_IN_ASYNC_GENERATOR, s)
Expand Down Expand Up @@ -4746,6 +4753,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":
self.fail(
message_registry.INVALID_EXCEPTION.with_additional_msg(
'; did you mean "NotImplementedError"?'
),
s,
)

def visit_try_stmt(self, s: TryStmt) -> None:
"""Type check a try statement."""
# Our enclosing frame will get the result if the try/except falls through.
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@ if object():
raise BaseException from f # E: Exception must be derived from BaseException
[builtins fixtures/exception.pyi]

[case testRaiseNotImplementedFails]
if object():
raise NotImplemented # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
if object():
raise NotImplemented() # E: NotImplemented? not callable
[builtins fixtures/notimplemented.pyi]

[case testTryFinallyStatement]
import typing
try:
Expand Down
10 changes: 7 additions & 3 deletions test-data/unit/fixtures/notimplemented.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# builtins stub used in NotImplemented related cases.
from typing import Any, cast

from typing import Any

class object:
def __init__(self) -> None: pass
Expand All @@ -10,5 +9,10 @@ class function: pass
class bool: pass
class int: pass
class str: pass
NotImplemented = cast(Any, None)
class dict: pass

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

class BaseException: pass