litestar-org/polyfactory

Bug: ModelFactory does not generate values for Optional[conlist(OTHER_MODEL)]

jomach opened this issue · 7 comments

Description

Using this code

class SystemStatusFactory(ModelFactory):
        __model__ = SystemStatus
        __randomize_collection_length__ = True
        __min_collection_length__ = 2
        __max_collection_length__ = 5

SystemStatus being a Pydantic class with a field like cpus = Optional[conlist(CpuStatus)]

This does not work. If I change it to cpus = Optional[conlist(int)] it works.

URL to code causing the issue

No response

MCVE

# Your MCVE code here

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

None

Release Version

2.7.2

Platform

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

Funding

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
Fund with Polar

What is the value that you are getting? Is it None or a list?

None

Could you try removing the Optional and see what you get??

Then it works ! But I can't remove that Optional as this is auto generated.

Aah gotcha. You can set the __allow_none_optionals__ value to False on your factory.

class SystemStatusFactory(ModelFactory):
        __model__ = SystemStatus
        __randomize_collection_length__ = True
        __min_collection_length__ = 2
        __max_collection_length__ = 5
        __allow_none_optionals__ = False

Let me know if that fixes it :)

Yes this fixes it ! thank you !!! Can you add this to the doc s?

Yes this fixes it ! thank you !!! Can you add this to the doc s?

Yupp I've created an issue for this.