Skip to content

Commit

Permalink
Allow passing a tuple of sources (#421)
Browse files Browse the repository at this point in the history
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 <felix@seita.nl>

* flake8

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Refactor import

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Apr 22, 2022
1 parent 9207b15 commit 66b6e49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Expand Up @@ -17,6 +17,7 @@ Infrastructure / Support
----------------------
* Unit conversion prefers shorter units in general [see `PR #415 <http://www.github.com/FlexMeasures/flexmeasures/pull/415>`_]
* Shorter CI builds in Github Actions by caching Python environment [see `PR #361 <http://www.github.com/FlexMeasures/flexmeasures/pull/361>`_]
* Allow to filter data by source using a tuple instead of a list [see `PR #421 <http://www.github.com/FlexMeasures/flexmeasures/pull/421>`_]


v0.9.3 | April 15, 2022
Expand Down
26 changes: 18 additions & 8 deletions 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
Expand All @@ -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 = (
Expand Down

0 comments on commit 66b6e49

Please sign in to comment.