Closes #187 and improves documentation
This commit is contained in:
@@ -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})")
|
||||
|
||||
|
Reference in New Issue
Block a user