litestar-org / litestar-org/polyfactory

Bug: Pydantic Config field `min_anystr_length` not taken into account

Open
#442 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Description

In a Pydantic model, when using `Config` field `min_anystr_length` (to set a minimum length for all strings in the model), Polyfactory does not seem to take it into account which leads to pydantic validation errors during build.

My understanding is that currently, Polyfactory only parses the `allow_population_by_field_name` ([here](https://github.com/litestar-org/polyfactory/blob/b479e4af4617d108cbd90890cbda4408ee51ba4a/polyfactory/factories/pydantic_factory.py#L340)) from the `Config`.

Is this assessment correct ? Thank you for your help !

### URL to code causing the issue

_No response_

### MCVE

```python
from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel

ModelFactory.seed_random(0)

class TestClass(BaseModel):
class Config:
min_anystr_length = 100
myfield: str

class TestClassFactory(ModelFactory[TestClass]):
__model__ = TestClass

class TestClassWithoutConfig(BaseModel):
myfield: str

class TestClassWithoutConfigFactory(ModelFactory[TestClassWithoutConfig]):
__model__ = TestClassWithoutConfig

print(TestClassWithoutConfigFactory.build()) # OK
print(TestClassFactory.build()) # ERROR

#E pydantic.error_wrappers.ValidationError: 1 validation error for TestClass
#E myfield
#E ensure this value has at least 100 characters (type=value_error.any_str.min_length; #limit_value=100)
```

### Steps to reproduce

```bash
Execute the example above (pydantic 1.10)
```

### Screenshots

_No response_

### Logs

```bash
E pydantic.error_wrappers.ValidationError: 1 validation error for TestClass
E myfield
E ensure this value has at least 100 characters (type=value_error.any_str.min_length; limit_value=100)
```

### Release Version

2.12.0

### Platform

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Pydantic 1.10 MCVE, then inspect polyfactory/factories/pydantic_factory.py around the Config handling referenced in the issue. Confirm how the factory generates string values and verify that TestClassFactory.build() succeeds with a value meeting the 100-character minimum.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.