-
Notifications
You must be signed in to change notification settings - Fork 4k
Type annotations for the .clear() method on cached functions
#11793
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 annotations for the .clear() method on cached functions
#11793
Conversation
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) ✅ license/snyk check is complete. No issues have been found. (View Details) |
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.
Pull Request Overview
This PR introduces type annotations for the .clear() method on cached functions to improve developer experience with auto-completion and type safety.
- Added unit tests in the typing directory to validate the clear() method's behavior.
- Updated both cache_resource_api.py and cache_data_api.py to include a CachedFunc Protocol that overloads clear() to support calls with or without arguments.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/tests/streamlit/typing/cache_types.py | Adds type tests to assert that calling clear() (with extra arguments) returns None. |
| lib/streamlit/runtime/caching/cache_resource_api.py | Updates type annotations for the cache resource API, including a new CachedFunc Protocol overload for clear(). |
| lib/streamlit/runtime/caching/cache_data_api.py | Updates type annotations for the cache data API, introducing a CachedFunc Protocol similar to the resource API. |
Comments suppressed due to low confidence (3)
lib/tests/streamlit/typing/cache_types.py:33
- The tests allow calling clear() with extra arguments even though its expected behavior is to clear the cache without parameters. Consider adding a comment to explain the rationale behind permitting extra arguments to avoid future confusion.
assert_type(cached_data_fn.clear(), None)
lib/streamlit/runtime/caching/cache_resource_api.py:208
- The clear() method overloads include a fallback using Any for parameters due to current type system limitations. A brief comment explaining this choice would help future maintainers understand why multiple overloads are provided.
def clear(self) -> None: ...
lib/streamlit/runtime/caching/cache_data_api.py:335
- Similar to the cache_resource_api, the clear() overloads in this module use a fallback overload with Any. Consider adding documentation that clarifies this design decision for consistency and improved maintainability.
def clear(self, *args: P.args, **kwargs: P.kwargs) -> None: ...
lukasmasuch
left a comment
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.
LGTM 👍 Thanks for the contribution.
|
I found this change has a degradation that the docstring of the cached function is not displayed in VSCode now. |
Oh, hmm... not sure if there is a way to have it show the correct docstring and be typed in a way that it has the |
|
It's discussed in microsoft/pylance-release#5840 for the case of Pylance/VSCode. |
…e classes and functions (#11821) ## Describe your changes I introduced `CachedFunc` protocol in #11793 for better typing on the caching methods, however, I found there is already [`CachedFunc` class](https://github.com/streamlit/streamlit/blob/dac672d51db847000e742e33ecd8d3eaff333304/lib/streamlit/runtime/caching/cache_utils.py#L193) and I thought enhancing the type annotations on the class and methods around it would be more straightforward than introducing the new protocol that duplicates some type definitions. This PR removes the protocol and enhances the type annotations on the classes and functions that have the implementations about caching instead. The core intention of this PR is to make the [`CachedFunc` class in `cache_utils.py`](https://github.com/streamlit/streamlit/pull/11821/files#diff-011ce302bc727edc7f3c2aad84725876b1d850a4af817b86fc61f915af4c6101R197) generic and to use it instead of [`CachedFunc` protocol in `cache_data_api.py`](https://github.com/streamlit/streamlit/pull/11821/files#diff-e5462f7904115502e2adeb16b9609d781c6ab56d1b40e6add0a537143c80063eL328) and [`cache_resource_api.py](https://github.com/streamlit/streamlit/pull/11821/files#diff-a9269bf7bb86a3e14792547a64e48f1cd5c123d3562c84e55862b5f896578f97L204). Other changes are automatically done to make the associated classes and functions generic as well. ## GitHub Issue Link (if applicable) ## Testing Plan - Explanation of why no additional tests are needed - [x] Unit Tests (JS and/or Python) - E2E Tests - Any manual testing needed? --- **Contribution License Agreement** By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
Describe your changes
I wanted type hints on the
.clear()method on cached functions for better developer experience such as auto-completion.GitHub Issue Link (if applicable)
Testing Plan
Unit Tests (Python)
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.