From 708c312ab13bdaaec938d0d6233fec8eb4befc61 Mon Sep 17 00:00:00 2001 From: marcozzxx810 Date: Wed, 31 Jul 2024 21:43:07 +0800 Subject: [PATCH] fix: constrained 0 length lists --- polyfactory/value_generators/constrained_collections.py | 2 +- tests/constraints/test_frozen_set_constraints.py | 2 +- tests/constraints/test_list_constraints.py | 2 +- tests/constraints/test_set_constraints.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/polyfactory/value_generators/constrained_collections.py b/polyfactory/value_generators/constrained_collections.py index 3953313b..65614e04 100644 --- a/polyfactory/value_generators/constrained_collections.py +++ b/polyfactory/value_generators/constrained_collections.py @@ -46,7 +46,7 @@ def handle_constrained_collection( collection: set[T] | list[T] = set() if (collection_type in (frozenset, set) or unique_items) else [] try: - length = factory.__random__.randint(min_items, max_items) or 1 + length = factory.__random__.randint(min_items, max_items) while (i := len(collection)) < length: if field_build_parameters and len(field_build_parameters) > i: build_params = field_build_parameters[i] diff --git a/tests/constraints/test_frozen_set_constraints.py b/tests/constraints/test_frozen_set_constraints.py index 5af83673..019957f6 100644 --- a/tests/constraints/test_frozen_set_constraints.py +++ b/tests/constraints/test_frozen_set_constraints.py @@ -83,4 +83,4 @@ def test_handle_constrained_set_with_different_types(t_type: Any) -> None: field_meta=FieldMeta(name="test", annotation=frozenset, random=Random()), item_type=t_type, ) - assert len(result) > 0 + assert len(result) >= 0 diff --git a/tests/constraints/test_list_constraints.py b/tests/constraints/test_list_constraints.py index 03edf91a..322035be 100644 --- a/tests/constraints/test_list_constraints.py +++ b/tests/constraints/test_list_constraints.py @@ -89,7 +89,7 @@ def test_handle_constrained_list_with_different_types(t_type: Any) -> None: field_meta=field_meta.children[0], # type: ignore[index] item_type=t_type, ) - assert len(result) > 0 + assert len(result) >= 0 def test_handle_unique_items() -> None: diff --git a/tests/constraints/test_set_constraints.py b/tests/constraints/test_set_constraints.py index e189af9b..95471902 100644 --- a/tests/constraints/test_set_constraints.py +++ b/tests/constraints/test_set_constraints.py @@ -83,4 +83,4 @@ def test_handle_constrained_set_with_different_types(t_type: Any) -> None: field_meta=FieldMeta(name="test", annotation=set, random=Random()), item_type=t_type, ) - assert len(result) > 0 + assert len(result) >= 0