-
Notifications
You must be signed in to change notification settings - Fork 4k
Add explicit support for Pydantic models in cache_data/resource #12137
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
Conversation
✅ PR preview is ready!
|
🎉 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) |
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
|
|
||
| def test_pydantic_model(self): | ||
| """Test that Pydantic models are properly hashed. | ||
|
|
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.
There's a trailing whitespace at the end of this line in the docstring. Consider removing it for consistency.
Spotted by Diamond
Is this helpful? React 👍 or 👎 to let us know.
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 adds explicit support for Pydantic models in Streamlit's caching system by allowing them to be hashed without requiring custom hash_func parameters. This improves the user experience by removing the need for manual hash function configuration when caching functions that accept Pydantic models.
Key Changes:
- Added Pydantic model detection and hashing logic in
hashing.py - Comprehensive test coverage for Pydantic model hashing scenarios
- Added Pydantic as a test dependency
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
lib/streamlit/runtime/caching/hashing.py |
Implements Pydantic model detection and JSON-based hashing logic |
lib/tests/streamlit/runtime/caching/hashing_test.py |
Adds comprehensive tests for Pydantic model hashing behavior |
lib/test-requirements.txt |
Adds Pydantic as a test dependency |
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 👍
One nit: pydantic is currently part of our integration unit tests. But I think its also fine to run this as part of our main unit tests since pydantic is very common as dependency. Two options:
- Add
@pytest.mark.require_integrationto the unit test and remove pydantic fromtest-requirements.txt. Might be a bit easier to do since ourpy-min-deps-testcomplains -> which is probably related to pydantic 2.0 being installed during the test. - Remove pydantic from
integration-requirements.txtand remove the@pytest.mark.require_integrationfromtest_is_pydantic_modeltest.
I think you also have to run ruff formatting, or just make autofix.
Co-authored-by: Copilot <[email protected]>
Describe your changes
Today, when you pass a Pydantic model to a cached function, you need to pass an explicit
hash_funcfor it to be handled correctly.With this change we now special-case Pydantic inside
hashing.pyto convert them to JSON, sohash_funcsaren't needed.GitHub Issue Link (if applicable)
#10348
Testing Plan
Explanation of why no additional tests are neededE2E TestsAny 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.