Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@whitphx
Copy link
Contributor

@whitphx whitphx commented Jun 27, 2025

Describe your changes

I wanted type hints on the .clear() method on cached functions for better developer experience such as auto-completion.

@st.cache_data
def fn():
    return 42

fn.clear()  # The type of `clear()` should be inferred as `() -> None`.

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.

@snyk-io
Copy link
Contributor

snyk-io bot commented Jun 27, 2025

🎉 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)

@lukasmasuch lukasmasuch requested a review from Copilot June 27, 2025 10:49
Copy link
Contributor

Copilot AI left a 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 lukasmasuch added security-assessment-completed Security assessment has been completed for PR change:other PR contains other type of change impact:users PR changes affect end users labels Jun 27, 2025
Copy link
Collaborator

@lukasmasuch lukasmasuch left a 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.

@lukasmasuch lukasmasuch enabled auto-merge (squash) June 27, 2025 11:02
@lukasmasuch lukasmasuch merged commit 9c230d0 into streamlit:develop Jun 27, 2025
38 of 39 checks passed
@whitphx whitphx deleted the cache-clear-type-annotation branch June 27, 2025 11:09
@whitphx
Copy link
Contributor Author

whitphx commented Jun 27, 2025

I found this change has a degradation that the docstring of the cached function is not displayed in VSCode now.
Will see it further.

@lukasmasuch
Copy link
Collaborator

lukasmasuch commented Jun 27, 2025

I found this change has a degradation that the docstring of the cached function is not displayed in VSCode now.
Will see it further.

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 clear method. Both options are not ideal :(

@whitphx
Copy link
Contributor Author

whitphx commented Jun 28, 2025

It's discussed in microsoft/pylance-release#5840 for the case of Pylance/VSCode.
Looks like the docstring in auto-completion is an issue on IDE's side?

lukasmasuch pushed a commit that referenced this pull request Jul 2, 2025
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:other PR contains other type of change impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants