basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

Don't report __orig_class__.__args__ as unknown members for generics

Open neijrr opened this issue 3 months ago • 1 comments

Description

While it is an implementation detail, currently it's the only way to reliably get access to type (instead of TypeVar) for generic class/function. So:

  • In non-generic classes/functions (so class X(...) or def x(...)) __orig_class__ should be reported as unknown, since it does not exist there
  • In generic classes/functions (class Y[...](...) or def y[...](...)) __orig_class__ should not be reported as unknown, and __orig_class__.__args__ should be considered to be tuple[type, ...]

Example: basedpyright playground

class A[T]:
    def func(self, a: int | float | T) -> int:
        # Check if a is instance of T
        if isinstance(a, self.__orig_class__.__args__[0]):
            return 0
        if isinstance(a, int):
            return 1
        return 2


a: A[str] = A[str]()
print(a.func("a"))
print(a.func(1))
print(a.func(1.1))

neijrr avatar Oct 12 '25 07:10 neijrr

the issue with not reporting an error here is we can't guarantee that __orig_class__ will actually be there at runtime:

a: A[str] = A()
print(a.func("a"))
AttributeError: 'A' object has no attribute '__orig_class__'. Did you mean: '__orig_bases__'?

DetachHead avatar Oct 13 '25 09:10 DetachHead