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

Pyright has problem with combo of @property and @cache #2133

Closed
MapleCCC opened this issue Jul 30, 2021 · 4 comments
Closed

Pyright has problem with combo of @property and @cache #2133

MapleCCC opened this issue Jul 30, 2021 · 4 comments
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request

Comments

@MapleCCC
Copy link

MapleCCC commented Jul 30, 2021

Describe the bug
The combo use of @property and @cache is backed by the document of functools.cached_property, so it should be considered a valid use case. However, pyright seems to have some bugs in handling such case.

To Reproduce

from functools import cache

class Rectangle:
    def __init__(self, length: int, width: int) -> None:
        self._length = length
        self._width = width

    @property
    @cache
    def area(self) -> int:
        return self._length * self._width

def is_large_rectangle(rec: Rectangle) -> bool:
    return rec.area >= 100

rec = Rectangle(10, 10)
print(is_large_rectangle(rec))

Pyright complains that Operator ">=" not supported for types "_lru_cache_wrapper[int]" and "Literal[100]".

Expected behavior
There should be no error for the above code.

VS Code extension or command-line
Pyright command-line tool 1.1.158

@erictraut
Copy link
Collaborator

This looks to be a limitation or a bug in the typeshed type definition for cache. I think pyright is applying the type information correctly here. It matches the behavior of mypy in this case.

    def cache(__user_function: Callable[..., _T]) -> _lru_cache_wrapper[_T]: ...

class _lru_cache_wrapper(Generic[_T]):
    __wrapped__: Callable[..., _T]
    def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...
    def cache_info(self) -> _CacheInfo: ...
    def cache_clear(self) -> None: ...

This definition implies that the resulting cache object is callable, and calling it will return the cached value. But that's not how it works at runtime.

I don't know why cache was defined as it currently is in functools.pyi. My advice is to file an issue in the typeshed repo and start a discussion there. We frequently pull changes from typeshed into pyright, so if a fix is made there, it will soon be fixed in pyright as well.

@erictraut erictraut added the as designed Not a bug, working as intended label Jul 30, 2021
@MapleCCC
Copy link
Author

@erictraut Thank you for your patient reply. To be honest, I am not familiar with the type annotation enough to understand your introspection. If it's convenient, could you consider open an issue in the typeshed library? I really don't know how to describe this complex type annotation thing 😵

@erictraut erictraut added enhancement request New feature or request addressed in next version Issue is fixed and will appear in next published version and removed as designed Not a bug, working as intended labels Jul 31, 2021
@erictraut
Copy link
Collaborator

This will be fixed in the next release.

@erictraut
Copy link
Collaborator

This is now addressed in pyright 1.1.159, which I just published. It will also be included in the next release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version enhancement request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants