Skip to content

Commit

Permalink
Centralize circular-import workaround in policies.py
Browse files Browse the repository at this point in the history
  • Loading branch information
raethlein committed May 26, 2024
1 parent 2c0d7df commit cfba40e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
13 changes: 5 additions & 8 deletions lib/streamlit/elements/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
)
from streamlit.elements.lib.event_utils import AttributeDictionary
from streamlit.elements.lib.pandas_styler_utils import marshall_styler
from streamlit.elements.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.policies
begins an import cycle.
from streamlit.errors import StreamlitAPIException
from streamlit.proto.Arrow_pb2 import Arrow as ArrowProto
from streamlit.runtime.metrics_util import gather_metrics
Expand Down Expand Up @@ -476,14 +481,6 @@ def dataframe(

if is_selection_activated:
# Run some checks that are only relevant when selections are activated

# Import here to avoid circular imports
from streamlit.elements.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

check_cache_replay_rules()
if callable(on_select):
check_callback_rules(self.dg, on_select)
Expand Down
31 changes: 20 additions & 11 deletions lib/streamlit/elements/plotly_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
from streamlit.elements.lib.streamlit_plotly_theme import (
configure_streamlit_plotly_theme,
)
from streamlit.elements.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.policies
begins an import cycle.
from streamlit.errors import StreamlitAPIException
from streamlit.proto.PlotlyChart_pb2 import PlotlyChart as PlotlyChartProto
from streamlit.runtime.metrics_util import gather_metrics
Expand Down Expand Up @@ -274,7 +279,11 @@ def plotly_chart(
key: Key | None = None,
on_select: Literal["ignore"], # No default value here to make it work with mypy
selection_mode: SelectionMode
| Iterable[SelectionMode] = ("points", "box", "lasso"),
| Iterable[SelectionMode] = (
"points",
"box",
"lasso",
),
**kwargs: Any,
) -> DeltaGenerator:
...
Expand All @@ -289,7 +298,11 @@ def plotly_chart(
key: Key | None = None,
on_select: Literal["rerun"] | WidgetCallback = "rerun",
selection_mode: SelectionMode
| Iterable[SelectionMode] = ("points", "box", "lasso"),
| Iterable[SelectionMode] = (
"points",
"box",
"lasso",
),
**kwargs: Any,
) -> PlotlyState:
...
Expand All @@ -304,7 +317,11 @@ def plotly_chart(
key: Key | None = None,
on_select: Literal["rerun", "ignore"] | WidgetCallback = "ignore",
selection_mode: SelectionMode
| Iterable[SelectionMode] = ("points", "box", "lasso"),
| Iterable[SelectionMode] = (
"points",
"box",
"lasso",
),
**kwargs: Any,
) -> DeltaGenerator | PlotlyState:
"""Display an interactive Plotly chart.
Expand Down Expand Up @@ -450,14 +467,6 @@ def plotly_chart(

if is_selection_activated:
# Run some checks that are only relevant when selections are activated

# Import here to avoid circular imports
from streamlit.elements.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

check_cache_replay_rules()
if callable(on_select):
check_callback_rules(self.dg, on_select)
Expand Down
11 changes: 7 additions & 4 deletions lib/streamlit/elements/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from typing import TYPE_CHECKING, Any

import streamlit
from streamlit import config, runtime
from streamlit.elements.form import is_in_form
from streamlit.errors import StreamlitAPIException, StreamlitAPIWarning
Expand Down Expand Up @@ -77,7 +76,9 @@ def check_session_state_rules(
and not _shown_default_value_warning
and not config.get_option("global.disableWidgetStateDuplicationWarning")
):
streamlit.warning(
from streamlit import warning

warning(
f'The widget with key "{key}" was created with a default value but'
" also had its value set via the Session State API."
)
Expand Down Expand Up @@ -110,10 +111,12 @@ def check_cache_replay_rules() -> None:
function to check for those as well. And rename it to check_widget_usage_rules.
"""
if runtime.exists():
from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx
# from streamlit.runtime.scriptrunner.script_run_context import get_script_run_ctx

ctx = get_script_run_ctx()
if ctx and ctx.disallow_cached_widget_usage:
from streamlit import exception

# We use an exception here to show a proper stack trace
# that indicates to the user where the issue is.
streamlit.exception(CachedWidgetWarning())
exception(CachedWidgetWarning())
12 changes: 5 additions & 7 deletions lib/streamlit/elements/vega_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
generate_chart,
)
from streamlit.elements.lib.event_utils import AttributeDictionary
from streamlit.elements.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.policies
begins an import cycle.
from streamlit.errors import StreamlitAPIException
from streamlit.proto.ArrowVegaLiteChart_pb2 import (
ArrowVegaLiteChart as ArrowVegaLiteChartProto,
Expand Down Expand Up @@ -1651,13 +1656,6 @@ def _vega_lite_chart(
if is_selection_activated:
# Run some checks that are only relevant when selections are activated

# Import here to avoid circular imports
from streamlit.elements.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

check_cache_replay_rules()
if callable(on_select):
check_callback_rules(self.dg, on_select)
Expand Down
19 changes: 5 additions & 14 deletions lib/streamlit/elements/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

from streamlit import runtime, source_util
from streamlit.elements.form import current_form_id, is_in_form
from streamlit.elements.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.policies
begins an import cycle.
from streamlit.errors import StreamlitAPIException
from streamlit.file_util import get_main_script_directory, normalize_path_join
from streamlit.proto.Button_pb2 import Button as ButtonProto
Expand Down Expand Up @@ -574,13 +579,6 @@ def _download_button(
) -> bool:
key = to_key(key)

# Importing these functions here to avoid circular imports
from streamlit.elements.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

check_cache_replay_rules()
check_session_state_rules(default_value=None, key=key, writes_allowed=False)
check_callback_rules(self.dg, on_click)
Expand Down Expand Up @@ -738,13 +736,6 @@ def _button(
) -> bool:
key = to_key(key)

# Importing these functions here to avoid circular imports
from streamlit.elements.policies import (
check_cache_replay_rules,
check_callback_rules,
check_session_state_rules,
)

if not is_form_submitter:
check_callback_rules(self.dg, on_click)
check_cache_replay_rules()
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/streamlit/elements/element_policies_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_cache_replay_rules_succeeds(self, patched_st_exception):

@patch("streamlit.runtime.Runtime.exists", MagicMock(return_value=True))
@patch(
"streamlit.runtime.scriptrunner.script_run_context.get_script_run_ctx",
"streamlit.elements.policies.get_script_run_ctx",
MagicMock(return_value=MagicMock(disallow_cached_widget_usage=True)),
)
@patch("streamlit.exception")
Expand Down

0 comments on commit cfba40e

Please sign in to comment.