fix: type component registry

This commit is contained in:
Gabriel Dugny 2024-03-24 14:25:18 +01:00
parent 04160f4e0b
commit 3952fd7437
2 changed files with 23 additions and 12 deletions

View file

@ -164,6 +164,7 @@ def _get_dir_path_from_component_module_path(component_module_path: str, candida
class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass): class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
# Either template_name or template must be set on subclass OR subclass must implement get_template() with # Either template_name or template must be set on subclass OR subclass must implement get_template() with
# non-null return. # non-null return.
class_hash: ClassVar[int]
template_name: ClassVar[Optional[str]] = None template_name: ClassVar[Optional[str]] = None
template: Optional[str] = None template: Optional[str] = None
js: Optional[str] = None js: Optional[str] = None

View file

@ -1,3 +1,13 @@
from __future__ import annotations
from typing import TYPE_CHECKING, Callable, Type, TypeVar
if TYPE_CHECKING:
from django_components import component
_TC = TypeVar("_TC", bound=Type["component.Component"])
class AlreadyRegistered(Exception): class AlreadyRegistered(Exception):
pass pass
@ -6,39 +16,39 @@ class NotRegistered(Exception):
pass pass
class ComponentRegistry(object): class ComponentRegistry:
def __init__(self): def __init__(self) -> None:
self._registry = {} # component name -> component_class mapping self._registry: dict[str, type[component.Component]] = {} # component name -> component_class mapping
def register(self, name=None, component=None): def register(self, name: str, component: type[component.Component]) -> None:
existing_component = self._registry.get(name) existing_component = self._registry.get(name)
if existing_component and existing_component.class_hash != component.class_hash: if existing_component and existing_component.class_hash != component.class_hash:
raise AlreadyRegistered('The component "%s" has already been registered' % name) raise AlreadyRegistered('The component "%s" has already been registered' % name)
self._registry[name] = component self._registry[name] = component
def unregister(self, name): def unregister(self, name: str) -> None:
self.get(name) self.get(name)
del self._registry[name] del self._registry[name]
def get(self, name): def get(self, name: str) -> type[component.Component]:
if name not in self._registry: if name not in self._registry:
raise NotRegistered('The component "%s" is not registered' % name) raise NotRegistered('The component "%s" is not registered' % name)
return self._registry[name] return self._registry[name]
def all(self): def all(self) -> dict[str, type[component.Component]]:
return self._registry return self._registry
def clear(self): def clear(self) -> None:
self._registry = {} self._registry = {}
# This variable represents the global component registry # This variable represents the global component registry
registry = ComponentRegistry() registry: ComponentRegistry = ComponentRegistry()
def register(name): def register(name: str) -> Callable[[_TC], _TC]:
"""Class decorator to register a component. """Class decorator to register a component.
Usage: Usage:
@ -48,7 +58,7 @@ def register(name):
... ...
""" """
def decorator(component): def decorator(component: _TC) -> _TC:
registry.register(name=name, component=component) registry.register(name=name, component=component)
return component return component