litestar-org / litestar-org/polyfactory
Bug: Examples in union types are not found when using coverage
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 120
- PR merge metrics
- No merged PRs in 30d
Description
Description
For union types where one of the subtypes are annotated with examples I'd expect those examples to be used when generating values from that subtype.
This works for .build() and .batch(), but breaks when generating coverage.
MCVE
from typing import Annotated
import pydantic
from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import AfterValidator, Field
the_list = ["foo", "bar", "baz"] # Some dynamically populated list
def l_validator(v):
if v not in the_list:
raise ValueError(f"{v} not in {the_list}")
return v
TheListType = Annotated[str, AfterValidator(l_validator), Field(examples=the_list)]
class WithoutNone(pydantic.BaseModel):
f: TheListType
class WithNone(pydantic.BaseModel):
f: TheListType | None
class WithoutNoneFactory(ModelFactory[WithoutNone]):
__check_model__ = True
__use_examples__ = True
class WithNoneFactory(ModelFactory[WithNone]):
__check_model__ = True
__use_examples__ = True
# These three works fine
print("Without, batch:", WithoutNoneFactory.batch(2))
print("With, batch:", WithNoneFactory.batch(2))
print("Without, coverage:", list(WithoutNoneFactory.coverage()))
# Breaks here
print("With, coverage:", list(WithNoneFactory.coverage()))
Logs
Without, batch: [WithoutNone(f='bar'), WithoutNone(f='bar')]
With, batch: [WithNone(f='bar'), WithNone(f=None)]
Without, coverage: [WithoutNone(f='foo'), WithoutNone(f='bar'), WithoutNone(f='baz')]
Traceback (most recent call last):
...
pydantic_core._pydantic_core.ValidationError: 1 validation error for WithNone
f
Value error, JtYDrNsNdqcQNeGCBrfN not in ['foo', 'bar', 'baz'] [type=value_error, input_value='JtYDrNsNdqcQNeGCBrfN', input_type=str]
For further information visit https://errors.pydantic.dev/2.13/v/value_error
Release Version
3.3.0
Platform
- Linux
- Mac
- Windows
- Other (Please specify in the description above)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ModelFactory.coverage() path and reproduce the supplied MCVE using Pydantic's Annotated, AfterValidator, and Field examples. Done means coverage for the optional union returns the annotated example values instead of generating an invalid value, while the existing batch and non-union coverage cases remain working.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100