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

Error while multiplying a `SupportsInt` and an int #4758

Closed
akaptur opened this Issue Mar 19, 2018 · 3 comments

Comments

Projects
None yet
4 participants
@akaptur

akaptur commented Mar 19, 2018

I've got the following code:

from decimal import Decimal
d = Decimal('10')
x = max(d, 0) * 3

Mypy types max(d, 0) as a SupportsInt, which seems correct, but then complains:

test.py:3: error: Unsupported operand types for * ("SupportsInt" and "int")

I expect to be able to perform any operation between X and SupportsX if that operation is supported between X and another X.

This is mypy 0.570 and the underlying code is Python 3.6.1. I can reproduce this issue with no flags passed.

@gvanrossum

This comment has been minimized.

Member

gvanrossum commented Mar 19, 2018

Hi Allison, SupportsInt basically means that an object can be passed to int(). This class does not itself have operator overload behavior like __add__.

It is derived as the return type for max() in your example because it's the common superclass for Decimal and int (the types of the arguments you pass it).

Your best option is probably to use max(d, Decimal(0)) instead.

@msullivan

This comment has been minimized.

Collaborator

msullivan commented Mar 21, 2018

Is there a documentation bug here that we should fix, or should we just close this?

@JukkaL

This comment has been minimized.

Collaborator

JukkaL commented Mar 22, 2018

I'll add a note about this to the documentation of SupportsInt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment