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.
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.
Activity
srittau commented on 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 commented on May 2, 2021
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 commented on May 2, 2021
Eric explained it better than me. I don't think we should change the stubs in this case.