I have the following example.
from typing import Union, TypeVar, Dict, Sequence
IdentifierSymbol = TypeVar("IdentifierSymbol", str, int)
def f(ids: Sequence[IdentifierSymbol]) -> Dict[int, IdentifierSymbol]:
return dict(zip(range(len(ids)), ids))
def g(ids: Union[Sequence[int], Sequence[str]]) -> int:
x = f(ids)
return 1
PyLance (I guess using mypy inside) complains with the following in the line x=f(ids)
:
Argument of type "Sequence[int] | Sequence[str]" cannot be assigned to parameter "ids" of type "Sequence[IdentifierSymbol@f]" in function "f"
Type "Sequence[int] | Sequence[str]" cannot be assigned to type "Sequence[IdentifierSymbol@f]"
TypeVar "_T_co@Sequence" is covariant
Type "str" is not compatible with constrained type "int" PylancereportGeneralTypeIssues
I am not sure how to interpret this, but it sounds like the problem is that int is not compatible with str. But why is this an issue in this case? I have either a sequence of strs or a sequence of ints, where does the compatibility between int and str come in?