Skip to content

Commit

Permalink
More factories with takes_self
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche committed Dec 25, 2024
1 parent 5d480f4 commit 2408236
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
from typing import Literal

from hypothesis import HealthCheck, settings
from hypothesis.strategies import just, one_of
from typing_extensions import TypeAlias

from cattrs import UnstructureStrategy

Expand All @@ -13,3 +15,5 @@
settings.load_profile("CI")

unstructure_strats = one_of(just(s) for s in UnstructureStrategy)

FeatureFlag: TypeAlias = Literal["always", "never", "sometimes"]
36 changes: 17 additions & 19 deletions tests/untyped.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
from attr._make import _CountingAttr
from attrs import NOTHING, AttrsInstance, Factory, make_class
from hypothesis import strategies as st
from hypothesis.strategies import SearchStrategy
from hypothesis.strategies import DrawFn, SearchStrategy, booleans

from . import FeatureFlag

PosArg = Any
PosArgs = tuple[PosArg]
Expand Down Expand Up @@ -167,7 +169,7 @@ def gen_attr_names() -> Iterable[str]:
def _create_hyp_class(
attrs_and_strategy: list[tuple[_CountingAttr, st.SearchStrategy[PosArgs]]],
frozen=None,
) -> SearchStrategy[tuple]:
) -> SearchStrategy[tuple[type[AttrsInstance], PosArgs, Any]]:
"""
A helper function for Hypothesis to generate attrs classes.
Expand Down Expand Up @@ -209,21 +211,14 @@ def just_class(tup):
return _create_hyp_class(combined_attrs)


def just_class_with_type(tup):
nested_cl = tup[1][0]
default = attr.Factory(nested_cl)
combined_attrs = list(tup[0])
combined_attrs.append(
(attr.ib(default=default, type=nested_cl), st.just(nested_cl()))
)
return _create_hyp_class(combined_attrs)


def just_class_with_type_takes_self(
tup: tuple[list[tuple[_CountingAttr, SearchStrategy]], tuple[type[AttrsInstance]]]
) -> SearchStrategy[tuple[type[AttrsInstance]]]:
def just_class_with_type(
draw: DrawFn, tup, takes_self: FeatureFlag = "sometimes"
) -> SearchStrategy[tuple[type[AttrsInstance], PosArgs, KwArgs]]:
nested_cl = tup[1][0]
default = Factory(lambda _: nested_cl(), takes_self=True)
if takes_self == "never":
default = Factory(nested_cl)
elif takes_self == "always" or takes_self == "sometimes" and draw(booleans()):
default = Factory(lambda _: nested_cl, takes_self=True)
combined_attrs = list(tup[0])
combined_attrs.append(
(attr.ib(default=default, type=nested_cl), st.just(nested_cl()))
Expand All @@ -240,9 +235,13 @@ def just_frozen_class_with_type(tup):
return _create_hyp_class(combined_attrs)


def list_of_class(tup):
def list_of_class(draw: DrawFn, tup, takes_self: FeatureFlag = "sometimes"):
nested_cl = tup[1][0]
default = attr.Factory(lambda: [nested_cl()])
if takes_self == "never":
default = Factory(lambda: [nested_cl()])
elif takes_self == "always" or takes_self == "sometimes" and draw(booleans()):
default = Factory(lambda _: [nested_cl()], takes_self=True)

combined_attrs = list(tup[0])
combined_attrs.append((attr.ib(default=default), st.just([nested_cl()])))
return _create_hyp_class(combined_attrs)
Expand Down Expand Up @@ -286,7 +285,6 @@ def _create_hyp_nested_strategy(simple_class_strategy):
| attrs_and_classes.flatmap(list_of_class_with_type)
| attrs_and_classes.flatmap(dict_of_class)
| attrs_and_classes.flatmap(just_frozen_class_with_type)
| attrs_and_classes.flatmap(just_class_with_type_takes_self)
)


Expand Down

0 comments on commit 2408236

Please sign in to comment.