/
test_abc.py
56 lines (42 loc) · 1.34 KB
/
test_abc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()