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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With