Skip to content

Pydantic: generate unique lists #664

Answered by adhtruong
WilliamDEdwards asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @WilliamDEdwards !

I think the would need to UniqueListItem should be bound at this point so would need to override PydanticTypeUniqueList so the constraint could be applied at that level. Some of the logic is quite deep in functions so not super easy to override this way

Some alternative approaches would be

  1. Change type definition to
PydanticTypeUniqueList = Annotated[
    List[UniqueListItem],
    AfterValidator(validate_unique_list),
    Field(json_schema_extra={"uniqueItems": True}),
    {'unique_items': True}
]

This adds some duplication but uses existing tested constraint parsing so that this is applied for polyfactory.

  1. Override on a per field level with process_kwargs (and cov…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@WilliamDEdwards
Comment options

@WilliamDEdwards
Comment options

@adhtruong
Comment options

@WilliamDEdwards
Comment options

Answer selected by WilliamDEdwards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants