Skip to content

Commit

Permalink
Fix removal of empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Aug 14, 2024
1 parent 79d7d2d commit 8f99200
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion click_extra/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def merge_default_map(self, ctx: Context, user_conf: dict) -> None:

# Clean-up the conf by removing all blank values left-over by the template
# structure.
clean_conf = remove_blanks(filtered_conf)
clean_conf = remove_blanks(filtered_conf, remove_str=False)

# Update the default_map.
if ctx.default_map is None:
Expand Down
31 changes: 23 additions & 8 deletions click_extra/platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,32 @@ def recursive_update(
return a


def remove_blanks(tree: dict) -> dict:
"""Returns a copy of a dict without items whose values are `None` or empty `dict`.
Works recusively.
def remove_blanks(
tree: dict,
remove_none: bool = True,
remove_dicts: bool = True,
remove_str: bool = True,
) -> dict:
"""Returns a copy of a dict without items whose values blanks.
Are considered blanks:
- `None` values
- empty strings
- empty `dict`
The removal of each of these class can be skipped by setting ``remove_*``
parameters.
Dictionarries are inspected recusively and their own blank values are removed.

Check warning on line 199 in click_extra/platforms.py

View workflow job for this annotation

GitHub Actions / docs / Fix typos

"recusively" should be "recursively" or "reclusively".
"""

def visit(path, key, value) -> bool:
"""Skip `None` values and empty `dict`."""
if value is None:
"""Ignore some class of blank values depending on configuration."""
if remove_none and value is None:
return False
if remove_dicts and isinstance(value, dict) and not len(value):
return False
if isinstance(value, dict) and not len(value):
if remove_str and isinstance(value, str) and not len(value):
return False
return True

Expand Down Expand Up @@ -232,7 +247,7 @@ def info(self) -> dict[str, str | None | dict[str, str | None]]:
"like": None,
"codename": None,
}
# Get that extra info from distro.
# Get extra info from distro.
if distro.id() == self.id:
cleaned_info = remove_blanks(distro.info())
info = recursive_update(info, cleaned_info)
Expand Down

0 comments on commit 8f99200

Please sign in to comment.