Skip to content
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

__lt__ and __gt__ should always be allowed to have return type bool #1101

Closed
gvanrossum opened this issue Jan 8, 2016 · 4 comments
Closed
Labels
bug mypy got something wrong

Comments

@gvanrossum
Copy link
Member

When the argument to __lt__ or __gt__ (or probably others) has type Any, mypy insists that the return type is also Any. (This is hardcoded: https://github.com/JukkaL/mypy/blob/master/mypy/checker.py#L621-L622 )

Why is this? Is it related to #363?

It's inconvenient.

@gvanrossum
Copy link
Member Author

Example:

from typing import Any

class Infinity:
    def __lt__(self, other: Any) -> bool:  # E: error: "Any" return type expected since argument to __lt__ has type "Any"
        return False
    def __gt__(self, other: Any) -> bool:  # E: error: "Any" return type expected since argument to __lt__ has type "Any"
        return True

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 8, 2016

This guards against a weird corner case that could cause a wrong type to be inferred. The example in the comment near the linked code gives some context (though it's not a complete example). It originates from my PhD research where type soundness was a major thing. As the goals of mypy have shifted, this check doesn't make sense any more. I'll just remove it.

@JukkaL JukkaL added the bug mypy got something wrong label Jan 8, 2016
gvanrossum added a commit that referenced this issue Jan 12, 2016
WIP: Rip out offending error messages for issue #1101.
@kamahen
Copy link
Contributor

kamahen commented Mar 20, 2018

It seems that mypy doesn't allow __ne__ returning NotImplemented, (this is allowed for __eq__).
And when I try explicitly adding NotImplemented as a possible return type, I get a run-time error:

  File ".../pod.py", line 65, in PlainOldData
    def __ne__(self, other: Any) -> Union[bool, NotImplemented]:
  File "/usr/lib/python3.6/typing.py", line 682, in inner
    return func(*args, **kwds)
  File "/usr/lib/python3.6/typing.py", line 800, in __getitem__
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/usr/lib/python3.6/typing.py", line 800, in <genexpr>
    parameters = tuple(_type_check(p, msg) for p in parameters)
  File "/usr/lib/python3.6/typing.py", line 374, in _type_check
    raise TypeError(msg + " Got %.100r." % (arg,))
TypeError: Union[arg, ...]: each arg must be a type. Got NotImplemented.

If I change this to def __ne__(self, other: Any) -> Union[bool, type(NotImplemented)]:, then mypy complains:

error: invalid type comment or annotation
note: Suggestion: use type[...] instead of type(...)

and when I try following the suggestion, I get a runtime error:

TypeError: 'type' object is not subscriptable

@gvanrossum
Copy link
Member Author

Please use a new issue for such feedback. The root cause seems to be that __ne__ is not included in the list BINARY_MAGIC_METHODS in mypy/sharedparse.py. You could submit a PR that fixes this (and adds a test).

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