fix: Generate correct collection size when annotation_types.Len is used
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
cc @Rub1kCube
@Rub1kCube addressed the comments. please take a look when you have a moment.
Change looks good! Can you look at the failing tests and update please?
Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/712
@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.
https://github.com/all-contributors add @priyankc for bugs