basedpyright
basedpyright copied to clipboard
Don't report __orig_class__.__args__ as unknown members for generics
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(...)ordef x(...))__orig_class__should be reported as unknown, since it does not exist there - In generic classes/functions (
class Y[...](...)ordef y[...](...))__orig_class__should not be reported as unknown, and__orig_class__.__args__should be considered to betuple[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))
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__'?