Coverage for pyodmongo/models/fields.py: 100%
15 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-16 15:08 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-16 15:08 +0000
1from __future__ import annotations as _annotations
3import typing
4from typing import Any
6from pydantic_core import PydanticUndefined
7from typing_extensions import Literal, Unpack
9from pydantic.fields import AliasPath, AliasChoices, _EmptyKwargs
10from pydantic import Field as Pydantic_Field
13_Unset: Any = PydanticUndefined
16def Field( # noqa: C901
17 default: Any = PydanticUndefined,
18 *,
19 default_factory: typing.Callable[[], Any] | None = _Unset,
20 alias: str | None = _Unset,
21 alias_priority: int | None = _Unset,
22 validation_alias: str | AliasPath | AliasChoices | None = _Unset,
23 serialization_alias: str | None = _Unset,
24 title: str | None = _Unset,
25 description: str | None = _Unset,
26 examples: list[Any] | None = _Unset,
27 exclude: bool | None = _Unset,
28 discriminator: str | None = _Unset,
29 json_schema_extra: dict[str, Any] | None = _Unset,
30 frozen: bool | None = _Unset,
31 validate_default: bool | None = _Unset,
32 repr: bool = _Unset,
33 init_var: bool | None = _Unset,
34 kw_only: bool | None = _Unset,
35 pattern: str | None = _Unset,
36 strict: bool | None = _Unset,
37 gt: float | None = _Unset,
38 ge: float | None = _Unset,
39 lt: float | None = _Unset,
40 le: float | None = _Unset,
41 multiple_of: float | None = _Unset,
42 allow_inf_nan: bool | None = _Unset,
43 max_digits: int | None = _Unset,
44 decimal_places: int | None = _Unset,
45 min_length: int | None = _Unset,
46 max_length: int | None = _Unset,
47 union_mode: Literal["smart", "left_to_right"] = _Unset,
48 index: bool = False,
49 unique: bool = False,
50 text_index: bool = False,
51 default_language: str = None,
52 **extra: Unpack[_EmptyKwargs],
53) -> Any:
54 """Usage docs: https://docs.pydantic.dev/2.2/usage/fields
56 Create a field for objects that can be configured.
58 Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
59 apply only to number fields (`int`, `float`, `Decimal`) and some apply only to `str`.
61 Note:
62 - Any `_Unset` objects will be replaced by the corresponding value defined in the `_DefaultValues` dictionary. If a key for the `_Unset` object is not found in the `_DefaultValues` dictionary, it will default to `None`
64 Args:
65 default: Default value if the field is not set.
66 default_factory: A callable to generate the default value, such as :func:`~datetime.utcnow`.
67 alias: An alternative name for the attribute.
68 alias_priority: Priority of the alias. This affects whether an alias generator is used.
69 validation_alias: 'Whitelist' validation step. The field will be the single one allowed by the alias or set of
70 aliases defined.
71 serialization_alias: 'Blacklist' validation step. The vanilla field will be the single one of the alias' or set
72 of aliases' fields and all the other fields will be ignored at serialization time.
73 title: Human-readable title.
74 description: Human-readable description.
75 examples: Example values for this field.
76 exclude: Whether to exclude the field from the model serialization.
77 discriminator: Field name for discriminating the type in a tagged union.
78 json_schema_extra: Any additional JSON schema data for the schema property.
79 frozen: Whether the field is frozen.
80 validate_default: Run validation that isn't only checking existence of defaults. This can be set to `True` or `False`. If not set, it defaults to `None`.
81 repr: A boolean indicating whether to include the field in the `__repr__` output.
82 init_var: Whether the field should be included in the constructor of the dataclass.
83 kw_only: Whether the field should be a keyword-only argument in the constructor of the dataclass.
84 strict: If `True`, strict validation is applied to the field.
85 See [Strict Mode](../usage/strict_mode.md) for details.
86 gt: Greater than. If set, value must be greater than this. Only applicable to numbers.
87 ge: Greater than or equal. If set, value must be greater than or equal to this. Only applicable to numbers.
88 lt: Less than. If set, value must be less than this. Only applicable to numbers.
89 le: Less than or equal. If set, value must be less than or equal to this. Only applicable to numbers.
90 multiple_of: Value must be a multiple of this. Only applicable to numbers.
91 min_length: Minimum length for strings.
92 max_length: Maximum length for strings.
93 pattern: Pattern for strings.
94 allow_inf_nan: Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
95 max_digits: Maximum number of allow digits for strings.
96 decimal_places: Maximum number of decimal places allowed for numbers.
97 union_mode: The strategy to apply when validating a union. Can be `smart` (the default), or `left_to_right`.
98 See [Union Mode](../usage/types/unions.md#union-mode) for details.
99 extra: Include extra fields used by the JSON schema.
101 !!! warning Deprecated
102 The `extra` kwargs is deprecated. Use `json_schema_extra` instead.
104 Returns:
105 A new [`FieldInfo`][pydantic.fields.FieldInfo], the return annotation is `Any` so `Field` can be used on
106 type annotated fields without causing a typing error.
107 """
108 # Check deprecated and removed params from V1. This logic should eventually be removed.
109 json_schema_extra = (
110 {}
111 if json_schema_extra == PydanticUndefined or json_schema_extra is None
112 else json_schema_extra
113 )
114 json_schema_extra["index"] = index
115 json_schema_extra["unique"] = unique
116 json_schema_extra["text_index"] = text_index
117 json_schema_extra["default_language"] = default_language
118 return Pydantic_Field(
119 default=default,
120 default_factory=default_factory,
121 alias=alias,
122 alias_priority=alias_priority,
123 validation_alias=validation_alias,
124 serialization_alias=serialization_alias,
125 title=title,
126 description=description,
127 examples=examples,
128 exclude=exclude,
129 discriminator=discriminator,
130 json_schema_extra=json_schema_extra,
131 frozen=frozen,
132 validate_default=validate_default,
133 repr=repr,
134 init_var=init_var,
135 kw_only=kw_only,
136 pattern=pattern,
137 strict=strict,
138 gt=gt,
139 ge=ge,
140 lt=lt,
141 le=le,
142 multiple_of=multiple_of,
143 allow_inf_nan=allow_inf_nan,
144 max_digits=max_digits,
145 decimal_places=decimal_places,
146 min_length=min_length,
147 max_length=max_length,
148 union_mode=union_mode,
149 **extra,
150 )