basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

error on union with a quoted type and an unquoted type is only reported as an error when `enableExperimentalFeatures` is enabled

Open DetachHead opened this issue 4 months ago • 0 comments

While Optional is a matter of taste, there are certain cases where Union is required. Assume there's a "B" type defined lower in a file. The following hints fail with the pipe syntax, but succeed with Union.

class A:
    pass
C =  A | "B" # => TypeError: unsupported operand type(s) for |: 'type' and 'str
C = Union[A, "B"] # => Works!

class B:
    pass

basedpyright already handles this scenario gracefully:

C = int | "A" # Union syntax cannot be used with string operand; use quotes around entire expression (reportGeneralTypeIssues)

class A: ...

playground

Basedpyright seem to stop emitting an error since 1.20.0 (1.19.1 do emit one), is that intentional?

https://basedpyright.com/?pyrightVersion=1.20.0&typeCheckingMode=all&code=MIAgvCCWB2AuIB8QCICCyBQGDGAbAhgM6EioBcGIVIAdHRkA

Originally posted by @Glinte in #734

this is a runtime error so basedpyright should report an error regardless

DetachHead avatar Oct 27 '25 09:10 DetachHead