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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Suppose that
xhas typestr | float. Shouldif not isinstance(x, float):narrowxtostr, or tostr | int? If it narrows it tostr, it seems like the checking will be "unsound", becauseints are considered assignable tofloat, but theisinstancecheck 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 theelsebranch.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