Skip to content

Commit aca9728

Browse files
author
Danny Cooper
committed
Add cached_property hypothesis check.
1 parent d248809 commit aca9728

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

tests/strategies.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
Testing strategies for Hypothesis-based tests.
55
"""
6-
6+
import functools
77
import keyword
88
import string
99

@@ -111,13 +111,19 @@ def simple_attrs_with_metadata(draw):
111111

112112
simple_attrs = simple_attrs_without_metadata | simple_attrs_with_metadata()
113113

114+
114115
# Python functions support up to 255 arguments.
115116
list_of_attrs = st.lists(simple_attrs, max_size=3)
116117

117118

118119
@st.composite
119120
def simple_classes(
120-
draw, slots=None, frozen=None, weakref_slot=None, private_attrs=None
121+
draw,
122+
slots=None,
123+
frozen=None,
124+
weakref_slot=None,
125+
private_attrs=None,
126+
cached_property=None,
121127
):
122128
"""
123129
A strategy that generates classes with default non-attr attributes.
@@ -157,6 +163,7 @@ class HypClass:
157163
pre_init_flag = draw(st.booleans())
158164
post_init_flag = draw(st.booleans())
159165
init_flag = draw(st.booleans())
166+
cached_property_flag = draw(st.booleans())
160167

161168
if pre_init_flag:
162169

@@ -179,9 +186,20 @@ def init(self, *args, **kwargs):
179186

180187
cls_dict["__init__"] = init
181188

189+
bases = (object,)
190+
if cached_property or (cached_property is None and cached_property_flag):
191+
192+
class BaseWithCachedProperty:
193+
@functools.cached_property
194+
def _cached_property(self) -> int:
195+
return 1
196+
197+
bases = (BaseWithCachedProperty,)
198+
182199
return make_class(
183200
"HypClass",
184201
cls_dict,
202+
bases=bases,
185203
slots=slots_flag if slots is None else slots,
186204
frozen=frozen_flag if frozen is None else frozen,
187205
weakref_slot=weakref_flag if weakref_slot is None else weakref_slot,

tests/test_3rd_party.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestCloudpickleCompat:
1919
Tests for compatibility with ``cloudpickle``.
2020
"""
2121

22-
@given(simple_classes())
22+
@given(simple_classes(cached_property=False))
2323
def test_repr(self, cls):
2424
"""
2525
attrs instances can be pickled and un-pickled with cloudpickle.

0 commit comments

Comments
 (0)