Skip to content

Conversation

@raethlein
Copy link
Collaborator

Describe your changes

In streamlit/elements/utils.py we have some check_* functions that are used to enforce certain rules. This PR moves them to their own policies.py file for cleaner abstraction.
Also, tests for check_cache_replay_rules were missing that are added now.

This is in preparation of adding a new check_* policy function in this PR: https://github.com/streamlit/streamlit/pull/8756/files

GitHub Issue Link (if applicable)

Testing Plan

  • Unit Tests (JS and/or Python)
    • moved unittests and added new ones

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@raethlein raethlein requested a review from lukasmasuch May 24, 2024 13:41
@raethlein raethlein added security-assessment-completed Security assessment has been completed for PR impact:internal PR changes only affect internal code change:refactor PR contains code refactoring without behavior change labels May 24, 2024
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.

It seems like that the methods are not yet removed from utils.py or am I missing something. Also, it seems to not yet cover all relevant elements (e.g data editor is missing?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit annoyed by all the import cycle warnings. I believe this import might be the issue. Can we move this import down into the function from where it is used?

Copy link
Collaborator Author

@raethlein raethlein May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the circular imports at some places in the code base are pretty annoying. I have spent some time today looking into refactoring it and refactoring everything including the delta generator are on top of my list 😄 moving the imports down - if it works at all as it opened a rabbit hole earlier - just hides the original problem that the code is not pretty well abstracted. I guess eventually we have to start cleaning it up top down.

Copy link
Collaborator

@lukasmasuch lukasmasuch May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the imports down - if it works at all as it opened a rabbit hole earlier - just hides the original problem that the code is not pretty well abstracted

Yeah, I think the better solution would be to have some internal import for exception and warning, but I'm not sure if that is viable without bigger refactoring. But just having a blank streamlit import here on top doesn't feel super good. Overall, it's better to move this import down and all the other check_ imports to the top level.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since its a delta generator function, the best way would likely be to have to pass the deltagenerator to the function instead of importing a global object. But this surfaces other import issues at other places; so I think the first step has to be abstracting the delta generator and probably move out the main_dg and other *_dg variables and dg_stack out and work with a singleton factory or so. But that requires a little bit more time

@raethlein raethlein force-pushed the refactor-widget-checks-util branch from 73b59b9 to b5aad90 Compare May 26, 2024 10:59
@raethlein raethlein force-pushed the refactor-widget-checks-util branch from 94e6571 to 12ec5c3 Compare May 27, 2024 19:27
@raethlein
Copy link
Collaborator Author

@lukasmasuch could you please have another look? The CodeScan things look like a lot, but I have moved the imports within the policies.py file so that we can move the inner-imports in the different element files to the top. So the situation should be better than before 🙂

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 👍 I think it would be good to move policies and/or utils into the element/lib folder. These are the only two non-element modules within this folders, and it feels like element/lib would be a better place.

@raethlein raethlein force-pushed the refactor-widget-checks-util branch from 12ec5c3 to 18888e0 Compare May 28, 2024 05:55
@raethlein
Copy link
Collaborator Author

LGTM 👍 I think it would be good to move policies and/or utils into the element/lib folder. These are the only two non-element modules within this folders, and it feels like element/lib would be a better place.

Great point, I have moved both utils files

f'The widget with key "{key}" was created with a default value but'
" also had its value set via the Session State API."
)
_shown_default_value_warning = True

Check notice

Code scanning / CodeQL

Unused global variable

The global variable '_shown_default_value_warning' is not used.
Comment on lines +47 to +51
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +27 to +31
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +58 to +62
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +22 to +26
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
from typing import TYPE_CHECKING, Any

from streamlit import config, runtime
from streamlit.elements.form import is_in_form

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.form](1) begins an import cycle.
Comment on lines +22 to +26
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +24 to +28
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +21 to +25
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
Comment on lines +46 to +50
from streamlit.elements.lib.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

Check notice

Code scanning / CodeQL

Cyclic import

Import of module [streamlit.elements.lib.policies](1) begins an import cycle.
@raethlein raethlein merged commit 405be2d into develop May 28, 2024
@raethlein raethlein deleted the refactor-widget-checks-util branch August 8, 2024 14:07
benjamin-awd pushed a commit to benjamin-awd/streamlit that referenced this pull request Sep 29, 2024
## Describe your changes

In https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/utils.py
we have some check_* functions that are used to enforce certain rules.
This PR moves them to their own policies.py file for cleaner
abstraction.
Also, tests for check_cache_replay_rules were missing that are added
now.

This is in preparation of adding a new check_* policy function in this
PR: https://github.com/streamlit/streamlit/pull/8756/files

## GitHub Issue Link (if applicable)

## Testing Plan

- Unit Tests (JS and/or Python)
  - moved unittests and added new ones

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
edegp pushed a commit to edegp/streamlit that referenced this pull request Jan 19, 2025
## Describe your changes

In https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/utils.py
we have some check_* functions that are used to enforce certain rules.
This PR moves them to their own policies.py file for cleaner
abstraction.
Also, tests for check_cache_replay_rules were missing that are added
now.

This is in preparation of adding a new check_* policy function in this
PR: https://github.com/streamlit/streamlit/pull/8756/files

## GitHub Issue Link (if applicable)

## Testing Plan

- Unit Tests (JS and/or Python)
  - moved unittests and added new ones

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
asmeralt pushed a commit to asmeralt/streamlit that referenced this pull request Sep 29, 2025
## Describe your changes

In https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/utils.py
we have some check_* functions that are used to enforce certain rules.
This PR moves them to their own policies.py file for cleaner
abstraction.
Also, tests for check_cache_replay_rules were missing that are added
now.

This is in preparation of adding a new check_* policy function in this
PR: https://github.com/streamlit/streamlit/pull/8756/files

## GitHub Issue Link (if applicable)

## Testing Plan

- Unit Tests (JS and/or Python)
  - moved unittests and added new ones

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
sfc-gh-jkinkead added a commit that referenced this pull request Dec 22, 2025
Replace the TTLCache in ResourceCache with a TTLCleanupCache.

Add on_release to the st.cache_resource API, and plumb it through to the TTLCleanupCache.

Update st.cache_resource.clear_all to clear the cache directly instead of just GCing it to ensure release functions are called.

Implements feature request in #8764 .
sfc-gh-jkinkead added a commit that referenced this pull request Dec 24, 2025
## Describe your changes

Replace the TTLCache in ResourceCache with a TTLCleanupCache.

Add on_release to the st.cache_resource API, and plumb it through to the
TTLCleanupCache.

Update st.cache_resource.clear_all to clear the cache directly instead
of just GCing it to ensure release functions are called.

Implements feature request in #8764 .

## GitHub Issue Link (if applicable)

- Closes #8674

## Testing Plan

- Unit Tests (JS and/or Python)

Implemented.

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.

---------

Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:refactor PR contains code refactoring without behavior change impact:internal PR changes only affect internal code security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants