Skip to content

Commit

Permalink
Only call super if no existing init subclass (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam authored Mar 20, 2022
1 parent 487d347 commit f0ad387
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions abcattrs/abcattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 3 additions & 2 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f0ad387

Please sign in to comment.