From 66b6e493b084be1d4dd8c65c77c53807bcedb8ae Mon Sep 17 00:00:00 2001 From: Felix Claessen <30658763+Flix6x@users.noreply.github.com> Date: Fri, 22 Apr 2022 12:44:11 +0200 Subject: [PATCH] Allow passing a tuple of sources (#421) Allow passing a tuple of sources, while modernizing type annotations for a small module. * Allow passing tuple of sources, while modernizing type annotations and expanding docstring Signed-off-by: F.N. Claessen * flake8 Signed-off-by: F.N. Claessen * Changelog entry Signed-off-by: F.N. Claessen * Refactor import Signed-off-by: F.N. Claessen --- documentation/changelog.rst | 1 + flexmeasures/data/models/parsing_utils.py | 26 ++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/documentation/changelog.rst b/documentation/changelog.rst index a8cb0596f..d29556cb5 100644 --- a/documentation/changelog.rst +++ b/documentation/changelog.rst @@ -17,6 +17,7 @@ Infrastructure / Support ---------------------- * Unit conversion prefers shorter units in general [see `PR #415 `_] * Shorter CI builds in Github Actions by caching Python environment [see `PR #361 `_] +* Allow to filter data by source using a tuple instead of a list [see `PR #421 `_] v0.9.3 | April 15, 2022 diff --git a/flexmeasures/data/models/parsing_utils.py b/flexmeasures/data/models/parsing_utils.py index 86f0b07a3..19bcb03a1 100644 --- a/flexmeasures/data/models/parsing_utils.py +++ b/flexmeasures/data/models/parsing_utils.py @@ -1,4 +1,6 @@ -from typing import Optional, Union, List +from __future__ import annotations + +from collections.abc import Sequence from flask import current_app from flexmeasures.data import db @@ -7,18 +9,26 @@ def parse_source_arg( - source: Optional[ - Union[DataSource, List[DataSource], int, List[int], str, List[str]] - ] -) -> Optional[List[DataSource]]: - """Parse the "source" argument by looking up DataSources corresponding to any given ids or names.""" + source: DataSource + | int + | str + | Sequence[DataSource] + | Sequence[int] + | Sequence[str] + | None, +) -> list[DataSource] | None: + """Parse the "source" argument by looking up DataSources corresponding to any given ids or names. + + Passes None as is (i.e. no source argument is given). + Accepts ids and names as list or tuples, always converting them to a list. + """ if source is None: return source - if not isinstance(source, list): + if isinstance(source, (DataSource, str, int)): sources = [source] else: sources = source - parsed_sources: List[DataSource] = [] + parsed_sources: list[DataSource] = [] for source in sources: if isinstance(source, int): parsed_source = (