int
being a subtype of float
and type narrowing
#1209
Replies: 1 comment 1 reply
-
Yes, Consider that a class could be a subclass of both So yes, there are plenty of holes here. This is one of those situations where practicality trumps purity. |
Beta Was this translation helpful? Give feedback.
-
Suppose that
x
has typestr | float
. Shouldif not isinstance(x, float):
narrowx
tostr
, or tostr | int
? If it narrows it tostr
, it seems like the checking will be "unsound", becauseint
s are considered assignable tofloat
, but theisinstance
check doesn't consider integers to be floats.Right now both mypy and pyright think this is OK:
But a call like
f(42)
(also considered legal) will cause a runtime error, because the execution will go to theelse
branch.I don't have any real-world examples for this issue, so this is purely hypothetical for now.
Beta Was this translation helpful? Give feedback.
All reactions