polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

fix: Generate correct collection size when annotation_types.Len is used

Open priyankc opened this issue 6 months ago • 3 comments

Description

Fixes a bug where annotation_types.Len can go over the described length

Test

Before the fix:

======================================================================================== FAILURES =========================================================================================
_______________________________________________________________________ test_annotated_type_collection_length[List] _______________________________________________________________________

type_ = typing.List[int]

    @pytest.mark.parametrize("type_", (List[int], Dict[int, int]))
    def test_annotated_type_collection_length(type_: type) -> None:
        class Foo(BaseModel):
            foo: Annotated[type_, Len(1)]

        class FooFactory(ModelFactory):
            __model__ = Foo

        for _ in range(10):
            foo = FooFactory.build()
>           assert len(foo.foo) == 1, len(foo.foo)
E           AssertionError: 2
E           assert 2 == 1
E            +  where 2 = len([8742, 3227])
E            +    where [8742, 3227] = Foo(foo=[8742, 3227]).foo

tests/test_collection_length.py:27: AssertionError
_______________________________________________________________________ test_annotated_type_collection_length[Dict] _______________________________________________________________________

type_ = typing.Dict[int, int]

    @pytest.mark.parametrize("type_", (List[int], Dict[int, int]))
    def test_annotated_type_collection_length(type_: type) -> None:
        class Foo(BaseModel):
            foo: Annotated[type_, Len(1)]

        class FooFactory(ModelFactory):
            __model__ = Foo

        for _ in range(10):
            foo = FooFactory.build()
>           assert len(foo.foo) == 1, len(foo.foo)
E           AssertionError: 2
E           assert 2 == 1
E            +  where 2 = len({2700: 1673, 9643: 3851})
E            +    where {2700: 1673, 9643: 3851} = Foo(foo={9643: 3851, 2700: 1673}).foo

tests/test_collection_length.py:27: AssertionError
================================================================================= short test summary info =================================================================================
FAILED tests/test_collection_length.py::test_annotated_type_collection_length[List] - AssertionError: 2
FAILED tests/test_collection_length.py::test_annotated_type_collection_length[Dict] - AssertionError: 2
============================================================================== 2 failed, 79 passed in 0.51s ===============================================================================

After the fix

collected 81 items

tests/test_collection_length.py .................................................................................                                                                   [100%]

=================================================================================== 81 passed in 0.49s ====================================================================================

Closes

Fixes #710

priyankc avatar Jun 10 '25 18:06 priyankc

cc @Rub1kCube

priyankc avatar Jun 10 '25 18:06 priyankc

@Rub1kCube addressed the comments. please take a look when you have a moment.

priyankc avatar Jun 11 '25 18:06 priyankc

Change looks good! Can you look at the failing tests and update please?

adhtruong avatar Jun 15 '25 11:06 adhtruong

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/712

github-actions[bot] avatar Jun 16 '25 06:06 github-actions[bot]

@adhtruong Fixed the failing tests. I found that that Pydantic v1 doesn't handle annotation constraints the same way as Pydantic v2 which is leading to test failures. v1 requires explicit extraction of constraints from annotated types while v2 automatically propagates them.

I left a comment where I was handling that. Please take a look and let me know.

priyankc avatar Jun 16 '25 06:06 priyankc

https://github.com/all-contributors add @priyankc for bugs

adhtruong avatar Jun 19 '25 07:06 adhtruong