From aca972813166a915d6f2a7818e22390b3d849ce9 Mon Sep 17 00:00:00 2001 From: Danny Cooper Date: Wed, 8 Nov 2023 17:13:15 +0000 Subject: [PATCH] Add cached_property hypothesis check. --- tests/strategies.py | 22 ++++++++++++++++++++-- tests/test_3rd_party.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/strategies.py b/tests/strategies.py index 55598750b..b776f485a 100644 --- a/tests/strategies.py +++ b/tests/strategies.py @@ -3,7 +3,7 @@ """ Testing strategies for Hypothesis-based tests. """ - +import functools import keyword import string @@ -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. @@ -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: @@ -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, diff --git a/tests/test_3rd_party.py b/tests/test_3rd_party.py index 0707b2cd2..b2ce06c29 100644 --- a/tests/test_3rd_party.py +++ b/tests/test_3rd_party.py @@ -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.