I am trying to do something when 2 objects have the same type. However, after checking if(type(list_type) == type(other_list_type))
, Pylance still thinks that the types are different: Type "ListType" cannot be assigned to type "T@ListPattern"PylancereportArgumentType
.
from typing import Generic, TypeVar
from pydantic import BaseModel
class LetterList(BaseModel):
letter: str
class NumberList(BaseModel):
numbers: list[int]
class BulletPointsList(BaseModel):
bullet: str
ListType = LetterList | NumberList | BulletPointsList | None
T = TypeVar("T", bound=ListType)
class ListPattern(Generic[T]):
def test(self, list_type: T, other_list_type: ListType):
if(type(list_type) == type(other_list_type)):
self.handle_same_type(
list_type=list_type,
# Argument of type "ListType" cannot be assigned to parameter "other_list_type" of type "T@ListPattern" in function "handle_same_type"
other_list_type=other_list_type
)
def handle_same_type(
self,
list_type: T,
other_list_type: T
):
pass
pattern = ListPattern[LetterList]
I think this has to do with the use of generics. Is there way to check that other_list_type
has type T
before passing it to the function handle_same_type
?