Skip to content

Mypy blows up sometimes with bad reverse method #5337

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

Closed
sid-kap opened this issue Jul 9, 2018 · 3 comments
Closed

Mypy blows up sometimes with bad reverse method #5337

sid-kap opened this issue Jul 9, 2018 · 3 comments

Comments

@sid-kap
Copy link
Contributor

sid-kap commented Jul 9, 2018

When I run mypy (no special arguments) on the following code (with --show-traceback):

class Foo:
    def __radd__(self, other: Foo, other2: Foo = None) -> Foo:
        return self

it fails with this error:

test_radd.py:2: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.610
Traceback (most recent call last):
  ...
  ...
  File "/Users/sidharthkapur/.local/share/virtualenvs/mypy-test-VQzn8vfh/lib/python3.7/site-packages/mypy/checker.py", line 957, in check_reverse_op_method
    assert len(reverse_type.arg_types) == 2
AssertionError: 
test_radd.py:2: : note: use --pdb to drop into pdb

The expected behavior should be the same as in the following, similar example:

class Foo:
    def __radd__(self, other: Foo, other2: Foo) -> Foo:
        return self

which gracefully fails with the message:

test_radd.py:2: error: Invalid signature "def (test_radd.Foo, test_radd.Foo, test_radd.Foo) -> test_radd.Foo"
  • Python version: 3.7.0
  • Mypy version: 0.610
@gvanrossum gvanrossum added the bug mypy got something wrong label Jul 9, 2018
@gvanrossum
Copy link
Member

Heh, that sounds like a pretty simple bug. We clearly should use a more sophisticated check than checking for exactly 2 arguments. Maybe you can give it a try yourself?

@sid-kap
Copy link
Contributor Author

sid-kap commented Jul 9, 2018

Sure, I can try!

@sid-kap
Copy link
Contributor Author

sid-kap commented Jul 9, 2018

After thinking about it some more, I think this is actually a false positive; having a default argument should actually be ok. (Rereading what you wrote above, I think you already realized this.) Running the following code works:

class Foo:
    def __radd__(self, other: object, other2: object = None) -> 'Foo':
        return self

f = Foo()
g = 1 + f

Would

assert is_subtype(reverse_type, method_type)

make sense as a better check here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants