From 11bb1e492aae3495cc3c3c877fd10580a19234c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 30 Aug 2019 14:44:26 +0200 Subject: [PATCH] Add get_type() method to TypeIdRegistryMixin --- .../event_handling/type_id_registry/__init__.py | 2 +- .../type_id_registry/test_type_id_registry.py | 13 ++++++++----- .../type_id_registry/type_id_registry.py | 13 ++++++++----- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/tfw/internals/event_handling/type_id_registry/__init__.py b/tfw/internals/event_handling/type_id_registry/__init__.py index 6786ca7..45ed6e6 100644 --- a/tfw/internals/event_handling/type_id_registry/__init__.py +++ b/tfw/internals/event_handling/type_id_registry/__init__.py @@ -1 +1 @@ -from .type_id_registry import TypeIdREgistryMixin +from .type_id_registry import TypeIdRegistryMixin diff --git a/tfw/internals/event_handling/type_id_registry/test_type_id_registry.py b/tfw/internals/event_handling/type_id_registry/test_type_id_registry.py index 617d695..df6a994 100644 --- a/tfw/internals/event_handling/type_id_registry/test_type_id_registry.py +++ b/tfw/internals/event_handling/type_id_registry/test_type_id_registry.py @@ -1,23 +1,26 @@ import pytest -from .type_id_registry import TypeIdREgistryMixin +from .type_id_registry import TypeIdRegistryMixin def test_registry(): - class A(TypeIdREgistryMixin): + class A(TypeIdRegistryMixin): _type_id_registry = {} class B(A): pass class C(A): pass + class D(B): + pass # pylint: disable=protected-access - assert A._type_id_registry['B'] == B - assert A._type_id_registry['C'] == C + assert A._type_id_registry['B'] == A.get_type('B') == B + assert A._type_id_registry['C'] == A.get_type('C') == C + assert A._type_id_registry['D'] == A.get_type('D') == D def test_build_by_type_id(): - class D(TypeIdREgistryMixin): + class D(TypeIdRegistryMixin): _type_id_registry = {} built = [] class E(D): diff --git a/tfw/internals/event_handling/type_id_registry/type_id_registry.py b/tfw/internals/event_handling/type_id_registry/type_id_registry.py index 2737480..7970392 100644 --- a/tfw/internals/event_handling/type_id_registry/type_id_registry.py +++ b/tfw/internals/event_handling/type_id_registry/type_id_registry.py @@ -2,7 +2,7 @@ from abc import ABCMeta from contextlib import suppress -class TypeIdREgistryMeta(ABCMeta): +class TypeIdRegistryMeta(ABCMeta): def __init__(cls, name, bases, attrs): with suppress(AttributeError): if cls.__name__ in cls._type_id_registry: @@ -11,11 +11,14 @@ class TypeIdREgistryMeta(ABCMeta): super().__init__(name, bases, attrs) -class TypeIdREgistryMixin(metaclass=TypeIdREgistryMeta): +class TypeIdRegistryMixin(metaclass=TypeIdRegistryMeta): @classmethod - def build_type(cls, type_id, *args, **kwargs): + def get_type(cls, type_id): try: - instance_type = cls._type_id_registry[type_id] - return instance_type(*args, **kwargs) + return cls._type_id_registry[type_id] except KeyError: raise RuntimeError(f'No type with id {type_id}!') + + @classmethod + def build_type(cls, type_id, *args, **kwargs): + return cls.get_type(type_id)(*args, **kwargs)