Skip to content

Commit 9bb029f

Browse files
committed
support unions
1 parent 4143846 commit 9bb029f

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

mypy/checker.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4895,6 +4895,10 @@ def infer_context_dependent(
48954895
return typ
48964896

48974897
def check_return_stmt(self, s: ReturnStmt) -> None:
4898+
4899+
def is_notimplemented(t: object) -> bool:
4900+
return isinstance(t, Instance) and t.type.fullname == "builtins._NotImplementedType"
4901+
48984902
defn = self.scope.current_function()
48994903
if defn is not None:
49004904
if defn.is_generator:
@@ -4946,6 +4950,7 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49464950
if defn.is_async_generator:
49474951
self.fail(message_registry.RETURN_IN_ASYNC_GENERATOR, s)
49484952
return
4953+
49494954
# Returning a value of type Any is always fine.
49504955
if isinstance(typ, AnyType):
49514956
# (Unless you asked to be warned in that case, and the
@@ -4971,18 +4976,18 @@ def check_return_stmt(self, s: ReturnStmt) -> None:
49714976
if is_lambda or isinstance(typ, NoneType):
49724977
return
49734978
self.fail(message_registry.NO_RETURN_VALUE_EXPECTED, s)
4974-
elif (
4975-
isinstance(typ, Instance)
4976-
and typ.type.fullname == "builtins._NotImplementedType"
4977-
and (
4978-
(defn.name in BINARY_MAGIC_METHODS or defn.name == "__subclasshook__")
4979-
)
4980-
):
4981-
return
49824979
else:
4980+
typ_: Type = typ
4981+
if defn.name in BINARY_MAGIC_METHODS or defn.name == "__subclasshook__":
4982+
if is_notimplemented(typ):
4983+
return
4984+
if isinstance(typ, UnionType):
4985+
typ_ = UnionType.make_union(
4986+
[i for i in typ.items if not is_notimplemented(i)]
4987+
)
49834988
self.check_subtype(
49844989
subtype_label="got",
4985-
subtype=typ,
4990+
subtype=typ_,
49864991
supertype_label="expected",
49874992
supertype=return_type,
49884993
context=s.expr,

test-data/unit/check-overloading.test

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6854,8 +6854,21 @@ reveal_type(headers) # N: Revealed type is "Union[__main__.Headers, typing.Iter
68546854
[builtins fixtures/isinstancelist.pyi]
68556855

68566856
[case testReturnNotImplementedInBinaryMagicMethods]
6857+
from typing import Union
68576858
class A:
6858-
def __eq__(self, other: object) -> bool: return NotImplemented
6859+
def __add__(self, other: object) -> int:
6860+
return NotImplemented
6861+
def __radd__(self, other: object) -> Union[int, NotImplementedType]:
6862+
return NotImplemented
6863+
def __sub__(self, other: object) -> Union[int, NotImplementedType]:
6864+
return 1
6865+
def __isub__(self, other: object) -> int:
6866+
x: Union[int, NotImplementedType]
6867+
return x
6868+
def __mul__(self, other: object) -> Union[int, NotImplementedType]:
6869+
x: Union[int, NotImplementedType]
6870+
return x
6871+
68596872
[builtins fixtures/notimplemented.pyi]
68606873

68616874
[case testReturnNotImplementedABCSubclassHookMethod]

0 commit comments

Comments
 (0)