Closed wch closed 1 week ago
Pyright is working as intended here, so I don't consider this a bug. I'm going to change this to an enhancement request and treat it as such.
IMO, it's really unfortunate that Python 3.10 added support for unions in isinstance
. A "union" operator applies only to sets of types and therefore should be used only in type expressions. This is the only place in all of Python where a union is treated specially within a value expression. This non-standard behavior leads to a bunch of problems and requires special-casing in specifications and tooling. And this change was completely unnecessary because isinstance
has always accepted tuples of classes.
I recommend avoiding the use of unions in isinstance
if you want your code to be robust. Use tuples instead.
I'm going to decline this enhancement request because I think it's pretty niche and applies only to Python 3.9 and older. Python 3.9 is nearing the end of its service lifetime.
Sounds good. Thanks for the explanation!
This is related to #8302.
The following code runs fine on Python>=3.10, but results in a runtime error on Python 3.9. This is the expected behavior for those versions, but it seems to me that Pyright should flag the issue when checking on 3.9?
Output from Python 3.9:
Pyright thinks it's OK:
However, I could understand if this is considered expected behavior for Pyright.
The reason I bring this up is because in our code base, we do have cases like this where
isinstance()
is used with a union type, and I would have hoped that pyright would find these type errors, instead of us encountering them at runtime.