Skip to content

Commit

Permalink
Fix seeding with multiple locales (#1754)
Browse files Browse the repository at this point in the history
* remove re-seeding on every provider method call

* test update

* cleanup

* test comment and make lint
  • Loading branch information
johntmyers committed Nov 22, 2022
1 parent 9fa6019 commit 2826e71
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 1 addition & 3 deletions faker/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .config import DEFAULT_LOCALE
from .exceptions import UniquenessException
from .factory import Factory
from .generator import Generator, Sentinel, random
from .generator import Generator, random
from .typing import SeedType
from .utils.distribution import choices_distribution

Expand Down Expand Up @@ -153,8 +153,6 @@ def _select_factory(self, method_name: str) -> Factory:
elif len(factories) == 1:
return factories[0]

if Generator._global_seed is not Sentinel:
random.seed(Generator._global_seed) # type: ignore
if weights:
factory = self._select_factory_distribution(factories, weights)
else:
Expand Down
13 changes: 10 additions & 3 deletions tests/test_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@ def test_seed_classmethod(self):

def test_seed_class_locales(self):
Faker.seed(2043)
count = 5
fake = Faker(["en_GB", "fr_FR", "en_IN"])
name = fake.name()
first_list = [fake.name() for _ in range(count)]
# We convert the list to a set to remove duplicates and ensure
# that we have exactly `count` unique fake values
assert len(set(first_list)) == count

for _ in range(5):
assert fake.name() == name
Faker.seed(2043)
fake = Faker(["en_GB", "fr_FR", "en_IN"])
second_list = [fake.name() for _ in range(count)]

assert first_list == second_list

def test_seed_instance(self):
locale = ["de_DE", "en-US", "en-PH", "ja_JP"]
Expand Down

0 comments on commit 2826e71

Please sign in to comment.