litestar-org / litestar-org/polyfactory
Bug: empty string generated for regex requiring at least one char
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 120
- PR merge metrics
- No merged PRs in 30d
Description
### Description
Regex seems to be ignored inside dicts, as Polyfactory is generating `` as a value:
{'environment_variables': {'XZdrHqToIHlKKDxepfKJ': ''}}
Calling `build()` 10 times results in `None` 6 times, and `` 4 times.
--
This does NOT happen when the value is not nullable, i.e.:
```
class Model(BaseModel):
environment_variables: Dict[str, constr(regex=r"^[A-Za-z_]+$")]
```
--
This ALSO happens when key/value are swapped:
```
class Model(BaseModel):
environment_variables: Optional[Dict[constr(regex=r"^[A-Za-z_]+$"), str]]
```
Result:
{'environment_variables': {'': 'BildVEhMalDZKoEqvGvl'}}
--
This does NOT happen outside a dict:
```
class Model(BaseModel):
environment_variables: constr(regex=r"^[A-Za-z_]+$")
```
Result:
{'environment_variables': 'XCjdsB'}
### URL to code causing the issue
_No response_
### MCVE
```python
from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel
from typing import Optional,Dict
from pydantic import constr,Field
class Model(BaseModel):
environment_variables: Optional[Dict[str, constr(regex=r"^[A-Za-z_]+$")]]
class Factory(ModelFactory[Model]):
...
print(Factory.build())
```
### Steps to reproduce
```bash
See MRE
```
### Screenshots
_No response_
### Logs
```bash
```
### Release Version
2.21.0
### Platform
- [ ] Linux
- [x] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)
Contributor guide
Research direction
Start with the MCVE using ModelFactory[Model] and the Pydantic model shown in the issue, then inspect how dictionary keys and values are generated for Optional fields. Reproduce the output across repeated builds; done means regex-constrained dictionary keys and values never become empty strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100