Skip to content

Commit

Permalink
fix None cannot be key of Registry
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, 2024
1 parent 75080b9 commit aa76500
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions chanfig/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from typing import Any

from .nested_dict import NestedDict
from .utils import Null
from .utils import NULL, Null


class Registry(NestedDict):
Expand Down Expand Up @@ -104,7 +104,7 @@ def __init__(self, override: bool | None = None, key: str = "name", fallback: bo
self.setattr("default", Null)

def register(
self, component: Any = None, name: Any | None = None, override: bool = False, default: bool = False
self, component: Any = Null, name: Any = Null, override: bool = False, default: bool = False
) -> Callable:
r"""
Register a new component.
Expand Down Expand Up @@ -141,23 +141,23 @@ def register(
raise ValueError(f"Component with name {name} already registered.")

# Registry.register()
if name is not None:
if name is not Null:
self.set(name, component)
if default:
self.setattr("default", component)
return component
# @Registry.register
if callable(component) and name is None:
if callable(component) and name is Null:
self.set(component.__name__, component)
if default:
self.setattr("default", component)
return component

# @Registry.register()
def decorator(name: Any | None = None):
def decorator(name: Any = Null):
@wraps(self.register)
def wrapper(component):
if name is None:
if name is Null:
self.set(component.__name__, component)
else:
self.set(name, component)
Expand Down Expand Up @@ -223,7 +223,7 @@ def init(cls: Callable, *args: Any, **kwargs: Any) -> Any: # pylint: disable=W0

return cls(*args, **kwargs)

def build(self, name: str | MutableMapping | None = None, *args: Any, **kwargs: Any) -> Any:
def build(self, name: str | MutableMapping | NULL = Null, *args: Any, **kwargs: Any) -> Any:
r"""
Build a component.
Expand Down Expand Up @@ -264,7 +264,7 @@ def build(self, name: str | MutableMapping | None = None, *args: Any, **kwargs:
if isinstance(name, MutableMapping):
name = deepcopy(name)
name, kwargs = name.pop(self.getattr("key", "name")), dict(name, **kwargs) # type: ignore[arg-type]
if name is None:
if name is Null:
name, kwargs = kwargs.pop(self.getattr("key")), dict(**kwargs)
return self.init(self.lookup(name), *args, **kwargs) # type: ignore[arg-type]

Expand Down

0 comments on commit aa76500

Please sign in to comment.