Skip to content

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

Description

@sander76

Description

Observe the below example. The comments explain the issue.

Example:

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):

if is_union(field_meta.annotation):

Release Version

3.3.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions