Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Conversation

@priyankc
Copy link
Member

@priyankc priyankc commented Jun 10, 2025

Description

Fixes the random state handling in the Use function to ensure it respects the seeded random instance from ModelFactory.seed_random().

Testing

Added a unit test to repro and fix.

Before the fix

((.venv) ) ➜  polyfactory git:(workback-fix-580) ✗ pytest tests/test_random_seed.py
=============================================== test session starts ================================================
platform darwin -- Python 3.12.10, pytest-8.4.0, pluggy-1.6.0
rootdir: /Users/priyank/OSS/polyfactory
configfile: pyproject.toml
plugins: hypothesis-6.135.4, Faker-37.3.0, cov-6.1.1, asyncio-1.0.0
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 4 items

tests/test_random_seed.py .F..                                                                               [100%]

===================================================== FAILURES =====================================================
_____________________________________ test_deterministic_use_function_seeding ______________________________________

    def test_deterministic_use_function_seeding():
        class MyModel(BaseModel):
            choice_value: str

        choices = ["option1", "option2", "option3", "option4", "option5"]

        class MyModelFactory(ModelFactory):
            __model__ = MyModel
            choice_value = Use(ModelFactory.__random__.choice, choices)

        ModelFactory.seed_random(12345)
        first_batch = [MyModelFactory.build().choice_value for _ in range(10)]


        ModelFactory.seed_random(12345) # Seed random again with the same value
        second_batch = [MyModelFactory.build().choice_value for _ in range(10)]

>       assert first_batch == second_batch
E       AssertionError: assert ['option2', '...option5', ...] == ['option2', '...option4', ...]
E
E         At index 1 diff: 'option3' != 'option4'
E         Use -v to get more diff

tests/test_random_seed.py:46: AssertionError
============================================= short test summary info ==============================================
FAILED tests/test_random_seed.py::test_deterministic_use_function_seeding - AssertionError: assert ['option2', '...option5', ...] == ['option2', '...option4', ...]
=========================================== 1 failed, 3 passed in 0.42s ============================================

After the fix

((.venv) ) ➜  polyfactory git:(workback-fix-580) ✗ pytest tests/test_random_seed.py
=============================================== test session starts ================================================
platform darwin -- Python 3.12.10, pytest-8.4.0, pluggy-1.6.0
rootdir: /Users/priyank/OSS/polyfactory
configfile: pyproject.toml
plugins: hypothesis-6.135.4, Faker-37.3.0, cov-6.1.1, asyncio-1.0.0
asyncio: mode=Mode.AUTO, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 4 items

tests/test_random_seed.py ....                                                                               [100%]

================================================ 4 passed in 0.37s =================================================

Closes

Fixes: litestar-org#580

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: unexpected behavior with seed_random

2 participants