Fix: Ensure deterministic locale selection in multi-locale mode #2287
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this change
This PR fixes a bug where Faker instances initialized with multiple locales would produce non-deterministic output even when seeded. It ensures that the selection of which locale to use (e.g., picking between en_US or ru_RU) respects the instance seed.
What was wrong
When Faker is acting as a proxy for multiple locales, it needs to randomly select a factory for each call. Previously, the methods _select_factory_distribution and _select_factory_choice used the global random module (via choices_distribution or random.choice).
Because seed_instance() only seeds the internal factory generators and not the global random module, the selection of the locale remained random and unseeded. This caused different outputs on subsequent runs despite using the same seed.
How this fixes it
I modified faker/proxy.py to use self._factories[0].random instead of the global random module for locale selection.
Since seed_instance() iterates through self._factories and seeds every single one of them with the same value, accessing the random generator of the first factory (_factories[0]) provides a safe, seeded, and deterministic source of randomness. This fixes the determinism issue without needing to expose a random property on the Proxy class (which is explicitly forbidden by the current architecture).
Fixes #2283
Checklist
make lint