Skip to content

Commit

Permalink
Fix premature check on abstract class (#23)
Browse files Browse the repository at this point in the history
`@abstractattrs` check shouldn't interpret an abstract class as
concrete. We're now checking if a class is abstract before
verifying abstract attributes.

I'm aware that there's a discrepancy here. If there's no methods
decorated as `@abstractmethod` and we only have ´@abstractattrs` we'll
still need to inherit `abc.ABC` on each subclass.

However, this change still covers some additional cases
  • Loading branch information
flaeppe authored Sep 17, 2022
1 parent 1444240 commit 0afc414
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion abcattrs/abcattrs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
import inspect
from collections.abc import Callable
from collections.abc import Iterable
from functools import partial
Expand Down Expand Up @@ -70,7 +71,7 @@ class UndefinedAbstractAttribute(TypeError):

def check_abstract_class_attributes(cls: type) -> None:
"""Check that a class defines inherited abstract attributes."""
if abc.ABC in cls.__bases__:
if inspect.isabstract(cls) or abc.ABC in cls.__bases__:
return

for attr in getattr(cls, "__abstract_attributes__", ()):
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ force_single_line = True

[tool:pytest]
addopts = --mypy-ini-file=setup.cfg

[coverage:report]
exclude_lines =
pragma: no cover
# ignore non-implementations
^\s*\.\.\.
13 changes: 13 additions & 0 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,16 @@ class B:

class C(A):
forward = B()


def test_does_not_interpret_abstract_subclass_as_concrete() -> None:
@abstractattrs
class A(abc.ABC):
attr: Abstract[int]

@abc.abstractmethod
def foo(self) -> None:
...

class B(A):
...

0 comments on commit 0afc414

Please sign in to comment.