Skip to content

Commit

Permalink
Replace or with Union for types
Browse files Browse the repository at this point in the history
  • Loading branch information
Miauwkeru committed Feb 15, 2024
1 parent c5224b7 commit aa486ff
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ class ListUnwrapper:
"""Provides utility functions to unwrap dictionary objects out of lists."""

@staticmethod
def unwrap(data: dict | list) -> dict | list:
def unwrap(data: Union[dict, list]) -> Union[dict, list]:
"""Transforms a list with dictionaries to a dictionary.
The order of the list is preserved. If no dictionary is found,
The order of the list is preserved. If no dictionary is found,
the list remains untouched:
["value1", "value2"] -> ["value1", "value2"]
Expand All @@ -357,32 +357,29 @@ def unwrap(data: dict | list) -> dict | list:
}
}
"""
orig = ListUnwrapper._unwrap_value(data)
orig = ListUnwrapper._unwrap_dict_list(data)
return ListUnwrapper._unwrap_dict(orig)

@staticmethod
def _unwrap_dict(data: dict | list) -> dict | list:
def _unwrap_dict(data: Union[dict, list]) -> Union[dict, list]:
"""Looks for dictionaries and unwraps its values."""

if not isinstance(data, dict):
return data

Check warning on line 368 in dissect/target/helpers/configutil.py

View check run for this annotation

Codecov / codecov/patch

dissect/target/helpers/configutil.py#L368

Added line #L368 was not covered by tests

root = dict()
for key, value in data.items():
_value = ListUnwrapper._unwrap_value(value)
_value = ListUnwrapper._unwrap_dict_list(value)
if isinstance(_value, dict):
_value = ListUnwrapper._unwrap_dict(_value)
root[key] = _value

return root

@staticmethod
def _unwrap_value(data: list | dict) -> dict | list:
def _unwrap_dict_list(data: Union[dict, list]) -> Union[dict, list]:
"""Unwraps a list containing dictionaries."""
if not isinstance(data, list):
return data

if not any(isinstance(obj, dict) for obj in data):
if not isinstance(data, list) or not any(isinstance(obj, dict) for obj in data):
return data

return_value = {}
Expand Down

0 comments on commit aa486ff

Please sign in to comment.