I'm trying to introduce Generic typing to a function of mine, but am getting a pylance error of:
Expression of type "A" cannot be assigned to declared type "T@func"
Type "A" cannot be assigned to type "T@func"
I've reduce my problem in my code to this simplified version:
from typing import TypeVar, Union, List
class A:
def __init__(self, arg: str) -> None:
self.arg = arg
class B:
def __init__(self, arg: int) -> None:
self.arg = arg
T = TypeVar("T", A, B)
def getA() -> A:
return A("a")
def getB() -> B:
return B(1)
def func(arg: T) -> T:
out: T
if isinstance(arg, A):
out = getA()
elif isinstance(arg, B):
out = getB()
return out
print(func(A("a")))
The error occurs at both out = getA()
and out = getB()
is pyright not able to accurately infer types here? Am I making a mistake?