Closes #187 and improves documentation
Some checks failed
Analysis / SonarCloud (pull_request) Failing after 5s
Tests / Build and Test (3.11) (pull_request) Successful in 1m16s
Tests / Build and Test (3.12) (pull_request) Successful in 1m9s
Tests / Build and Test (3.13) (pull_request) Successful in 1m7s

This commit is contained in:
2025-02-09 18:40:30 +01:00
parent ad70648ea2
commit 6b2be48052
2 changed files with 25 additions and 23 deletions

View File

@@ -3,11 +3,11 @@ from typing import Any, Dict
from typing import Callable
def supports_argument(func: Callable, arg_name: str) -> bool:
def supports_argument(func: Callable[..., Any], arg_name: str) -> bool:
"""Check whether a function has a specific argument
### Args:
* func (`Callable`): Function to be inspected
* func (`Callable[..., Any]`): Function to be inspected
* arg_name (`str`): Argument to be checked
### Returns:
@@ -24,11 +24,13 @@ def supports_argument(func: Callable, arg_name: str) -> bool:
return False
def nested_set(target: dict, value: Any, *path: str, create_missing=True) -> Dict[str, Any]:
def nested_set(
target: Dict[str, Any], value: Any, *path: str, create_missing: bool = True
) -> Dict[str, Any]:
"""Set the key by its path to the value
### Args:
* target (`dict`): Dictionary to perform modifications on
* target (`Dict[str, Any]`): Dictionary to perform modifications on
* value (`Any`): Any data
* *path (`str`): Path to the key of the target
* create_missing (`bool`, *optional*): Create keys on the way if they're missing. Defaults to `True`
@@ -39,29 +41,29 @@ def nested_set(target: dict, value: Any, *path: str, create_missing=True) -> Dic
### Returns:
* `Dict[str, Any]`: Changed dictionary
"""
d = target
target_copy: Dict[str, Any] = target
for key in path[:-1]:
if key in d:
d = d[key]
if key in target_copy:
target_copy = target_copy[key]
elif create_missing:
d = d.setdefault(key, {})
target_copy = target_copy.setdefault(key, {})
else:
raise KeyError(
f"Key '{key}' is not found under path provided ({path}) and create_missing is False"
)
if path[-1] in d or create_missing:
d[path[-1]] = value
if path[-1] in target_copy or create_missing:
target_copy[path[-1]] = value
return target
def nested_delete(target: dict, *path: str) -> Dict[str, Any]:
def nested_delete(target: Dict[str, Any], *path: str) -> Dict[str, Any]:
"""Delete the key by its path
### Args:
* target (`dict`): Dictionary to perform modifications on
* target (`Dict[str, Any]`): Dictionary to perform modifications on
### Raises:
* `KeyError`: Key is not found under path provided
@@ -69,16 +71,16 @@ def nested_delete(target: dict, *path: str) -> Dict[str, Any]:
### Returns:
`Dict[str, Any]`: Changed dictionary
"""
d = target
target_copy: Dict[str, Any] = target
for key in path[:-1]:
if key in d:
d = d[key]
if key in target_copy:
target_copy = target_copy[key]
else:
raise KeyError(f"Key '{key}' is not found under path provided ({path})")
if path[-1] in d:
del d[path[-1]]
if path[-1] in target_copy:
del target_copy[path[-1]]
else:
raise KeyError(f"Key '{path[-1]}' is not found under path provided ({path})")