Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The day PyCharm thought int / float does not implement __div__

Tags:

python

pycharm

UPDATE This issue is solved in PyCharm 2017.2

def foo(x):
    return x / (60 * 60)

z = 1
y = 2
bar = 0 if y == 0 else z / y

foo(bar)

This causes PyCharm 2017.1.4 with pretty much default analyzer settings (I believe) show the following warning:

Expected type '{__div__}', got 'Union[int, float]' instead.

This inspection detects type errors in function call expressions. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases

Now, of course Python doesn't have static type checking, but it is obvious here that bar will always be int or float (and PyCharm understands it as evident by got Union[int, float] in the above warning).

So, is this a bug in PyCharm with this specific use case in which it fails to see that int and float do implement __div__, or is there something else that I'm missing here?

UPDATE

Oddly enough, if foo is modified to return x * 60 * 60 the warning goes away and PyCharm doesn't warn about x not implementing __mul__.

UPDATE 2

Changing foo to return float(x) / (60 * 60) obviously makes the warning disappear, but x is already inferred as int or float as evident by the warning itself.

Definitely seems like a bug in PyCharm.

like image 224
DeepSpace Avatar asked Nov 07 '22 19:11

DeepSpace


1 Answers

This seems to be a bug of your PyCharm Version, I can't reproduce it in 2017.1.2

Edit: Which Python compiler do you use? 2.7x or 3.x?

like image 93
Lucy The Brazen Avatar answered Nov 15 '22 04:11

Lucy The Brazen