Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Broken union type generation since "polyfactory<2.6" (pydantic_factory) #366

Closed
realitycheck opened this issue Sep 15, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@realitycheck
Copy link

realitycheck commented Sep 15, 2023

Description

Value generation for union_field: str | list[str] types is broken since polyfactory<2.6.

Sample of broken results:

{'union_field': ['xHTRynXkQXaHKksrCLan', ['mlAkGEPvArmUXfHMUDvh']]}
{'union_field': ['AOQqrBoBIUXjkkDazQMu', ['EDNDtAdsaLdPVrSjwrDo']]}
{'union_field': ['cVPkYHEIYQOEVCbYEOiS', ['evgTnOLFzcVsbaZWjmim']]}

URL to code causing the issue

No response

MCVE

import pydantic

from polyfactory.factories.pydantic_factory import ModelFactory


class UnionModel(pydantic.BaseModel):
    union_field: str | list[str]


class UnionFactory(ModelFactory):
    __model__ = UnionModel

for _ in range(100):
    print(UnionFactory.process_kwargs())

Steps to reproduce

1. Install "polyfactory<2.6"
2. Run MCVE
3. Notice correct results (sample):

{'union_field': ['pxmZfDPIiXJBzmcMiDFC']}
{'union_field': ['qmaITGzIrhtIbwXSNCHF']}
{'union_field': ['FTTgnfVwmySLgdbylTkQ']}
{'union_field': ['EonBSyUuDseCjXhuzONc']}
{'union_field': ['QqBzlNBMKRrlLuEmiDBl']}
{'union_field': 'IfiTUXnKFaCgnrvCnEpi'}
{'union_field': ['GZaprKiCagdtrSNciVQa']}
  1. Install "polyfactory>=2.6"
  2. Run MCVE
  3. Notice incorrect results (sample):
{'union_field': ['xHTRynXkQXaHKksrCLan', ['mlAkGEPvArmUXfHMUDvh']]}
{'union_field': ['AOQqrBoBIUXjkkDazQMu', ['EDNDtAdsaLdPVrSjwrDo']]}
{'union_field': ['cVPkYHEIYQOEVCbYEOiS', ['evgTnOLFzcVsbaZWjmim']]}


### Screenshots

_No response_

### Logs

_No response_

### Release Version

polyfactory>=2.6,<3

### Platform

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

<!-- POLAR PLEDGE BADGE START -->
---

## 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

<a href="https://polar.sh/litestar-org/polyfactory/issues/366">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/polyfactory/issues/366/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/polyfactory/issues/366/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
@realitycheck realitycheck added the bug Something isn't working label Sep 15, 2023
@guacs
Copy link
Member

guacs commented Sep 16, 2023

Yeah this is definitely a bug. Thank you for reporting this :)

mdczaplicki pushed a commit to mdczaplicki/polyfactory that referenced this issue Sep 19, 2023
@guacs guacs mentioned this issue Oct 18, 2023
5 tasks
@adhtruong adhtruong mentioned this issue Jan 3, 2024
5 tasks
@adhtruong
Copy link
Collaborator

This seems like it's fixed on main now by #468. Below is the output of the original example

➜  polyfactory git:(main) ✗ pdm run python -m a
{'union_field': ['RWjcFtahQiPQtoUvVwEz']}
{'union_field': 'SSYSdEVUTbiyGUfWRlSn'}
{'union_field': ['cepYPKWhGknvHXGykLXm']}
{'union_field': ['gmVhBhPhYvhfGsfDRPrV']}
{'union_field': 'zbmGlcaQcjdHCEnYmxkd'}
{'union_field': 'PIXzWGqKEQdCoslhuHTN'}
{'union_field': 'xqVijCVLcMcEWuNZwItl'}
{'union_field': 'bGZVfMnctOfkRUONRqPj'}
{'union_field': ['fwrQzvnToUnFLEeyfVOO']}
{'union_field': 'OSrMqpcnTFtjhOqUIAMF'}

@guacs
Copy link
Member

guacs commented Jan 19, 2024

Seems it's not fixed yet in Pydantic v1 :/ I think we may have to just special case pydantic v1 as mentioned here.

@guacs
Copy link
Member

guacs commented Jan 21, 2024

Fixed in #491.

@guacs guacs closed this as completed Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants