1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
from __future__ import annotations
import functools
from dataclasses import asdict
from typing import TYPE_CHECKING, Any, Hashable, Literal, Mapping, Pattern, TypedDict, cast
from typing_extensions import get_args, get_origin
from polyfactory.constants import DEFAULT_RANDOM, TYPE_MAPPING
from polyfactory.utils.deprecation import check_for_deprecated_parameters
from polyfactory.utils.helpers import (
get_annotation_metadata,
is_dataclass_instance,
unwrap_annotated,
unwrap_new_type,
)
from polyfactory.utils.normalize_type import normalize_type
from polyfactory.utils.predicates import is_annotated
from polyfactory.utils.types import NoneType
if TYPE_CHECKING:
import datetime
from decimal import Decimal
from random import Random
from typing import Sequence
from typing_extensions import NotRequired, Self
class Null:
"""Sentinel class for empty values"""
class UrlConstraints(TypedDict):
max_length: NotRequired[int]
allowed_schemes: NotRequired[list[str]]
host_required: NotRequired[bool]
default_host: NotRequired[str]
default_port: NotRequired[int]
default_path: NotRequired[str]
class Constraints(TypedDict):
"""Metadata regarding a type constraints, if any"""
allow_inf_nan: NotRequired[bool]
decimal_places: NotRequired[int]
ge: NotRequired[int | float | Decimal]
gt: NotRequired[int | float | Decimal]
item_type: NotRequired[Any]
le: NotRequired[int | float | Decimal]
lower_case: NotRequired[bool]
lt: NotRequired[int | float | Decimal]
max_digits: NotRequired[int]
max_length: NotRequired[int]
min_length: NotRequired[int]
multiple_of: NotRequired[int | float | Decimal]
path_type: NotRequired[Literal["file", "dir", "new"]]
pattern: NotRequired[str | Pattern]
tz: NotRequired[datetime.tzinfo]
unique_items: NotRequired[bool]
upper_case: NotRequired[bool]
url: NotRequired[UrlConstraints]
uuid_version: NotRequired[Literal[1, 3, 4, 5]]
class FieldMeta:
"""Factory field metadata container. This class is used to store the data about a field of a factory's model."""
__slots__ = ("__dict__", "annotation", "children", "constraints", "default", "name", "random")
annotation: Any
random: Random
children: list[FieldMeta] | None
default: Any
name: str
constraints: Constraints | None
def __init__(
self,
*,
name: str,
annotation: type,
random: Random | None = None,
default: Any = Null,
children: list[FieldMeta] | None = None,
constraints: Constraints | None = None,
) -> None:
"""Create a factory field metadata instance."""
check_for_deprecated_parameters("2.20.0", parameters=(("random", random),))
self.annotation = annotation
self.random = random or DEFAULT_RANDOM
self.children = children
self.default = default
self.name = name
self.constraints = constraints
def __repr__(self) -> str:
"""Return a string representation of the field meta."""
return f"FieldMeta(name={self.name!r}, annotation={self.annotation!r}, default={self.default!r}, children={self.children!r}, constraints={self.constraints!r})"
@functools.cached_property
def type_args(self) -> tuple[Any, ...]:
"""Return the normalized type args of the annotation, if any.
:returns: a tuple of types.
"""
annotation = self.annotation
if is_annotated(annotation):
annotation = get_args(self.annotation)[0]
return tuple(
TYPE_MAPPING.get(arg, arg) if isinstance(arg, Hashable) else arg # pyright: ignore[reportCallIssue,reportArgumentType]
for arg in get_args(annotation)
)
@classmethod
def from_type(
cls,
annotation: Any,
random: Random | None = None,
name: str = "",
default: Any = Null,
constraints: Constraints | None = None,
randomize_collection_length: bool | None = None,
min_collection_length: int | None = None,
max_collection_length: int | None = None,
children: list[FieldMeta] | None = None,
) -> Self:
"""Builder method to create a FieldMeta from a type annotation.
:param annotation: A type annotation.
:param random: An instance of random.Random.
:param name: Field name
:param default: Default value, if any.
:param constraints: A dictionary of constraints, if any.
:param randomize_collection_length: A boolean flag whether to randomize collections lengths
:param min_collection_length: Minimum number of elements in randomized collection
:param max_collection_length: Maximum number of elements in randomized collection
:returns: A field meta instance.
"""
check_for_deprecated_parameters(
"2.11.0",
parameters=(
("randomize_collection_length", randomize_collection_length),
("min_collection_length", min_collection_length),
("max_collection_length", max_collection_length),
("random", random),
),
)
annotation = normalize_type(annotation)
annotated = is_annotated(annotation)
if not constraints and annotated:
metadata = cls.get_constraints_metadata(annotation)
constraints = cls.parse_constraints(metadata)
# annotations can take many forms: Optional, an Annotated type, or anything with __args__
# in order to normalize the annotation, we need to unwrap the annotation.
if not annotated and (origin := get_origin(annotation)) and origin in TYPE_MAPPING:
container = TYPE_MAPPING[origin]
annotation = container[get_args(annotation)] # type: ignore[index]
field = cls(
annotation=annotation,
name=name,
default=default,
children=children,
constraints=constraints,
)
if field.type_args and not field.children:
field.children = [
cls.from_type(annotation=unwrap_new_type(arg)) for arg in field.type_args if arg is not NoneType
]
return field
@classmethod
def parse_constraints(cls, metadata: Sequence[Any]) -> "Constraints":
constraints = {}
for value in metadata:
if is_annotated(value):
_, inner_metadata = unwrap_annotated(value)
constraints.update(cast("dict[str, Any]", cls.parse_constraints(metadata=inner_metadata)))
elif func := getattr(value, "func", None):
if func is str.islower:
constraints["lower_case"] = True
elif func is str.isupper:
constraints["upper_case"] = True
elif func is str.isascii:
constraints["pattern"] = "[[:ascii:]]"
elif func is str.isdigit:
constraints["pattern"] = "[[:digit:]]"
elif is_dataclass_instance(value) and (value_dict := asdict(value)) and ("allowed_schemes" in value_dict):
constraints["url"] = {k: v for k, v in value_dict.items() if v is not None}
# This is to support `Constraints`, but we can't do a isinstance with `Constraints` since isinstance
# checks with `TypedDict` is not supported.
elif isinstance(value, Mapping):
constraints.update(value)
else:
constraints.update(
{
k: v
for k, v in {
"allow_inf_nan": getattr(value, "allow_inf_nan", None),
"decimal_places": getattr(value, "decimal_places", None),
"ge": getattr(value, "ge", None),
"gt": getattr(value, "gt", None),
"item_type": getattr(value, "item_type", None),
"le": getattr(value, "le", None),
"lower_case": getattr(value, "to_lower", None),
"lt": getattr(value, "lt", None),
"max_digits": getattr(value, "max_digits", None),
"max_length": getattr(value, "max_length", getattr(value, "max_length", None)),
"min_length": getattr(value, "min_length", getattr(value, "min_items", None)),
"multiple_of": getattr(value, "multiple_of", None),
"path_type": getattr(value, "path_type", None),
"pattern": getattr(value, "regex", getattr(value, "pattern", None)),
"tz": getattr(value, "tz", None),
"unique_items": getattr(value, "unique_items", None),
"upper_case": getattr(value, "to_upper", None),
"uuid_version": getattr(value, "uuid_version", None),
}.items()
if v is not None
},
)
return cast("Constraints", constraints)
@classmethod
def get_constraints_metadata(cls, annotation: Any) -> Sequence[Any]:
"""Get the metadatas of the constraints from the given annotation.
:param annotation: A type annotation.
:param random: An instance of random.Random.
:returns: A list of the metadata in the annotation.
"""
return get_annotation_metadata(annotation)
|