Skip to content

max: comparing int with Decimal doesn't typecheck #5275

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
zormit opened this issue May 1, 2021 · 3 comments
Closed

max: comparing int with Decimal doesn't typecheck #5275

zormit opened this issue May 1, 2021 · 3 comments

Comments

@zormit
Copy link

zormit commented May 1, 2021

from decimal import Decimal

max(1, Decimal(2))

error: Value of type variable "SupportsLessThanT" of "max" cannot be "object"

I would expect this to pass.

@srittau
Copy link
Collaborator

srittau commented May 1, 2021

We could probably work around this in typeshed for the 2-arity case of max() by using a separate type var for the arguments and returning an union. The base problem is that mypy looks for a common base type in case like this, though, and only finds object. In my opinion it would be preferable to experiment with what happens if mypy would infer a union instead.

@erictraut
Copy link
Contributor

This is arguably a bug / limitation in mypy rather than a problem with the typeshed definition. The problem is that mypy uses a "join" operation to widen types in its type constraint solver rather than using a union operation.

The above sample type checks fine with pyright, which uses unions to widen types in its constraint solver. The return type of the call to max is int | Decimal.

@srittau
Copy link
Collaborator

srittau commented May 2, 2021

Eric explained it better than me. I don't think we should change the stubs in this case.

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

No branches or pull requests

3 participants