polars.testing.parametric.create_list_strategy#

polars.testing.parametric.create_list_strategy(
inner_dtype: PolarsDataType | None,
select_from: Sequence[Any] | None = None,
size: int | None = None,
min_size: int | None = None,
max_size: int | None = None,
unique: bool = False,
) SearchStrategy[list[Any]][source]#

Hypothesis strategy for producing polars List data.

Parameters:
inner_dtypePolarsDataType

type of the inner list elements (can also be another List).

select_fromlist, optional

randomly select the innermost values from this list (otherwise the default strategy associated with the innermost dtype is used).

sizeint, optional

if set, generated lists will be of exactly this size (and ignore the min_size/max_size params).

min_sizeint, optional

set the minimum size of the generated lists (default: 0 if unset).

max_sizeint, optional

set the maximum size of the generated lists (default: 3 if min_size is unset or zero, otherwise 2x min_size).

uniquebool, optional

ensure that the generated lists contain unique values.

Examples

Create a strategy that generates a list of i32 values:

>>> lst = create_list_strategy(inner_dtype=pl.Int32)
>>> lst.example()  
[-11330, 24030, 116]

Create a strategy that generates lists of lists of specific strings:

>>> lst = create_list_strategy(
...     inner_dtype=pl.List(pl.Utf8),
...     select_from=["xx", "yy", "zz"],
... )
>>> lst.example()  
[['yy', 'xx'], [], ['zz']]

Create a UInt8 dtype strategy as a hypothesis composite that generates pairs of small int values where the first is always <= the second:

>>> from hypothesis.strategies import composite
>>>
>>> @composite
... def uint8_pairs(draw, uints=create_list_strategy(pl.UInt8, size=2)):
...     pairs = list(zip(draw(uints), draw(uints)))
...     return [sorted(ints) for ints in pairs]
...
>>> uint8_pairs().example()  
[(12, 22), (15, 131)]
>>> uint8_pairs().example()  
[(59, 176), (149, 149)]