Skip to content

False positive "unsafe[] overlapping" for _floordiv__/__rfloordiv__ with Real argument, but nearly identical signatures for other operators are fine #10755

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
posita opened this issue Jul 2, 2021 · 3 comments
Labels
bug mypy got something wrong

Comments

@posita
Copy link
Contributor

posita commented Jul 2, 2021

Bug Report

Where the single argument to __floordiv__/__rfloordiv__ includes numbers.Real, it results in (what I think is) a false Signatures … are unsafely overlapping error.

To Reproduce

# test_case.py
from numbers import Real
from operator import floordiv, mul, truediv
from typing import NamedTuple, Union

_OperandT = Union[float, Real]
# _OperandT = Real  # <- results in the same error
# _OperandT = float  # <- this works

class Spam(NamedTuple):
    value: _OperandT

    def __floordiv__(self, other: _OperandT) -> "Spam":
        return Spam(floordiv(self.value, other))

    def __rfloordiv__(self, other: _OperandT) -> "Spam":  # <- this presents an error …
        return Spam(floordiv(other, self.value))

    def __mul__(self, other: _OperandT) -> "Spam":
        return Spam(mul(self.value, other))

    def __rmul__(self, other: _OperandT) -> "Spam":  # <- … but this is just fine …
        return Spam(mul(other, self.value))

    def __truediv__(self, other: _OperandT) -> "Spam":
        return Spam(truediv(self.value, other))

    def __rtruediv__(self, other: _OperandT) -> "Spam":  # <- … and so is this
        return Spam(truediv(other, self.value))

Actual Behavior

$ mypy --version
mypy 0.902
$ mypy test_case.py
test_case.py:16: error: Signatures of "__rfloordiv__" of "Spam" and "__floordiv__" of "Union[float, Real]" are unsafely overlapping
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I would expect symmetry with other binary operator signatures. I'm also pretty sure there isn't any actual overlap here, but I could be wrong.

@posita posita added the bug mypy got something wrong label Jul 2, 2021
@posita posita changed the title Alleged "unsafe[] overlapping" with _floordiv__/__rfloordiv__ only with Real argument, but nearly identical __truediv__/__rtruediv__ signatures are fine False positive "unsafe[] overlapping" for _floordiv__/__rfloordiv__ with Real argument, but nearly identical signatures for other operators are fine Jul 2, 2021
@ethanscorey
Copy link

ethanscorey commented Feb 16, 2022

Did you ever find a fix for this? I ran into the same exact issue with my code, which is structured similarly to yours. In particular, I get the issue if I use numbers.Rational in the signature for __floordiv__ and __rfloordiv__, but not any other arithmetic operation.

@JelleZijlstra
Copy link
Member

I suspect this is because numbers.pyi in typeshed declares Real.__rfloordiv__ as returning int, but Real.__rmul__ returns Any. I'm not sure how this should be fixed though.

@A5rocks
Copy link
Collaborator

A5rocks commented Jun 27, 2024

As of #17392 the overlapping is gone. However, in the time since, this has gained an LSP error. I still think the issue can be closed?

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

5 participants