Skip to content

Commit 2f302a9

Browse files
committed
Hackish bugfix for union reverse_op_method
1 parent 0b82574 commit 2f302a9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

mypy/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ def check_reverse_op_method(self, defn: FuncItem, typ: Callable,
585585
self.msg.reverse_operator_method_with_any_arg_must_return_any(
586586
method, defn)
587587
return
588+
elif isinstance(arg_type, UnionType):
589+
if not arg_type.has_readable_member(other_method):
590+
fail = True
588591
else:
589592
fail = True
590593
if fail:

mypy/types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ def length(self) -> int:
433433
def accept(self, visitor: 'TypeVisitor[T]') -> T:
434434
return visitor.visit_union_type(self)
435435

436+
def has_readable_member(self, name):
437+
"""For a tree of unions of instances, check whether all
438+
instances have a given member.
439+
440+
This should probably be refactored to go elsewhere."""
441+
return all(isinstance(x, UnionType) and x.has_readable_member(name) or
442+
isinstance(x, Instance) and
443+
x.type.has_readable_member(name)
444+
for x in self.items)
445+
436446

437447
class RuntimeTypeVar(Type):
438448
"""Reference to a runtime variable with the value of a type variable.

0 commit comments

Comments
 (0)