Skip to content

Commit

Permalink
fix nesteddict repr
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Apr 14, 2023
1 parent a56e9ad commit b15617f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
8 changes: 4 additions & 4 deletions chanfig/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def unlocked(self):
if was_frozen:
self.freeze()

def get(self, name: str, default: Any = Null) -> Any:
def get(self, name: Any, default: Any = Null) -> Any:
r"""
Get value from `Config`.
Expand Down Expand Up @@ -484,7 +484,7 @@ def get(self, name: str, default: Any = Null) -> Any:
@frozen_check
def set(
self,
name: str,
name: Any,
value: Any,
convert_mapping: Optional[bool] = None,
) -> None:
Expand Down Expand Up @@ -526,7 +526,7 @@ def set(
return super().set(name, value, convert_mapping)

@frozen_check
def delete(self, name: str) -> None:
def delete(self, name: Any) -> None:
r"""
Delete value from `Config`.
Expand Down Expand Up @@ -565,7 +565,7 @@ def delete(self, name: str) -> None:
super().delete(name)

@frozen_check
def pop(self, name: str, default: Any = Null) -> Any:
def pop(self, name: Any, default: Any = Null) -> Any:
r"""
Pop value from `Config`.
Expand Down
2 changes: 1 addition & 1 deletion chanfig/default_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, default_factory: Optional[Callable] = None, *args, **kwargs)
f"`default_factory={default_factory}` must be Callable, but got {type(default_factory)}."
)

def __missing__(self, name: str, default=Null) -> Any: # pylint: disable=R1710
def __missing__(self, name: Any, default=Null) -> Any: # pylint: disable=R1710
if default is Null:
if not self.hasattr("default_factory"):
raise KeyError(name) from None
Expand Down
22 changes: 11 additions & 11 deletions chanfig/flat_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ def _init(self, *args, **kwargs) -> None:
for key, value in kwargs.items():
self.set(key, value)

def __getattribute__(self, name) -> Any:
def __getattribute__(self, name: Any) -> Any:
if name not in ("__class__", "__dict__", "getattr") and name in self:
return self[name]
return super().__getattribute__(name)

def get(self, name: str, default: Any = Null) -> Any:
def get(self, name: Any, default: Any = Null) -> Any:
r"""
Get value from `FlatDict`.
Expand Down Expand Up @@ -183,15 +183,15 @@ def get(self, name: str, default: Any = Null) -> Any:
"""

if name in self:
return super().__getitem__(name)
return dict.__getitem__(self, name)
if default is not Null:
return default
return self.__missing__(name)

def __getitem__(self, name: str) -> Any:
def __getitem__(self, name: Any) -> Any:
return self.get(name)

def __getattr__(self, name) -> Any:
def __getattr__(self, name: Any) -> Any:
try:
return self.get(name)
except KeyError:
Expand Down Expand Up @@ -239,13 +239,13 @@ def set(self, name: Any, value: Any) -> None:
def __setitem__(self, name: Any, value: Any) -> None:
self.set(name, value)

def __setattr__(self, name, value) -> None:
def __setattr__(self, name: Any, value: Any) -> None:
try:
self.set(name, value)
except KeyError:
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'") from None

def delete(self, name: str) -> None:
def delete(self, name: Any) -> None:
r"""
Delete value from `FlatDict`.
Expand Down Expand Up @@ -279,12 +279,12 @@ def delete(self, name: str) -> None:
```
"""

super().__delitem__(name)
dict.__delitem__(self, name)

def __delitem__(self, name: str) -> None:
def __delitem__(self, name: Any) -> None:
return self.delete(name)

def __delattr__(self, name) -> None:
def __delattr__(self, name: Any) -> None:
try:
self.delete(name)
except KeyError:
Expand Down Expand Up @@ -428,7 +428,7 @@ def hasattr(self, name: str) -> bool:
except AttributeError:
return False

def __missing__(self, name: str) -> Any: # pylint: disable=R1710
def __missing__(self, name: Any) -> Any: # pylint: disable=R1710
raise KeyError(name)

def dict(self, cls: Callable = dict) -> Mapping:
Expand Down
11 changes: 5 additions & 6 deletions chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def apply(self, func: Callable) -> NestedDict:
func(self)
return self

def get(self, name: str, default: Any = Null) -> Any:
def get(self, name: Any, default: Any = Null) -> Any:
r"""
Get value from `NestedDict`.
Expand Down Expand Up @@ -276,7 +276,7 @@ def get(self, name: str, default: Any = Null) -> Any:

def set( # pylint: disable=W0221
self,
name: str,
name: Any,
value: Any,
convert_mapping: Optional[bool] = None,
) -> None:
Expand Down Expand Up @@ -337,12 +337,11 @@ def set( # pylint: disable=W0221
convert_mapping = self.convert_mapping
delimiter = self.getattr("delimiter", ".")
default_mapping = self.getattr("default_mapping", NestedDict)
default = default_mapping() if convert_mapping else Null
try:
while isinstance(name, str) and delimiter in name:
name, rest = name.split(delimiter, 1)
if name not in self:
self.__missing__(name, default)
self.__missing__(name, default_mapping())
self, name = self[name], rest # pylint: disable=W0642
except (AttributeError, TypeError):
raise KeyError(name) from None
Expand All @@ -358,7 +357,7 @@ def set( # pylint: disable=W0221
f"Cannot set `{full_name}` to `{value}`, as `{delimiter.join(full_name.split(delimiter)[:-1])}={self}`."
)

def delete(self, name: str) -> None:
def delete(self, name: Any) -> None:
r"""
Delete value from `NestedDict`.
Expand Down Expand Up @@ -406,7 +405,7 @@ def delete(self, name: str) -> None:
raise KeyError(name) from None
super().delete(name)

def pop(self, name: str, default: Any = Null) -> Any:
def pop(self, name: Any, default: Any = Null) -> Any:
r"""
Pop value from `NestedDict`.
Expand Down
2 changes: 1 addition & 1 deletion chanfig/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def _get_value(obj) -> Any:
return obj.value
return obj

def __getattr__(self, attr):
def __getattr__(self, attr) -> Any:
return getattr(self.value, attr)

def __lt__(self, other) -> bool:
Expand Down

0 comments on commit b15617f

Please sign in to comment.