Skip to content

Commit

Permalink
remove lambda in registry.register to support serialisation
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Apr 25, 2023
1 parent 97ca427 commit 7813971
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions chanfig/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, override: bool = False):
super().__init__()
self.setattr("override", override)

def register(self, component: Optional[Callable] = None, name: Optional[str] = None) -> Callable:
def register(self, component: Any = None, name: Optional[Any] = None) -> Callable:
r"""
Register a new component.
Expand All @@ -79,6 +79,7 @@ def register(self, component: Optional[Callable] = None, name: Optional[str] = N
Returns:
component: The registered component.
Registered component are expected to be `Callable`.
Raises:
ValueError: If the component with the same name already registered and `Registry.override=False`.
Expand Down Expand Up @@ -109,20 +110,25 @@ def register(self, component: Optional[Callable] = None, name: Optional[str] = N
# Registry.register()
if name is not None:
self.set(name, component)

# @Registry.register()
@wraps(self.register)
def register(component, name=None):
if name is None:
name = component.__name__
self.set(name, component)
return component

return component # type: ignore
# @Registry.register
if callable(component) and name is None:
return register(component)
elif callable(component) and name is None:
self.set(component.__name__, component)
return component

return lambda x: register(x, component)
# @Registry.register()
def decorator(name: Optional[Any] = None):
@wraps(self.register)
def wrapper(component):
if name is None:
self.set(component.__name__, component)
else:
self.set(name, component)
return component

return wrapper

return decorator(component)

def lookup(self, name: str) -> Any:
r"""
Expand Down

0 comments on commit 7813971

Please sign in to comment.