Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate FieldMeta collection params #417

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions polyfactory/factories/attrs_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ def get_model_fields(cls) -> list[FieldMeta]:
name=field.alias,
default=default_value,
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
),
)

Expand Down
3 changes: 0 additions & 3 deletions polyfactory/factories/dataclass_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ def get_model_fields(cls) -> list["FieldMeta"]:
name=field.name,
default=default_value,
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
),
)

Expand Down
11 changes: 1 addition & 10 deletions polyfactory/factories/msgspec_factory.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
from __future__ import annotations

from inspect import isclass
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
TypeVar,
)
from typing import TYPE_CHECKING, Any, Callable, Generic, TypeVar

from typing_extensions import get_type_hints

Expand Down Expand Up @@ -73,9 +67,6 @@ def get_model_fields(cls) -> list[FieldMeta]:
name=field.name,
default=default_value,
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
),
)
return fields_meta
68 changes: 26 additions & 42 deletions polyfactory/factories/pydantic_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,17 @@
from functools import partial
from os.path import realpath
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
ClassVar,
ForwardRef,
Generic,
Mapping,
Tuple,
TypeVar,
cast,
)
from typing import TYPE_CHECKING, Any, ClassVar, ForwardRef, Generic, Mapping, Tuple, TypeVar, cast
from uuid import NAMESPACE_DNS, uuid1, uuid3, uuid5

from typing_extensions import Literal, get_args, get_origin

from polyfactory.collection_extender import CollectionExtender
from polyfactory.constants import (
DEFAULT_RANDOM,
MAX_COLLECTION_LENGTH,
MIN_COLLECTION_LENGTH,
RANDOMIZE_COLLECTION_LENGTH,
)
from polyfactory.constants import DEFAULT_RANDOM
from polyfactory.exceptions import MissingDependencyException
from polyfactory.factories.base import BaseFactory
from polyfactory.field_meta import Constraints, FieldMeta, Null
from polyfactory.utils.deprecation import deprecated_parameter
from polyfactory.utils.helpers import unwrap_new_type, unwrap_optional
from polyfactory.utils.predicates import is_optional, is_safe_subclass, is_union
from polyfactory.value_generators.primitives import create_random_bytes
Expand Down Expand Up @@ -94,9 +80,9 @@ def from_field_info(
field_info: FieldInfo,
use_alias: bool,
random: Random | None,
randomize_collection_length: bool = RANDOMIZE_COLLECTION_LENGTH,
min_collection_length: int = MIN_COLLECTION_LENGTH,
max_collection_length: int = MAX_COLLECTION_LENGTH,
randomize_collection_length: bool | None = None,
min_collection_length: int | None = None,
max_collection_length: int | None = None,
) -> PydanticFieldMeta:
"""Create an instance from a pydantic field info.

Expand All @@ -110,6 +96,14 @@ def from_field_info(

:returns: A PydanticFieldMeta instance.
"""
deprecated_parameter(
"2.11.0",
parameters=(
("randomize_collection_length", randomize_collection_length),
("min_collection_length", min_collection_length),
("max_collection_length", max_collection_length),
),
)
if callable(field_info.default_factory):
default_value = field_info.default_factory()
else:
Expand All @@ -128,10 +122,7 @@ def from_field_info(
cls.from_field_info(
field_info=FieldInfo.from_annotation(arg),
field_name=field_name,
max_collection_length=max_collection_length,
min_collection_length=min_collection_length,
random=random,
randomize_collection_length=randomize_collection_length,
use_alias=use_alias,
)
for arg in get_args(annotation)
Expand Down Expand Up @@ -159,21 +150,18 @@ def from_field_info(
children=children,
constraints=cast("Constraints", {k: v for k, v in constraints.items() if v is not None}) or None,
default=default_value,
max_collection_length=max_collection_length,
min_collection_length=min_collection_length,
name=name,
random=random or DEFAULT_RANDOM,
randomize_collection_length=randomize_collection_length,
)

@classmethod
def from_model_field( # pragma: no cover
cls,
model_field: ModelField, # pyright: ignore[reportGeneralTypeIssues]
use_alias: bool,
randomize_collection_length: bool,
min_collection_length: int,
max_collection_length: int,
randomize_collection_length: bool | None = None,
min_collection_length: int | None = None,
max_collection_length: int | None = None,
random: Random = DEFAULT_RANDOM,
) -> PydanticFieldMeta:
"""Create an instance from a pydantic model field.
Expand All @@ -187,6 +175,14 @@ def from_model_field( # pragma: no cover
:returns: A PydanticFieldMeta instance.

"""
deprecated_parameter(
"2.11.0",
parameters=(
("randomize_collection_length", randomize_collection_length),
("min_collection_length", min_collection_length),
("max_collection_length", max_collection_length),
),
)
from pydantic import AmqpDsn, AnyHttpUrl, AnyUrl, HttpUrl, KafkaDsn, PostgresDsn, RedisDsn
from pydantic.fields import DeferredType, Undefined # type: ignore[attr-defined]

Expand Down Expand Up @@ -251,9 +247,6 @@ def from_model_field( # pragma: no cover

children: list[FieldMeta] = []
if model_field.key_field or model_field.sub_fields:
number_of_args = (
random.randint(min_collection_length, max_collection_length) if randomize_collection_length else 1
)
fields_to_iterate = (
([model_field.key_field, *model_field.sub_fields])
if model_field.key_field is not None
Expand All @@ -271,15 +264,12 @@ def from_model_field( # pragma: no cover
if get_origin(outer_type) in (tuple, Tuple) and get_args(outer_type)[-1] == Ellipsis:
# pydantic removes ellipses from Tuples in sub_fields
type_args += (...,)
extended_type_args = CollectionExtender.extend_type_args(annotation, type_args, number_of_args)
extended_type_args = CollectionExtender.extend_type_args(annotation, type_args, 1)
children.extend(
PydanticFieldMeta.from_model_field(
model_field=type_arg_to_sub_field[arg],
use_alias=use_alias,
random=random,
randomize_collection_length=randomize_collection_length,
min_collection_length=min_collection_length,
max_collection_length=max_collection_length,
)
for arg in extended_type_args
)
Expand Down Expand Up @@ -335,9 +325,6 @@ def get_model_fields(cls) -> list["FieldMeta"]:
field,
use_alias=not cls.__model__.__config__.allow_population_by_field_name, # type: ignore[attr-defined]
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
)
for field in cls.__model__.__fields__.values() # type: ignore[attr-defined]
]
Expand All @@ -348,9 +335,6 @@ def get_model_fields(cls) -> list["FieldMeta"]:
field_name=field_name,
random=cls.__random__,
use_alias=not cls.__model__.model_config.get("populate_by_name", False),
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
)
for field_name, field_info in cls.__model__.model_fields.items()
]
Expand Down
6 changes: 0 additions & 6 deletions polyfactory/factories/sqlalchemy_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,6 @@ def get_model_fields(cls) -> list[FieldMeta]:
annotation=cls.get_type_from_column(column),
name=name,
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
)
for name, column in table.columns.items()
if cls.should_column_be_set(column)
Expand All @@ -152,9 +149,6 @@ def get_model_fields(cls) -> list[FieldMeta]:
name=name,
annotation=annotation,
random=cls.__random__,
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
),
)

