-
-
Notifications
You must be signed in to change notification settings - Fork 999
chore(util): preserve LRU cache decorator types #2643
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
Open
paulkagiri
wants to merge
5
commits into
falconry:master
Choose a base branch
from
paulkagiri:type-lru-cache-decorators
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−16
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
710250d
fix(util): preserve lru cache decorator types
paulkagiri 73d5283
Merge branch 'master' into type-lru-cache-decorators
vytas7 358c0b9
Delete docs/_newsfragments/2629.newandimproved.rst
vytas7 90548ce
Merge branch 'master' into type-lru-cache-decorators
vytas7 a9e8995
Merge branch 'master' into type-lru-cache-decorators
vytas7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ | |
| import os | ||
| import os.path | ||
| import re | ||
| from typing import Any, Callable | ||
| from typing import Any, Callable, cast, TYPE_CHECKING | ||
| import unicodedata | ||
|
|
||
| from falcon import status_codes | ||
|
|
@@ -44,6 +44,23 @@ | |
| # public Falcon interface. | ||
| from .deprecation import deprecated | ||
|
|
||
| if TYPE_CHECKING: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These protocols look good at first glance. 👍 But could you please move them to |
||
| from typing import ParamSpec, Protocol, TypeVar | ||
|
|
||
| _P = ParamSpec('_P') | ||
| _R_co = TypeVar('_R_co', covariant=True) | ||
|
|
||
| class _CallableWithCacheClear(Protocol[_P, _R_co]): | ||
| cache_clear: Callable[[], None] | ||
|
|
||
| def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R_co: ... | ||
|
|
||
| class _LruCacheFactory(Protocol): | ||
| def __call__( | ||
| self, maxsize: int | ||
| ) -> Callable[[Callable[_P, _R_co]], _CallableWithCacheClear[_P, _R_co]]: ... | ||
|
|
||
|
|
||
| try: | ||
| from falcon.cyutil.misc import encode_items_to_latin1 as _cy_encode_items_to_latin1 | ||
| except ImportError: | ||
|
|
@@ -103,23 +120,27 @@ | |
| # the nocover pragma here. | ||
| def _lru_cache_nop( | ||
| maxsize: int, | ||
| ) -> Callable[[Callable[..., Any]], Callable[..., Any]]: # pragma: nocover | ||
| def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||
| ) -> Callable[ | ||
| [Callable[_P, _R_co]], _CallableWithCacheClear[_P, _R_co] | ||
| ]: # pragma: nocover | ||
| def decorator(func: Callable[_P, _R_co]) -> _CallableWithCacheClear[_P, _R_co]: | ||
| # NOTE(kgriffs): Partially emulate the lru_cache protocol; only add | ||
| # cache_info() later if/when it becomes necessary. | ||
| func.cache_clear = lambda: None # type: ignore | ||
| cached_func = cast('_CallableWithCacheClear[_P, _R_co]', func) | ||
| cached_func.cache_clear = lambda: None | ||
|
|
||
| return func | ||
| return cached_func | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| # PERF(kgriffs): Using lru_cache is slower on PyPy when the wrapped | ||
| # function is just doing a few non-IO operations. | ||
| _lru_cache_for_simple_logic: _LruCacheFactory | ||
| if PYPY: | ||
| _lru_cache_for_simple_logic = _lru_cache_nop # pragma: nocover | ||
| else: | ||
| _lru_cache_for_simple_logic = functools.lru_cache | ||
| _lru_cache_for_simple_logic = cast('_LruCacheFactory', functools.lru_cache) | ||
|
|
||
|
|
||
| def is_python_func(func: Callable[..., Any] | Any) -> bool: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please undo these unrelated changes?
Or does going via a variable somehow help with typing?
Since this is the testing client's code, we are not that concerned about performance, but it would still be good to understand why exactly we need this pattern.