-
As noticed in this comment, it seems that under some circumstances, adding Here's the code from that comment simplified to not use pydantic: from functools import lru_cache
from typing import TypeVar, Generic, Any, TYPE_CHECKING
T = TypeVar("T")
class TypeAdapter(Generic[T]):
def validate_python(self, v: Any) -> T:
...
class VoiceRegion:
pass
@lru_cache()
def models_list_type(type_: type[T]) -> TypeAdapter[list[T]]:
return TypeAdapter(list[type_]) # type: ignore
var: list[VoiceRegion] = models_list_type(VoiceRegion).validate_python('abc') As written above, I get the following result when running pyright: /Users/davidmontague/Library/Application Support/JetBrains/PyCharm2023.1/scratches/scratch_379.py:19:26 - error: Expression of type "list[T@models_list_type]" cannot be assigned to declared type "list[VoiceRegion]"
"list[T@models_list_type]" is incompatible with "list[VoiceRegion]"
TypeVar "_T@list" is invariant
Type "T@models_list_type" cannot be assigned to type "VoiceRegion" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations However, if I comment out the This seems like a bug to me, since I don't think |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This unfortunate behavior is due to the way that typeshed defines def lru_cache(maxsize: Callable[..., _T], typed: bool = False) -> _lru_cache_wrapper[_T]: ... Where @final
class _lru_cache_wrapper(Generic[_T]):
__wrapped__: Callable[..., _T]
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
... Because the typeshed definition uses a To fix this, def lru_cache(maxsize: Callable[_P, _T], typed: bool = False) -> _lru_cache_wrapper[_P, _T]: ...
@final
class _lru_cache_wrapper(Generic[_P, _T]):
__wrapped__: Callable[_P, _T]
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ...
... If I make this change to my local copy of the typeshed stub file |
Beta Was this translation helpful? Give feedback.
This unfortunate behavior is due to the way that typeshed defines
lru_cache
.Where
_lru_cache_wrapper
is a class defined as:Because the typeshed definition uses a
...
, all of the input parameters of the wrapped call are lost, and there's no way to "solve" the return type for a wrapped generic function.To fix this,
_lru_cache_wrapper
could be rewritten to use aParamSpec
.