Support more types for freezing
This commit is contained in:
parent
c8fbced859
commit
8f1418c346
1 changed files with 27 additions and 6 deletions
|
|
@ -1,12 +1,22 @@
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any, Callable, Dict, List
|
from typing import Any, Callable, Dict, List, Set, Union
|
||||||
|
|
||||||
|
|
||||||
class FrozenDict(dict):
|
class FrozenDict(dict):
|
||||||
def __hash__(self) -> int: # type: ignore
|
def __hash__(self) -> int:
|
||||||
return hash(frozenset(self.items()))
|
return hash(frozenset(self.items()))
|
||||||
|
|
||||||
|
|
||||||
|
class FrozenList(list):
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash(tuple(self))
|
||||||
|
|
||||||
|
|
||||||
|
class FrozenSet(set):
|
||||||
|
def __hash__(self) -> int:
|
||||||
|
return hash(frozenset(self))
|
||||||
|
|
||||||
|
|
||||||
def freeze_arguments(func: Callable[..., Any]) -> Callable[..., Any]:
|
def freeze_arguments(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
"""Transform mutable dictionary
|
"""Transform mutable dictionary
|
||||||
Into immutable
|
Into immutable
|
||||||
|
|
@ -16,10 +26,21 @@ def freeze_arguments(func: Callable[..., Any]) -> Callable[..., Any]:
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> Any:
|
def wrapper(*args: List[Any], **kwargs: Dict[str, Any]) -> Any:
|
||||||
args = tuple(FrozenDict(arg) if isinstance(arg, dict) else arg for arg in args)
|
args = tuple(_freeze(arg) for arg in args)
|
||||||
kwargs = {
|
kwargs = {k: _freeze(v) for k, v in kwargs.items()}
|
||||||
k: FrozenDict(v) if isinstance(v, dict) else v for k, v in kwargs.items()
|
|
||||||
}
|
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def _freeze(value: Union[List[Any], Dict[str, Any], Set[Any]]) -> Any:
|
||||||
|
if isinstance(value, dict):
|
||||||
|
return FrozenDict(value)
|
||||||
|
|
||||||
|
if isinstance(value, list):
|
||||||
|
return FrozenList(value)
|
||||||
|
|
||||||
|
if isinstance(value, set):
|
||||||
|
return FrozenSet(value)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue