litestar-org / litestar-org/polyfactory

Factory doesn't pick up `faker_session_locale` fixture changes, generates data with default settings

Open
#715 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

### Description

## Issue Description
When running tests and modifying Faker settings within the `faker_session_locale` fixture to generate data in a specific language (e.g., Russian), any Factory does not recognize these changes and continues generating data based on the default Faker object settings. As a result, data is generated with default settings instead of the expected Russian localization.

## Proposed Solution
This issue can be resolved by creating a project-level fixture that replaces the Faker object instantiated when creating Factory objects. Since the problem manifests at the test level, the solution should be implemented at the same level.

### MCVE
To write minimal code, you need to create several files and download several libraries:
```bash
uv init \
&& mkdir tests \
&& touch ./tests/conftest.py ./tests/test_example.py \
&& uv add polyfactory==2.21.0 pydantic pytest

# If not UV
python -m venv .venv \
&& source .venv/bin/activate \
&& mkdir tests \
&& touch ./tests/conftest.py ./tests/test_example.py \
&& pip install polyfactory==2.21.0 pydantic pytest
```

`tests/conftest.py`
```python
import pytest

@pytest.fixture(scope='session', autouse=True)
def faker_session_locale():
return ['ru_RU']
```

`tests/test_example.py`
```python
import re

from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory import Use
from pydantic import BaseModel
from faker import Faker

class ExampleModel(BaseModel):
field: str

class ExampleFactory(ModelFactory[ExampleModel]):
field = Use(ModelFactory.__faker__.word)

def test_exmaple(faker: Faker):
factory = ExampleFactory.build()
assert bool(re.search(r'[а-яёА-ЯЁ]', factory.field)), (factory.field, faker.word())

```

This test will fail because the answer will consist of English letters.

### Release Version

2.21.0

### Platform

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

Contributor guide

Open the contributing guide

Research direction

Start with the reproduction in tests/conft.py and tests/test_example.py, then trace ModelFactory.__faker__ and the faker fixture used by the test. Run the provided pytest example with polyfactory 2.21.0 and verify that changing faker_session_locale makes ExampleFactory.build() generate Russian-localized data rather than English words.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.