Skip to content

mypy --py2 does not respect division future statement #5679

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
li-dan opened this issue Sep 26, 2018 · 3 comments
Closed

mypy --py2 does not respect division future statement #5679

li-dan opened this issue Sep 26, 2018 · 3 comments
Labels
bug mypy got something wrong

Comments

@li-dan
Copy link
Contributor

li-dan commented Sep 26, 2018

When I use from __future__ import division, mypy looks for __div__ on objects instead of __truediv__. Example code:

from __future__ import division


class ClassWithDiv(object):
    def __div__(self, other):
        # type: (ClassWithDiv) -> None
        return None


class ClassWithTrueDiv(object):
    def __truediv__(self, other):
        # type: (ClassWithTrueDiv) -> None
        return None


ClassWithDiv() / ClassWithDiv()
ClassWithTrueDiv() / ClassWithTrueDiv()

The actual output of running mypy --py2:

$ mypy --py2 test_div.py
test_div.py:17: error: "ClassWithTrueDiv" has no attribute "__div__"

The expected output should be something like this:

test_div.py:16: error: "ClassWithDiv" has no attribute "__truediv__"

It would also be acceptable to give the same output as in Python 3 mode, which is:

$ mypy test_div.py
test_div.py:16: error: Unsupported left operand type for / ("ClassWithDiv")

I am using mypy from Git master. Version info:

$ mypy --version
mypy 0.640+dev-2244721df395ea7ccb986286a12594e2d7b097e8
$ python --version
Python 3.6.5

I am not using any mypy flags besides --py2.

@gvanrossum gvanrossum added the bug mypy got something wrong label Sep 26, 2018
@li-dan
Copy link
Contributor Author

li-dan commented Sep 26, 2018

A more obvious reproducer:

from __future__ import division

reveal_type(1 / 2)

The output of this is:

$ mypy --py2 test_div2.py
test_div2.py:3: error: Revealed type is 'builtins.int'

But it should be:

test_div2.py:3: error: Revealed type is 'builtins.float'

@gvanrossum
Copy link
Member

That's a good catch. Do you feel up to attempting to submit a fix as a PR?

@ilevkivskyi
Copy link
Member

This is actually a duplicate of #2048, so I am going to close this one, since the original has more discussion. Feel free to submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants