litestar-org / polyfactory

Simple and powerful factories for mock data generation
https://polyfactory.litestar.dev/
MIT License
1k stars 78 forks source link

Bug: polyfactoryutils.helpers.flatten_annotation does not handle Optional properly #439

Closed sam-or closed 10 months ago

sam-or commented 10 months ago

Description

calling the flatten_annotation function with an optional annotation int | None/Optional[int] does not properly flatten the annotation, instead the result is [NoneType,[int]] (expected: [NoneType, int])

MCVE

# Your MCVE code here
@dataclass
class OptionalInt:
    i: Optional[int]

class OptionalIntFactory(DataclassFactory[OptionalInt]):
    __model__ = OptionalInt

results = list(OptionalIntFactory.coverage())

Steps to reproduce

Run the `<factory>.coverage()` function with any factory for a model that has an optional field

Logs

TypeError: unhashable type: 'list'

Release Version

2.12.0

Platform


[!NOTE]
While we are open for sponsoring on GitHub Sponsors and OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh Litestar dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.

Fund with Polar

sam-or commented 10 months ago

I'll create a PR for this