Skip to content

Commit

Permalink
fix typing issue with seed
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Apr 28, 2022
1 parent d3f754b commit 9c30b22
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions faker/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing import TYPE_CHECKING, Any, Callable, Dict, Hashable, List, Optional

from .typing import SeedType

if TYPE_CHECKING:
from .providers import BaseProvider

Expand Down Expand Up @@ -65,7 +67,7 @@ def random(self) -> random_module.Random:
def random(self, value: random_module.Random) -> None:
self.__random = value

def seed_instance(self, seed: Optional[Hashable] = None) -> "Generator":
def seed_instance(self, seed: Optional[SeedType] = None) -> "Generator":
"""Calls random.seed"""
if self.__random == random:
# create per-instance random obj when first time seed_instance() is
Expand All @@ -76,7 +78,7 @@ def seed_instance(self, seed: Optional[Hashable] = None) -> "Generator":
return self

@classmethod
def seed(cls, seed: Optional[Hashable] = None) -> None:
def seed(cls, seed: Optional[SeedType] = None) -> None:
random.seed(seed)
cls._global_seed = seed
cls._is_seeded = True
Expand Down
9 changes: 5 additions & 4 deletions faker/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .exceptions import UniquenessException
from .factory import Factory
from .generator import Generator, Sentinel, random
from .typing import SeedType
from .utils.distribution import choices_distribution

_UNIQUE_ATTEMPTS = 1000
Expand Down Expand Up @@ -153,7 +154,7 @@ def _select_factory(self, method_name: str) -> Factory:
return factories[0]

if Generator._global_seed is not Sentinel:
random.seed(Generator._global_seed)
random.seed(Generator._global_seed) # type: ignore
if weights:
factory = self._select_factory_distribution(factories, weights)
else:
Expand Down Expand Up @@ -200,15 +201,15 @@ def _map_provider_method(self, method_name: str) -> Tuple[List[Factory], Optiona
return mapping

@classmethod
def seed(cls, seed: Optional[Hashable] = None) -> None:
def seed(cls, seed: Optional[SeedType] = None) -> None:
"""
Hashables the shared `random.Random` object across all factories
:param seed: seed value
"""
Generator.seed(seed)

def seed_instance(self, seed: Optional[Hashable] = None) -> None:
def seed_instance(self, seed: Optional[SeedType] = None) -> None:
"""
Creates and seeds a new `random.Random` object for each factory
Expand All @@ -217,7 +218,7 @@ def seed_instance(self, seed: Optional[Hashable] = None) -> None:
for factory in self._factories:
factory.seed_instance(seed)

def seed_locale(self, locale: str, seed: Optional[Hashable] = None) -> None:
def seed_locale(self, locale: str, seed: Optional[SeedType] = None) -> None:
"""
Creates and seeds a new `random.Random` object for the factory of the specified locale
Expand Down
1 change: 1 addition & 0 deletions faker/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
DateParseType = Union[date, datetime, timedelta, str, int]
HueType = TypeVar("HueType", str, float, Sequence[int])
GenderType = TypeVar("GenderType", bound=Literal["M", "F"])
SeedType = Union[int, float, str, bytes, bytearray, None]

0 comments on commit 9c30b22

Please sign in to comment.