On the day PyCharm thought that int / float did not implement __div__

UPDATE This issue is resolved 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 leads to the fact that PyCharm 2017.1.4 with quite a few analyzer default settings (I believe) shows the following warning:

The expected type '{__div__}', instead is 'Union [int, float]'.

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

Now, of course, Python does not have a static type check, but here it is obvious that barthere will always be inteither float(and PyCharm understands this as obvious in the got Union[int, float]warning above).

, PyCharm , , int float __div__, - , ?

UPDATE

, foo return x * 60 * 60, , PyCharm x __mul__.

2

foo return float(x) / (60 * 60), , , x int float, .

​​PyCharm.

+4
1

, , PyCharm, 2017.1.2

: Python ? 2.7x 3.x?

0

Source: https://habr.com/ru/post/1680798/


All Articles