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

Unsupported operand types using += with floats initialized to 0 (not 0.0) #1193

Open
timabbott opened this issue Jan 29, 2016 · 5 comments
Open
Labels

Comments

@timabbott
Copy link

mypy complains with:
/home/tabbott/foo.py:4: error: Unsupported operand types for + ("int" and "float") with the following code:

foo = 0
foo += 0.5

While one can work around this by initializing to 0.0, it seems worth thinking about if mypy should accept this.

@gvanrossum
Copy link
Member

gvanrossum commented Jan 29, 2016 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2016

We could probably tweak partial types to work for the original example, but as Guido pointed out, it would not work in general. However, it might be good enough. I'd like to see more real-world examples before deciding what to do with this.

Note that you can also add an annotation (though changing the initializer is usually easier):

foo = 0  # type: float
foo += 0.5

@timabbott
Copy link
Author

Yep makes sense; for a data point I had to do this 6 times in zulip.git.

    -Tim Abbott

On Fri, Jan 29, 2016 at 12:02 PM, Jukka Lehtosalo notifications@github.com
wrote:

We could probably tweak partial types to work for the original example,
but as Guido pointed out, it would not work in general. However, it might
be good enough. I'd like to see more real-world examples before deciding
what to do with this.

Note that you can also add an annotation (though changing the initializer
is usually easier):

foo = 0 # type: float
foo += 0.5


Reply to this email directly or view it on GitHub
#1193 (comment).

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2016

@timabbott Could you give more information about the 6 cases in zulip.git -- or were they all similar to x = 0 / x += 0.5?

@timabbott
Copy link
Author

See timabbott/zulip@bf82ac3

5/6 are like that and the other is like that except x is in a dict.

@ddfisher ddfisher added this to the Future milestone Mar 1, 2016
@gvanrossum gvanrossum removed this from the Future milestone Mar 29, 2017
@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants