From f0ad3879b0ef7bc3f21d7faa60c4c78d0bdbb8df Mon Sep 17 00:00:00 2001 From: Anton Agestam Date: Sun, 20 Mar 2022 22:29:15 +0100 Subject: [PATCH] Only call super if no existing init subclass (#9) --- abcattrs/abcattrs.py | 8 +++++--- tests/test_decorator.py | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/abcattrs/abcattrs.py b/abcattrs/abcattrs.py index a7079a2..e3229a6 100644 --- a/abcattrs/abcattrs.py +++ b/abcattrs/abcattrs.py @@ -79,13 +79,15 @@ def check_abstract_class_attributes(cls: type) -> None: def _init_subclass( cls: type, - existing_init_subclass: Callable[..., None] = lambda *_, **__: None, + existing_init_subclass: Callable[..., None] | None = None, *args: Any, **kwargs: Any, ) -> None: """ __init_subclass__ implementation added to classes decorated with @abstractattrs. """ - super(cls).__init_subclass__() # type: ignore[misc] + if existing_init_subclass is None: + super(cls).__init_subclass__() # type: ignore[misc] + else: + existing_init_subclass(cls, *args, **kwargs) check_abstract_class_attributes(cls) - existing_init_subclass(cls, *args, **kwargs) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index a4256aa..a09edc8 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -93,9 +93,10 @@ class A(abc.ABC): attr: Abstract[int] with pytest.raises(UndefinedAbstractAttribute): - type("B", (A,), {}) + type("B", (A,), {}, class_arg="doot") - init_subclass.assert_not_called() + init_subclass.assert_called_once() + init_subclass.reset_mock() class C(A, class_arg="intact"): attr = 1