Skip to content

Commit

Permalink
Add cached_property hypothesis check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny Cooper committed Nov 9, 2023
1 parent d248809 commit aca9728
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
Testing strategies for Hypothesis-based tests.
"""

import functools
import keyword
import string

Expand Down Expand Up @@ -111,13 +111,19 @@ def simple_attrs_with_metadata(draw):

simple_attrs = simple_attrs_without_metadata | simple_attrs_with_metadata()


# Python functions support up to 255 arguments.
list_of_attrs = st.lists(simple_attrs, max_size=3)


@st.composite
def simple_classes(
draw, slots=None, frozen=None, weakref_slot=None, private_attrs=None
draw,
slots=None,
frozen=None,
weakref_slot=None,
private_attrs=None,
cached_property=None,
):
"""
A strategy that generates classes with default non-attr attributes.
Expand Down Expand Up @@ -157,6 +163,7 @@ class HypClass:
pre_init_flag = draw(st.booleans())
post_init_flag = draw(st.booleans())
init_flag = draw(st.booleans())
cached_property_flag = draw(st.booleans())

if pre_init_flag:

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

cls_dict["__init__"] = init

bases = (object,)
if cached_property or (cached_property is None and cached_property_flag):

class BaseWithCachedProperty:
@functools.cached_property
def _cached_property(self) -> int:
return 1

bases = (BaseWithCachedProperty,)

return make_class(
"HypClass",
cls_dict,
bases=bases,
slots=slots_flag if slots is None else slots,
frozen=frozen_flag if frozen is None else frozen,
weakref_slot=weakref_flag if weakref_slot is None else weakref_slot,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_3rd_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestCloudpickleCompat:
Tests for compatibility with ``cloudpickle``.
"""

@given(simple_classes())
@given(simple_classes(cached_property=False))
def test_repr(self, cls):
"""
attrs instances can be pickled and un-pickled with cloudpickle.
Expand Down

0 comments on commit aca9728

Please sign in to comment.