diff --git a/optuna_dashboard/_custom_plot_data.py b/optuna_dashboard/_custom_plot_data.py index a88fc0a4..a4dfa4af 100644 --- a/optuna_dashboard/_custom_plot_data.py +++ b/optuna_dashboard/_custom_plot_data.py @@ -53,7 +53,7 @@ def save_plotly_graph_object( Returns: The graph object ID. """ - if graph_object_id is not None and not is_valid_html_name(graph_object_id): + if graph_object_id is not None and not is_valid_graph_object_id(graph_object_id): raise ValueError("graph_object_id must be a valid HTML id attribute value.") storage = study._storage @@ -119,18 +119,16 @@ def concat_plot_data(plot_data_attrs: dict[str, str], key_prefix: str) -> str: return "".join(plot_data_attrs[f"{key_prefix}{i}"] for i in range(len(plot_data_attrs))) -def is_valid_html_name(graph_object_id: str) -> bool: +def is_valid_graph_object_id(graph_object_id: str) -> bool: if len(graph_object_id) == 0: return False - # Must begin with a letter [A-Za-z] - if not ("a" <= graph_object_id[0] <= "z" or "A" <= graph_object_id[0] <= "Z"): - return False - - # Can only contain letters [A-Za-z], numbers [0-9], hyphens ("-"), underscores ("_"), colons, and periods. + # Can only contain letters [A-Za-z], numbers [0-9], hyphens ("-"), underscores ("_"), + # colons, and periods. if not all( "a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9" or c in ("-", "_", ":", ".") for c in graph_object_id[1:] ): return False + # Unlike HTML id attribute, graph object id can begin with a letter [A-Za-z] return True diff --git a/python_tests/test_custom_plot_data.py b/python_tests/test_custom_plot_data.py index f73f7097..3dcfc856 100644 --- a/python_tests/test_custom_plot_data.py +++ b/python_tests/test_custom_plot_data.py @@ -65,22 +65,22 @@ def test_update_plotly_graph_object() -> None: @pytest.mark.parametrize( "name", [ + "0", "a", "a1-:_.", ], ) -def test_is_valid_html_name(name): - assert custom_plot_data.is_valid_html_name(name) +def test_is_valid_graph_object_id(name: str) -> None: + assert custom_plot_data.is_valid_graph_object_id(name) @pytest.mark.parametrize( "name", [ - "0", "a,", "a b", "aあいうえお", ], ) -def test_is_invalid_html_name(name): - assert not custom_plot_data.is_valid_html_name(name) +def test_is_invalid_graph_object_id(name: str) -> None: + assert not custom_plot_data.is_valid_graph_object_id(name)