Repository: joke2k/faker

Issue:
Reseed code broke in 0.8 unnecessarily
Commit ea4f189bbff1925d7a7e2d7cbc6e91e2e8a9a3f3 changed the name of the `random.Random()` object in `faker.generator` from `random` to `mod_random`, re-exposing the `random` module as the name `faker.generator.random`. This broke code like:

```py
from faker.generator import random
random.seed(1)
``` 

which is basically what my `pytest-randomly` plugin does. At first I thought this was #586 but then I saw that every run was broken. The failure is silent because `random` is still a name in `faker.generator`, it just now points at the global `random` module 😱

I suggest just doing some renaming in the module to fix this so old code continues to work.


Discussion:

Resolving patch:
diff --git a/faker/generator.py b/faker/generator.py
--- a/faker/generator.py
+++ b/faker/generator.py
@@ -3,11 +3,12 @@
 from __future__ import unicode_literals
 
 import re
-import random
+import random as random_module
 
 
 _re_token = re.compile(r'\{\{(\s?)(\w+)(\s?)\}\}')
-mod_random = random.Random()
+random = random_module.Random()
+mod_random = random  # compat with name released in 0.8
 
 
 class Generator(object):
@@ -18,7 +19,7 @@ def __init__(self, **config):
         self.providers = []
         self.__config = dict(
             list(self.__config.items()) + list(config.items()))
-        self.__random = mod_random
+        self.__random = random
 
     def add_provider(self, provider):
 
@@ -57,14 +58,14 @@ def random(self):
 
     def seed_instance(self, seed=None):
         """Calls random.seed"""
-        if self.__random == mod_random:
+        if self.__random == random:
             # create per-instance random obj when first time seed_instance() is called
-            self.__random = random.Random()
+            self.__random = random_module.Random()
         self.__random.seed(seed)
 
     @classmethod
     def seed(cls, seed=None):
-        mod_random.seed(seed)
+        random.seed(seed)
 
     def format(self, formatter, *args, **kwargs):
         """
diff --git a/faker/utils/distribution.py b/faker/utils/distribution.py
--- a/faker/utils/distribution.py
+++ b/faker/utils/distribution.py
@@ -1,7 +1,8 @@
 # coding=utf-8
 
 import bisect
-from faker.generator import mod_random
+from faker.generator import random as mod_random
+
 
 def random_sample(random=None):
     if random is None:


Test patch:
diff --git a/tests/test_factory.py b/tests/test_factory.py
--- a/tests/test_factory.py
+++ b/tests/test_factory.py
@@ -5,7 +5,6 @@
 import re
 import unittest
 import string
-import six
 import sys
 
 try:
@@ -14,7 +13,7 @@
     from io import StringIO
 
 from faker import Generator, Factory
-from faker.generator import mod_random as random
+from faker.generator import random
 from faker.utils import text, decorators
 from . import string_types
 
diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py
--- a/tests/utils/__init__.py
+++ b/tests/utils/__init__.py
@@ -2,7 +2,7 @@
 from faker.utils.distribution import choice_distribution
 from faker.utils.datasets import add_dicts
 from faker.config import PROVIDERS
-from faker.generator import mod_random
+from faker.generator import random
 from faker.utils.loading import find_available_providers
 from faker.config import META_PROVIDERS_MODULES
 from importlib import import_module
@@ -25,7 +25,7 @@ def test_choice_distribution(self):
             random_state = json.load(fh)
         random_state[1] = tuple(random_state[1])
 
-        mod_random.setstate(random_state)
+        random.setstate(random_state)
         samples = [choice_distribution(a, p) for i in range(100)]
         a_pop = len([i for i in samples if i == 'a'])
         b_pop = len([i for i in samples if i == 'b'])