Skip to content

Check signature of reverse operator methods #4249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2017
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
20 changes: 15 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])

if name: # Special method names
if name in nodes.reverse_op_method_set:
self.check_reverse_op_method(item, typ, name)
self.check_reverse_op_method(item, typ, name, defn)
elif name in ('__getattr__', '__getattribute__'):
self.check_getattr_method(typ, defn, name)
elif name == '__setattr__':
Expand Down Expand Up @@ -816,13 +816,24 @@ def is_trivial_body(self, block: Block) -> bool:
isinstance(stmt.expr, EllipsisExpr)))

def check_reverse_op_method(self, defn: FuncItem, typ: CallableType,
method: str) -> None:
method: str, context: Context) -> None:
"""Check a reverse operator method such as __radd__."""

# This used to check for some very obscure scenario. It now
# just decides whether it's worth calling
# check_overlapping_op_methods().

# First check for a valid signature
method_type = CallableType([AnyType(TypeOfAny.special_form),
AnyType(TypeOfAny.special_form)],
[nodes.ARG_POS, nodes.ARG_POS],
[None, None],
AnyType(TypeOfAny.special_form),
self.named_type('builtins.function'))
if not is_subtype(typ, method_type):
self.msg.invalid_signature(typ, context)
return

if method in ('__eq__', '__ne__'):
# These are defined for all objects => can't cause trouble.
return
Expand All @@ -835,9 +846,8 @@ def check_reverse_op_method(self, defn: FuncItem, typ: CallableType,
if isinstance(ret_type, Instance):
if ret_type.type.fullname() == 'builtins.object':
return
# Plausibly the method could have too few arguments, which would result
# in an error elsewhere.
if len(typ.arg_types) <= 2:

if len(typ.arg_types) == 2:
# TODO check self argument kind

# Check for the issue described above.
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,18 @@ class C:
def __radd__(self, x: Any) -> int: pass
[out]

[case testReverseOperatorMethodInvalid]
from foo import *
[file foo.pyi]
class A: ...
class B:
def __rmul__(self) -> A: ...
class C:
def __radd__(self, other, oops) -> int: ...
[out]
tmp/foo.pyi:3: error: Invalid signature "def (foo.B) -> foo.A"
tmp/foo.pyi:5: error: Invalid signature "def (foo.C, Any, Any) -> builtins.int"

[case testReverseOperatorMethodForwardIsAny]
from typing import Any
def deco(f: Any) -> Any: return f
Expand Down