Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic cached return value cannot be converted to declared value even when they are the same type #11729

Closed
jzazo opened this issue Apr 7, 2024 · 1 comment

Comments

@jzazo
Copy link
Contributor

jzazo commented Apr 7, 2024

Describe the bug
I declare a generic return type, and I have a provider of that same return type, but cached. Mypy does not declare an error but Pyright raises one. If I remove the caching no error is reported.

I raised this issue in pyright's repo. @erictraut stated that cache method does not maintain the signature of the function it's decorating. This other issue might be related.

Code

from abc import abstractmethod
from functools import cache
from typing import Any, Generic, Protocol, TypeVar

from typing_extensions import NamedTuple

T = TypeVar("T")
T_co = TypeVar("T_co", covariant=True)

class ServiceId(NamedTuple, Generic[T]):
    id: str

class Provider(Protocol[T_co]):
    @abstractmethod
    def __call__(self) -> T_co:
        ...

class Servicer:
    _d: dict[str, Any]

    @cache  # commenting this line removes the error
    def get_service(self, service_id: ServiceId[T]) -> T:
        return self._d[service_id.id]

    def get_provider(self, service_id: ServiceId[T]) -> Provider[T]:
        return lambda: self.get_service(service_id)  # Expression of type "() -> T@get_service" cannot be assigned to return type "Provider[T@get_provider]"
@srittau
Copy link
Collaborator

srittau commented Apr 7, 2024

Looks to be a duplicate of #11280.

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants