Skip to content

Commit

Permalink
misc: make types to work on 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
nichmor committed Jul 22, 2024
1 parent 83dc283 commit 01377d0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
25 changes: 1 addition & 24 deletions src/rattler_build_conda_compat/loader.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

import itertools
from contextlib import contextmanager
from typing import TYPE_CHECKING, Any

import yaml

from rattler_build_conda_compat.conditional_list import visit_conditional_list
from rattler_build_conda_compat.utils import flatten_lists, remove_empty_keys

if TYPE_CHECKING:
from collections.abc import Iterator
Expand Down Expand Up @@ -105,29 +105,6 @@ def load_yaml(content: str | bytes) -> Any: # noqa: ANN401
return yaml.load(content, Loader=yaml.BaseLoader) # noqa: S506


def remove_empty_keys(variant_dict: dict[str, Any]) -> dict[str, Any]:
filtered_dict = {}
for key, value in variant_dict.items():
if isinstance(value, list) and len(value) == 0:
continue
filtered_dict[key] = value

return filtered_dict


def flatten_lists(variant_dict: dict[str, Any]) -> dict[str, Any]:
result_dict: dict[str, Any] = {}
for key, value in variant_dict.items():
if isinstance(value, dict):
result_dict[key] = flatten_lists(value)
elif isinstance(value, list) and value and isinstance(value[0], list):
result_dict[key] = list(itertools.chain(*value))
else:
result_dict[key] = value

return result_dict


def parse_recipe_config_file(
path: PathLike[str], namespace: dict[str, Any] | None, *, allow_missing_selector: bool = False
) -> dict[str, Any]:
Expand Down
26 changes: 25 additions & 1 deletion src/rattler_build_conda_compat/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fnmatch
import itertools
from logging import getLogger
import os
from pathlib import Path
from typing import Iterable
from typing import Any, Dict, Iterable


VALID_METAS = ("recipe.yaml",)
Expand Down Expand Up @@ -171,3 +172,26 @@ def has_recipe(recipe_dir: Path) -> bool:
return False
except OSError:
return False


def remove_empty_keys(some_dict: Dict[str, Any]) -> Dict[str, Any]:
filtered_dict = {}
for key, value in some_dict.items():
if isinstance(value, list) and len(value) == 0:
continue
filtered_dict[key] = value

return filtered_dict


def flatten_lists(some_dict: Dict[str, Any]) -> Dict[str, Any]:
result_dict: Dict[str, Any] = {}
for key, value in some_dict.items():
if isinstance(value, dict):
result_dict[key] = flatten_lists(value)
elif isinstance(value, list) and value and isinstance(value[0], list):
result_dict[key] = list(itertools.chain(*value))
else:
result_dict[key] = value

return result_dict

0 comments on commit 01377d0

Please sign in to comment.