Expand Down
3 changes: 0 additions & 3 deletions polyfactory/factories/typed_dict_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def get_model_fields(cls) -> list["FieldMeta"]:
random=DEFAULT_RANDOM,
name=field_name,
default=getattr(cls.__model__, field_name, Null),
randomize_collection_length=cls.__randomize_collection_length__,
min_collection_length=cls.__min_collection_length__,
max_collection_length=cls.__max_collection_length__,
)
for field_name, annotation in model_type_hints.items()
]
Expand Down
26 changes: 13 additions & 13 deletions polyfactory/field_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
from typing_extensions import Mapping, get_args, get_origin

from polyfactory.collection_extender import CollectionExtender
from polyfactory.constants import (
DEFAULT_RANDOM,
MAX_COLLECTION_LENGTH,
MIN_COLLECTION_LENGTH,
RANDOMIZE_COLLECTION_LENGTH,
TYPE_MAPPING,
)
from polyfactory.constants import DEFAULT_RANDOM, TYPE_MAPPING
from polyfactory.utils.deprecation import deprecated_parameter
from polyfactory.utils.helpers import normalize_annotation, unwrap_annotated, unwrap_args, unwrap_new_type
from polyfactory.utils.predicates import is_annotated, is_any_annotated

Expand Down Expand Up @@ -110,9 +105,9 @@ def from_type(
name: str = "",
default: Any = Null,
constraints: Constraints | None = None,
randomize_collection_length: bool = RANDOMIZE_COLLECTION_LENGTH,
min_collection_length: int = MIN_COLLECTION_LENGTH,
max_collection_length: int = MAX_COLLECTION_LENGTH,
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.
Expand All @@ -128,6 +123,14 @@ def from_type(

:returns: A field meta instance.
"""
deprecated_parameter(
"2.11.0",
parameters=(
("randomize_collection_length", randomize_collection_length),
("min_collection_length", min_collection_length),
("max_collection_length", max_collection_length),
),
)
field_type = normalize_annotation(annotation, random=random)

if not constraints and is_annotated(annotation):
Expand Down Expand Up @@ -156,9 +159,6 @@ def from_type(
FieldMeta.from_type(
annotation=unwrap_new_type(arg),
random=random,
randomize_collection_length=randomize_collection_length,
min_collection_length=min_collection_length,
max_collection_length=max_collection_length,
)
for arg in extended_type_args
]
Expand Down
Loading
Loading