Skip to content

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

Closed
@li-dan

Description

@li-dan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions