Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call abc.update_abstractmethods on 3.10+ (#1001)
- Loading branch information
Showing
3 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
On Python 3.10 and later, call `abc.update_abstractmethods() <https://docs.pytho. | ||
org/3.10/library/abc.html#abc.update_abstractmethods>`_ on dict classes after creation. | ||
This improves the detection of abstractness. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# SPDX-License-Identifier: MIT | ||
|
||
import abc | ||
import inspect | ||
|
||
import pytest | ||
|
||
import attrs | ||
|
||
from attr._compat import PY310 | ||
|
||
|
||
@pytest.mark.skipif(not PY310, reason="abc.update_abstractmethods is 3.10+") | ||
class TestUpdateAbstractMethods: | ||
def test_abc_implementation(self, slots): | ||
""" | ||
If an attrs class implements an abstract method, it stops being | ||
abstract. | ||
""" | ||
|
||
class Ordered(abc.ABC): | ||
@abc.abstractmethod | ||
def __lt__(self, other): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def __le__(self, other): | ||
pass | ||
|
||
@attrs.define(order=True, slots=slots) | ||
class Concrete(Ordered): | ||
x: int | ||
|
||
assert not inspect.isabstract(Concrete) | ||
assert Concrete(2) > Concrete(1) | ||
|
||
def test_remain_abstract(self, slots): | ||
""" | ||
If an attrs class inherits from an abstract class but doesn't implement | ||
abstract methods, it remains abstract. | ||
""" | ||
|
||
class A(abc.ABC): | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
|
||
@attrs.define(slots=slots) | ||
class StillAbstract(A): | ||
pass | ||
|
||
assert inspect.isabstract(StillAbstract) | ||
with pytest.raises( | ||
TypeError, match="class StillAbstract with abstract method foo" | ||
): | ||
StillAbstract() |