Skip to content

Annotation for builtin max() function uses wrong comparison operator #6336

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
rhettinger opened this issue Nov 18, 2021 · 1 comment · Fixed by #6342
Closed

Annotation for builtin max() function uses wrong comparison operator #6336

rhettinger opened this issue Nov 18, 2021 · 1 comment · Fixed by #6342
Labels
stubs: false positive Type checkers report false errors

Comments

@rhettinger
Copy link

The stubs for max() use SupportsLessThanT but should use SupportsGreaterThanT instead.

The CPython source uses Py_GT.

Instrumenting calls to max() shows the '>' operator being invoked:

class I(int):
    def __gt__(self, other):
        print(f'{self} > {other}')
        return int.__gt__(self, other)
    def __lt__(self, other):
        print(f'{self} < {other}')
        return int.__Lt__(self, other)

>>> largest = max(map(I, [10, 40, 20]))
40 > 10
20 > 40
>>> largest
40

A class that supplies only __gt__ suffices for a call to max():

class ReverseInt:
    def __init__(self, x):
        self.x = x
    def __gt__(self, other):
        return self.x < other.x
    def __repr__(self):
        return f'{type(self).__name__}({self.x!r})'
  
>>> max(map(ReverseInt, [10, 40, 20]))
ReverseInt(10)
@srittau srittau added the stubs: false positive Type checkers report false errors label Nov 19, 2021
srittau added a commit to srittau/typeshed that referenced this issue Nov 19, 2021
Add SupportsGreaterThan and SupportsGreaterThanT

Closes: python#6336
srittau added a commit to srittau/typeshed that referenced this issue Nov 19, 2021
Add SupportsGreaterThan and SupportsGreaterThanT

Closes: python#6336
srittau added a commit to srittau/typeshed that referenced this issue Nov 19, 2021
Add SupportsGreaterThan and SupportsGreaterThanT

Closes: python#6336
srittau added a commit that referenced this issue Nov 21, 2021
Add SupportsGreaterThan and SupportsGreaterThanT

Closes: #6336

Co-authored-by: Jelle Zijlstra <[email protected]>
@rhettinger
Copy link
Author

Thanks for fixing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants