From b92bcdddfb4b01918f655d1f5cd895feeae239af Mon Sep 17 00:00:00 2001 From: Antony Milne Date: Mon, 2 Dec 2024 17:28:16 +0000 Subject: [PATCH] Lint --- vizro-core/src/vizro/_vizro.py | 4 ++-- .../_callback_mapping/_callback_mapping_utils.py | 10 ++++++++-- vizro-core/src/vizro/managers/_model_manager.py | 6 +++--- vizro-core/src/vizro/models/_controls/filter.py | 7 +++++-- .../src/vizro/models/_navigation/_navigation_utils.py | 4 ++-- vizro-core/src/vizro/models/_page.py | 4 +++- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/vizro-core/src/vizro/_vizro.py b/vizro-core/src/vizro/_vizro.py index ad530be00..f648d053f 100644 --- a/vizro-core/src/vizro/_vizro.py +++ b/vizro-core/src/vizro/_vizro.py @@ -5,7 +5,7 @@ from collections.abc import Iterable from contextlib import suppress from pathlib import Path, PurePosixPath -from typing import TYPE_CHECKING, TypedDict +from typing import TYPE_CHECKING, TypedDict, cast import dash import plotly.io as pio @@ -145,7 +145,7 @@ def _pre_build(): # Any models that are created during the pre-build process *will not* themselves have pre_build run on them. # In future may add a second pre_build loop after the first one. - for filter in model_manager._get_models(Filter): + for filter in cast(Iterable[Filter], model_manager._get_models(Filter)): # Run pre_build on all filters first, then on all other models. This handles dependency between Filter # and Page pre_build and ensures that filters are pre-built before the Page objects that use them. # This is important because the Page pre_build method checks whether filters are dynamic or not, which is diff --git a/vizro-core/src/vizro/actions/_callback_mapping/_callback_mapping_utils.py b/vizro-core/src/vizro/actions/_callback_mapping/_callback_mapping_utils.py index f275f570d..ba18e4fa5 100644 --- a/vizro-core/src/vizro/actions/_callback_mapping/_callback_mapping_utils.py +++ b/vizro-core/src/vizro/actions/_callback_mapping/_callback_mapping_utils.py @@ -117,7 +117,10 @@ def _get_export_data_callback_outputs(action: Action) -> dict[str, Output]: targets = None targets = targets or [ - model.id for model in model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(action)) + model.id + for model in cast( + Iterable[VizroBaseModel], model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(action)) + ) ] return { @@ -138,7 +141,10 @@ def _get_export_data_callback_components(action: Action) -> list[dcc.Download]: targets = None targets = targets or [ - model.id for model in model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(action)) + model.id + for model in cast( + Iterable[VizroBaseModel], model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(action)) + ) ] return [ diff --git a/vizro-core/src/vizro/managers/_model_manager.py b/vizro-core/src/vizro/managers/_model_manager.py index b260eb111..fe19a1e11 100644 --- a/vizro-core/src/vizro/managers/_model_manager.py +++ b/vizro-core/src/vizro/managers/_model_manager.py @@ -4,8 +4,8 @@ import random import uuid -from collections.abc import Generator -from typing import TYPE_CHECKING, NewType, Optional, TypeVar, Union +from collections.abc import Generator, Iterable +from typing import TYPE_CHECKING, NewType, Optional, TypeVar, Union, cast from vizro.managers._managers_utils import _state_modifier @@ -119,7 +119,7 @@ def _get_model_page(self, model: Model) -> Page: # type: ignore[return] if isinstance(model, Page): return model - for page in self._get_models(Page): + for page in cast(Iterable[Page], self._get_models(Page)): if model in self.__get_model_children(page): return page diff --git a/vizro-core/src/vizro/models/_controls/filter.py b/vizro-core/src/vizro/models/_controls/filter.py index 93e43b24f..167c93d3c 100644 --- a/vizro-core/src/vizro/models/_controls/filter.py +++ b/vizro-core/src/vizro/models/_controls/filter.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Literal, Union +from typing import Any, Literal, Union, cast, Iterable import pandas as pd from dash import dcc @@ -137,7 +137,10 @@ def pre_build(self): # This is the case when bool(self.targets) is False. # Possibly in future this will change (which would be breaking change). proposed_targets = self.targets or [ - model.id for model in model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(self)) + cast(ModelID, model.id) + for model in cast( + Iterable[VizroBaseModel], model_manager._get_models(FIGURE_MODELS, model_manager._get_model_page(self)) + ) ] # TODO: Currently dynamic data functions require a default value for every argument. Even when there is a # dataframe parameter, the default value is used when pre-build the filter e.g. to find the targets, diff --git a/vizro-core/src/vizro/models/_navigation/_navigation_utils.py b/vizro-core/src/vizro/models/_navigation/_navigation_utils.py index 6341a54ec..08c3d5849 100644 --- a/vizro-core/src/vizro/models/_navigation/_navigation_utils.py +++ b/vizro-core/src/vizro/models/_navigation/_navigation_utils.py @@ -1,7 +1,7 @@ from __future__ import annotations import itertools -from typing import TypedDict +from typing import TypedDict, cast, Iterable import dash_bootstrap_components as dbc @@ -15,7 +15,7 @@ def _validate_pages(pages): pages_as_list = list(itertools.chain(*pages.values())) if isinstance(pages, dict) else pages # Ideally we would use dashboard.pages in the model manager here, but we only register pages in # dashboard.pre_build and model manager cannot find a Dashboard at validation time. - registered_pages = [page.id for page in model_manager._get_models(Page)] + registered_pages = [page.id for page in cast(Iterable[Page], model_manager._get_models(Page))] if not pages_as_list: raise ValueError("Ensure this value has at least 1 item.") diff --git a/vizro-core/src/vizro/models/_page.py b/vizro-core/src/vizro/models/_page.py index 75d2d22cd..e137987bb 100644 --- a/vizro-core/src/vizro/models/_page.py +++ b/vizro-core/src/vizro/models/_page.py @@ -96,7 +96,9 @@ def __vizro_exclude_fields__(self) -> Optional[Union[set[str], Mapping[str, Any] @_log_call def pre_build(self): - figure_targets = [model.id for model in model_manager._get_models(FIGURE_MODELS, page=self)] + figure_targets = [ + model.id for model in cast(Iterable[VizroBaseModel], model_manager._get_models(FIGURE_MODELS, page=self)) + ] filter_targets = [ filter.id for filter in cast(Iterable[Filter], model_manager._get_models(Filter, page=self))