litestar-org / litestar-org/polyfactory

Bug: coverage method does not respect constraints when used in combination with a Union

Open
#854 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Description

Observe the below example. The comments explain the issue.

Example:

```python
from dataclasses import dataclass
from typing import Annotated

from annotated_types import Gt, Lt

from polyfactory.factories import DataclassFactory

@dataclass
class MyClass:
values: Annotated[int, Gt(0), Lt(5)] | None # <------- the int type should be >0, <5

class Factory(DataclassFactory[MyClass]): ...

from_coverage = list(Factory.coverage())
print(from_coverage)
# >>> [MyClass(values=2863), MyClass(values=None)] # above constraints are not respected.

from_batch = Factory.batch(2)
print(from_batch)
# >>> [MyClass(values=None), MyClass(values=2)] # above constraints are respected.

```

My guess is the issue lies here (no solution yet): https://github.com/litestar-org/polyfactory/blob/c79cebee372fdf7bdde6df973e4996e31586d598/polyfactory/factories/base.py#L895

### Release Version

3.3.0

### Platform

- [x] Linux
- [ ] Mac
- [x] Windows
- [ ] Other (Please specify in the description above)

Contributor guide

Open the contributing guide

Research direction

Start at polyfactory/factories/base.py around line 895, the location identified in the report. Reproduce the dataclass example and compare Factory.coverage() with Factory.batch(2), focusing on the Annotated integer constraints inside the Union. Done means coverage-generated values respect Gt(0) and Lt(5) while retaining the expected None case.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.