From e6208faa19aa1b2fad6ae6d83f73d0b800effad0 Mon Sep 17 00:00:00 2001 From: petar-qb Date: Mon, 25 Nov 2024 08:44:33 +0100 Subject: [PATCH] Lint --- vizro-core/examples/scratch_dev/data.yaml | 2 +- .../src/vizro/models/_controls/filter.py | 16 +- .../tests/unit/vizro/actions/conftest.py | 1 - .../vizro/models/_controls/test_filter.py | 164 ++++++++++-------- 4 files changed, 100 insertions(+), 83 deletions(-) diff --git a/vizro-core/examples/scratch_dev/data.yaml b/vizro-core/examples/scratch_dev/data.yaml index d8b0aea90..63e64b40d 100644 --- a/vizro-core/examples/scratch_dev/data.yaml +++ b/vizro-core/examples/scratch_dev/data.yaml @@ -1,6 +1,6 @@ # Choose between 0-50 setosa: 5 -versicolor: 10 +#versicolor: 10 virginica: 15 # Choose between: 4.3 to 7.4 diff --git a/vizro-core/src/vizro/models/_controls/filter.py b/vizro-core/src/vizro/models/_controls/filter.py index c70e7032b..8920dce34 100644 --- a/vizro-core/src/vizro/models/_controls/filter.py +++ b/vizro-core/src/vizro/models/_controls/filter.py @@ -1,11 +1,10 @@ from __future__ import annotations +from contextlib import suppress from typing import Any, Literal, Union -import numpy as np import pandas as pd from dash import dcc -from contextlib import suppress from pandas.api.types import is_datetime64_any_dtype, is_numeric_dtype from vizro.managers._data_manager import DataSourceName @@ -275,13 +274,16 @@ def _get_min_max(targeted_data: pd.DataFrame, current_value=None) -> tuple[float _max = pd.to_datetime(_max) current_value = pd.to_datetime(current_value) # Convert DatetimeIndex to list of Timestamp objects so that we can use min and max functions below. - with suppress(AttributeError): current_value = current_value.tolist() + with suppress(AttributeError): + current_value = current_value.tolist() # Use item() to convert to convert scalar from numpy to Python type. This isn't needed during pre_build because # pydantic will coerce the type, but it is necessary in __call__ where we don't update model field values # and instead just pass straight to the Dash component. - with suppress(AttributeError): _min = _min.item() - with suppress(AttributeError): _max = _max.item() + with suppress(AttributeError): + _min = _min.item() + with suppress(AttributeError): + _max = _max.item() if current_value is not None: current_value = current_value if isinstance(current_value, list) else [current_value] @@ -305,7 +307,7 @@ def _get_options(targeted_data: pd.DataFrame, current_value=None) -> list[Any]: if isinstance(current_value, list) and ALL_OPTION in current_value: current_value.remove(ALL_OPTION) - targeted_data_series = targeted_data.stack().dropna() + targeted_data_series = targeted_data.stack().dropna() # noqa: PD013 current_value_series = pd.Series(current_value).astype(targeted_data_series.dtypes) - return sorted(list(pd.concat([targeted_data_series, current_value_series]).unique())) + return sorted(pd.concat([targeted_data_series, current_value_series]).unique()) diff --git a/vizro-core/tests/unit/vizro/actions/conftest.py b/vizro-core/tests/unit/vizro/actions/conftest.py index 6833fa0d9..3b7bc5337 100644 --- a/vizro-core/tests/unit/vizro/actions/conftest.py +++ b/vizro-core/tests/unit/vizro/actions/conftest.py @@ -1,4 +1,3 @@ -import pandas as pd import pytest import vizro.models as vm diff --git a/vizro-core/tests/unit/vizro/models/_controls/test_filter.py b/vizro-core/tests/unit/vizro/models/_controls/test_filter.py index 04d0004c0..6f4f5550d 100644 --- a/vizro-core/tests/unit/vizro/models/_controls/test_filter.py +++ b/vizro-core/tests/unit/vizro/models/_controls/test_filter.py @@ -1,16 +1,15 @@ from datetime import date, datetime from typing import Literal -from dash import dcc import pandas as pd import pytest from asserts import assert_component_equal +from dash import dcc import vizro.models as vm import vizro.plotly.express as px from vizro import Vizro from vizro.managers import data_manager, model_manager -from vizro.models import DatePicker from vizro.models._action._actions_chain import ActionsChain from vizro.models._controls.filter import Filter, _filter_between, _filter_isin from vizro.models.types import CapturedCallable @@ -231,18 +230,22 @@ class TestFilterStaticMethods: ([["A", "B", "A"]], ["A", "B"]), ([[1, 2, 1]], [1, 2]), ([[1.1, 2.2, 1.1]], [1.1, 2.2]), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 1), - ]], - [ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - ]), + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 1), + ] + ], + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + ], + ), ([["A", "B"], ["B", "C"]], ["A", "B", "C"]), ([["A", "B"], ["C"]], ["A", "B", "C"]), - ([["A" ], []], ["A" ]), + ([["A"], []], ["A"]), ], ) def test_get_options(self, data_columns, expected): @@ -264,30 +267,38 @@ def test_get_options(self, data_columns, expected): ([[1, 2]], [3, 4], [1, 2, 3, 4]), ([[1.1, 2.2]], 3.3, [1.1, 2.2, 3.3]), ([[1.1, 2.2]], [3.3, 4.4], [1.1, 2.2, 3.3, 4.4]), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - ]], - datetime(2024, 1, 3), - [ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 3), - ]), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - ]], - [ + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + ] + ], datetime(2024, 1, 3), - datetime(2024, 1, 4), - ], - [ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 3), - datetime(2024, 1, 4), - ]), + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 3), + ], + ), + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + ] + ], + [ + datetime(2024, 1, 3), + datetime(2024, 1, 4), + ], + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 3), + datetime(2024, 1, 4), + ], + ), ], ) def test_get_options_with_current_value(self, data_columns, current_value, expected): @@ -300,15 +311,19 @@ def test_get_options_with_current_value(self, data_columns, current_value, expec [ ([[1, 2, 1]], (1, 2)), ([[1.1, 2.2, 1.1]], (1.1, 2.2)), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 1), - ]], - ( - datetime(2024, 1, 1), - datetime(2024, 1, 2), - )), + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 1), + ] + ], + ( + datetime(2024, 1, 1), + datetime(2024, 1, 2), + ), + ), ([[1, 2], [2, 3]], (1, 3)), ([[1, 2], [3]], (1, 3)), ([[1, 2], []], (1, 2)), @@ -326,29 +341,36 @@ def test_get_min_max(self, data_columns, expected): ([[1, 2]], [3, 4], (1, 4)), ([[1.1, 2.2]], 3.3, (1.1, 3.3)), ([[1.1, 2.2]], [3.3, 4.4], (1.1, 4.4)), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 1), - ]], - datetime(2024, 1, 3), - ( - datetime(2024, 1, 1), - datetime(2024, 1, 3), - )), - ([[ - datetime(2024, 1, 1), - datetime(2024, 1, 2), - datetime(2024, 1, 1), - ]], - [ + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 1), + ] + ], datetime(2024, 1, 3), - datetime(2024, 1, 4), - ], - ( - datetime(2024, 1, 1), - datetime(2024, 1, 4), - ) + ( + datetime(2024, 1, 1), + datetime(2024, 1, 3), + ), + ), + ( + [ + [ + datetime(2024, 1, 1), + datetime(2024, 1, 2), + datetime(2024, 1, 1), + ] + ], + [ + datetime(2024, 1, 3), + datetime(2024, 1, 4), + ], + ( + datetime(2024, 1, 1), + datetime(2024, 1, 4), + ), ), ([[1, 2], [2, 3]], 4, (1, 4)), ([[1, 2], [2, 3]], [4, 5], (1, 5)), @@ -552,10 +574,7 @@ def test_filter_is_not_dynamic(self): ], ) def test_filter_is_dynamic_with_dynamic_selectors( - self, - test_column, - test_selector, - gapminder_dynamic_first_n_last_n_function + self, test_column, test_selector, gapminder_dynamic_first_n_last_n_function ): data_manager["gapminder_dynamic_first_n_last_n"] = gapminder_dynamic_first_n_last_n_function filter = vm.Filter(column=test_column, selector=test_selector) @@ -590,10 +609,7 @@ def test_filter_is_not_dynamic_with_non_dynamic_selectors(self, gapminder_dynami ], ) def test_filter_is_not_dynamic_with_options_min_max_specified( - self, - test_column, - test_selector, - gapminder_dynamic_first_n_last_n_function + self, test_column, test_selector, gapminder_dynamic_first_n_last_n_function ): data_manager["gapminder_dynamic_first_n_last_n"] = gapminder_dynamic_first_n_last_n_function filter = vm.Filter(column=test_column, selector=test_selector)