-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
type hint: how should type of lru_cache be defined? #5107
Comments
The underlying issue here is that the type variable for the I believe variadic function parameters could help with this. |
Well, almost. The type variable only represents the return type; the
decorator leaves the arguments unspecified, and the "Untyped decorator
makes function "{}" untyped" error (generated by
--disallow-untyped-decorators) considers that untyped.
Since the function in this case has no arguments it is arguably wrong, but
in general the lru_cache decorator does drop the types of the arguments
(alas), so `# type: ignore` is still the best you can do ATM.
|
I don't know if I was just unlucky but I encountered this bug few minutes after attempting to start using mypy for the first time on a project. Lucky me..., I had one function to check, and that one was using @lru_cache. What is the workaround? (code still supports py27). |
Since lru_cache is mostly transparent, maybe this?
|
Or for the parameterised variant: def lru_cache(maxsize: int = 128, typed: bool = False) -> Callable[[F], F]:
pass (And note also that |
|
What about the ability to access the decorated function in order to access cache info, clear the cache, etc.? The type hints for lru_cache decorator should reflect those are available as well so mypy won't emit errors when accessing them. What do you think? |
* [pre-commit.ci] pre-commit autoupdate updates: - https://github.com/pre-commit/mirrors-isort → https://github.com/PyCQA/isort - [github.com/PyCQA/isort: v5.9.3 → 5.9.3](PyCQA/isort@v5.9.3...5.9.3) - https://github.com/python/black.git → https://github.com/psf/black - [github.com/psf/black: 21.7b0 → 21.9b0](psf/black@21.7b0...21.9b0) - https://gitlab.com/pycqa/flake8.git → https://github.com/PyCQA/flake8 - [github.com/PyCQA/flake8: 3.9.2 → 4.0.1](PyCQA/flake8@3.9.2...4.0.1) - [github.com/ansible/ansible-lint.git: v5.1.2 → v5.2.0](https://github.com/ansible/ansible-lint.git/compare/v5.1.2...v5.2.0) - [github.com/pre-commit/mirrors-mypy: v0.910 → v0.910-1](pre-commit/mirrors-mypy@v0.910...v0.910-1) - [github.com/PyCQA/pylint: v2.10.2 → v2.11.1](pylint-dev/pylint@v2.10.2...v2.11.1) * Linting fixes * Avoided lru_cache due mypy problems python/mypy#5107 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
Is there a workaround for this issue? I tried using a |
The problem with the
The stubs in the typeshed chose B and C, which are arguably valuable, but it's a shame that we have to choose. The suggestion from @gvanrossum chooses A, which is also valuable: from typing import TYPE_CHECKING, TypeVar
if TYPE_CHECKING:
F = TypeVar('F', Callable)
def lru_cache(f: F) -> F: pass
else:
from functools import lru_cache It would be nice if The end result might look like this: _P = ParamSpec('P', bound=Hashable) # <-- enforce all args are hashable
_T = TypeVar('T')
@final
class _lru_cache_wrapper(Generic[_P, _T]):
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _T: ...
def cache_info(self) -> _CacheInfo: ...
def cache_clear(self) -> None: ...
def __copy__(self) -> _lru_cache_wrapper[_P, _T]: ...
def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_P, _T]: ...
def lru_cache(maxsize: int | None) -> Callable[Callable[_P, _T], Callable[_P, _T]]
... |
Shouldn't this be moved to the typeshed repo? |
We don't currently have the features we need to make the stubs any better. We have to choose the least bad solution, and I think the case could be made in either direction, so there's not a strong argument to break backward compatibility of the stubs. We either need a new feature (maybe something for Does anyone have any good ideas about what a new feature to solve this problem would look like? Does my |
In Python 3.11 this feature appears to be working correctly |
I don't think there's anything actionable for mypy here in any case. |
|
Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
Please provide more information to help us understand the issue:
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
What is the behavior/output you expect?
it should not emit an error, alternatively there should be instructions what to do.
What are the versions of mypy and Python you are using?
mypy 0.600
python 3.6.5
Do you see the same issue after installing mypy from Git master? did not try
What are the mypy flags you are using? (For example --strict-optional)
mypy predictor --ignore-missing-imports --strict
If mypy crashed with a traceback, please paste
the full traceback below.
Also asked in SO: https://stackoverflow.com/questions/50495759/type-hint-how-should-type-of-lru-cache-be-defined
The text was updated successfully, but these errors were encountered: