Replies: 1 comment 2 replies
-
Yes, this is intended behavior. The def bar(z: Base):
if _v1 := isinstance(z, Sub):
reveal_type(z) # Sub[Unknown]
_v1 = z.y
else:
reveal_type(z) # Base
reveal_type(z) # Sub[Unknown] | Base
print(_v1) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
https://pyright-play.net/?pythonVersion=3.14&strict=true&code=MYGwhgzhAEBCkFMBc0B06BQHSRgZQFcAjAbQBUBdACnggQEokNoXoAPFM51gTxSID2AkFgAmCAGbQJQqkUQpaDFOlRjJ0eQCcqAL0WJG3FgActASwB2AFyrmIViNbCXgCPQBpohIvWgvRaF1UHnpjaVldeiA
Basically: if you have a generic subclass, and (for runtime reasons), you do an
isinstance()
check (not qualifying the generic, as that's not valid), that somehow seems to taint the variable such that you can no longer use it in contexts that expect the base class:produces this error on the last line:
Here's some confusing data points:
print(...)
line, it no longer complainsand z.y
, it no longer complainsprint()
withif isinstance(...):
, it still complainsPoint 3 I can somewhat understand, even if I don't like it. We've now scoped
z
as aSub[Unknown]
and it doesn't like it. (I still feel we should be able to use it as aBase
)But in the current version, where just having done the
isinstance()
check at all it's erroring... that makes no sense to me.To be clear, it's not the case that there's an implicit guard for
z
to be aSub
because otherwisez.y
would error out, becauseand
on theprint
line will short-circuit, so passing aBase
in tobar()
would work fine (at runtime), and so would passing in a hypotheticalclass Sub2(Base): ...
instance.Beta Was this translation helpful? Give feedback.
All reactions