Skip to content
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

Move check_* utils to own file #8764

Merged
merged 7 commits into from
May 28, 2024
Merged

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.

lib/streamlit/elements/policies.py Fixed Show fixed Hide fixed
lib/streamlit/elements/widgets/checkbox.py Fixed Show fixed Hide fixed
lib/streamlit/elements/widgets/radio.py Fixed Show fixed Hide fixed
lib/streamlit/elements/widgets/slider.py Fixed Show fixed Hide fixed
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?)


from typing import TYPE_CHECKING, Any

import streamlit
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

lib/streamlit/elements/policies.py Outdated Show resolved Hide resolved
lib/streamlit/elements/policies.py Outdated Show resolved Hide resolved
@raethlein raethlein force-pushed the refactor-widget-checks-util branch from 73b59b9 to b5aad90 Compare May 26, 2024 10:59
lib/streamlit/elements/policies.py Fixed Show fixed Hide fixed
lib/streamlit/elements/policies.py Fixed Show fixed Hide fixed
lib/streamlit/elements/utils.py Fixed Show fixed Hide fixed
lib/streamlit/elements/utils.py Fixed Show fixed Hide fixed
lib/streamlit/elements/utils.py Fixed Show fixed Hide fixed
lib/streamlit/elements/utils.py Fixed Show fixed Hide fixed
lib/streamlit/elements/arrow.py Fixed Show fixed Hide fixed
lib/streamlit/elements/widgets/button.py Fixed Show fixed Hide fixed
lib/streamlit/elements/plotly_chart.py Fixed Show fixed Hide fixed
lib/streamlit/elements/vega_charts.py Fixed Show fixed Hide fixed
@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

lib/streamlit/elements/lib/policies.py Dismissed Show dismissed Hide dismissed
lib/streamlit/elements/arrow.py Dismissed Show dismissed Hide dismissed
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 Note

Import of module
streamlit.elements.lib.policies
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 Note

Import of module
streamlit.elements.lib.policies
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 Note

Import of module
streamlit.elements.lib.policies
begins an import cycle.
lib/streamlit/elements/lib/policies.py Dismissed Show dismissed Hide dismissed
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 Note

Import of module
streamlit.elements.lib.policies
begins an import cycle.
lib/streamlit/elements/widgets/select_slider.py Dismissed Show dismissed Hide dismissed
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 Note

Import of module
streamlit.elements.lib.policies
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 Note

Import of module
streamlit.elements.lib.policies
begins an import cycle.
@raethlein raethlein merged commit 405be2d into develop May 28, 2024
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants