Skip to content

Commit

Permalink
Add a default value for FieldMeta random args (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Jun 18, 2023
1 parent 17d6fb3 commit 5f2e278
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 3 additions & 0 deletions polyfactory/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import abc, defaultdict, deque
from random import Random
from typing import (
DefaultDict,
Deque,
Expand Down Expand Up @@ -39,3 +40,5 @@
}

IGNORED_TYPE_ARGS: Set = {Ellipsis}

DEFAULT_RANDOM = Random()
7 changes: 3 additions & 4 deletions polyfactory/field_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import TYPE_CHECKING, Any, Literal, Pattern, TypedDict, cast

from polyfactory.constants import IGNORED_TYPE_ARGS, TYPE_MAPPING
from polyfactory.constants import DEFAULT_RANDOM, IGNORED_TYPE_ARGS, TYPE_MAPPING
from polyfactory.utils.helpers import normalize_annotation, unwrap_annotated, unwrap_args, unwrap_new_type
from polyfactory.utils.predicates import is_annotated

Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(
*,
name: str,
annotation: type,
random: Random,
random: Random = DEFAULT_RANDOM,
default: Any = Null,
children: list[FieldMeta] | None = None,
constraints: Constraints | None = None,
Expand All @@ -83,7 +83,6 @@ def __init__(
def type_args(self) -> tuple[Any, ...]:
"""Return the normalized type args of the annotation, if any.
:param random: An instance of random.Random.
:returns: a tuple of types.
"""
return tuple(arg for arg in unwrap_args(self.annotation, random=self.random) if arg not in IGNORED_TYPE_ARGS)
Expand All @@ -92,7 +91,7 @@ def type_args(self) -> tuple[Any, ...]:
def from_type(
cls,
annotation: Any,
random: Random,
random: Random = DEFAULT_RANDOM,
name: str = "",
default: Any = Null,
constraints: Constraints | None = None,
Expand Down

0 comments on commit 5f2e278

Please sign in to comment.