pboot-validator 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pboot_validator/__init__.py +461 -0
- pboot_validator/_internal/__init__.py +0 -0
- pboot_validator/_internal/_config.py +408 -0
- pboot_validator/_internal/_contextlib.py +84 -0
- pboot_validator/_internal/_core_metadata.py +97 -0
- pboot_validator/_internal/_core_utils.py +156 -0
- pboot_validator/_internal/_dataclasses.py +327 -0
- pboot_validator/_internal/_decorators.py +906 -0
- pboot_validator/_internal/_decorators_v1.py +174 -0
- pboot_validator/_internal/_discriminated_union.py +510 -0
- pboot_validator/_internal/_docs_extraction.py +128 -0
- pboot_validator/_internal/_fields.py +910 -0
- pboot_validator/_internal/_forward_ref.py +23 -0
- pboot_validator/_internal/_generate_schema.py +3053 -0
- pboot_validator/_internal/_generics.py +572 -0
- pboot_validator/_internal/_git.py +27 -0
- pboot_validator/_internal/_import_utils.py +20 -0
- pboot_validator/_internal/_known_annotated_metadata.py +412 -0
- pboot_validator/_internal/_mock_val_ser.py +241 -0
- pboot_validator/_internal/_model_construction.py +833 -0
- pboot_validator/_internal/_namespace_utils.py +300 -0
- pboot_validator/_internal/_repr.py +129 -0
- pboot_validator/_internal/_schema_gather.py +49 -0
- pboot_validator/_internal/_schema_generation_shared.py +126 -0
- pboot_validator/_internal/_serializers.py +59 -0
- pboot_validator/_internal/_signature.py +193 -0
- pboot_validator/_internal/_type_refs.py +128 -0
- pboot_validator/_internal/_typing_extra.py +506 -0
- pboot_validator/_internal/_utils.py +433 -0
- pboot_validator/_internal/_validate_call.py +141 -0
- pboot_validator/_internal/_validators.py +541 -0
- pboot_validator/_migration.py +317 -0
- pboot_validator/alias_generators.py +62 -0
- pboot_validator/aliases.py +136 -0
- pboot_validator/annotated_handlers.py +123 -0
- pboot_validator/class_validators.py +5 -0
- pboot_validator/color.py +605 -0
- pboot_validator/config.py +1295 -0
- pboot_validator/dataclasses.py +385 -0
- pboot_validator/datetime_parse.py +5 -0
- pboot_validator/decorator.py +5 -0
- pboot_validator/deprecated/__init__.py +0 -0
- pboot_validator/deprecated/class_validators.py +257 -0
- pboot_validator/deprecated/config.py +72 -0
- pboot_validator/deprecated/copy_internals.py +224 -0
- pboot_validator/deprecated/decorator.py +286 -0
- pboot_validator/deprecated/json.py +142 -0
- pboot_validator/deprecated/parse.py +81 -0
- pboot_validator/deprecated/tools.py +104 -0
- pboot_validator/env_settings.py +5 -0
- pboot_validator/error_wrappers.py +5 -0
- pboot_validator/errors.py +185 -0
- pboot_validator/experimental/__init__.py +1 -0
- pboot_validator/experimental/arguments_schema.py +44 -0
- pboot_validator/experimental/missing_sentinel.py +29 -0
- pboot_validator/experimental/pipeline.py +684 -0
- pboot_validator/fields.py +2065 -0
- pboot_validator/functional_serializers.py +461 -0
- pboot_validator/functional_validators.py +920 -0
- pboot_validator/generics.py +5 -0
- pboot_validator/json.py +5 -0
- pboot_validator/json_schema.py +3283 -0
- pboot_validator/main.py +1947 -0
- pboot_validator/mypy.py +1469 -0
- pboot_validator/networks.py +1417 -0
- pboot_validator/parse.py +5 -0
- pboot_validator/plugin/__init__.py +190 -0
- pboot_validator/plugin/_loader.py +56 -0
- pboot_validator/plugin/_schema_validator.py +152 -0
- pboot_validator/py.typed +0 -0
- pboot_validator/root_model.py +189 -0
- pboot_validator/schema.py +5 -0
- pboot_validator/tools.py +5 -0
- pboot_validator/type_adapter.py +780 -0
- pboot_validator/types.py +3405 -0
- pboot_validator/typing.py +5 -0
- pboot_validator/utils.py +5 -0
- pboot_validator/v1/__init__.py +131 -0
- pboot_validator/v1/_hypothesis_plugin.py +391 -0
- pboot_validator/v1/annotated_types.py +72 -0
- pboot_validator/v1/class_validators.py +361 -0
- pboot_validator/v1/color.py +494 -0
- pboot_validator/v1/config.py +191 -0
- pboot_validator/v1/dataclasses.py +503 -0
- pboot_validator/v1/datetime_parse.py +248 -0
- pboot_validator/v1/decorator.py +264 -0
- pboot_validator/v1/env_settings.py +350 -0
- pboot_validator/v1/error_wrappers.py +161 -0
- pboot_validator/v1/errors.py +646 -0
- pboot_validator/v1/fields.py +1253 -0
- pboot_validator/v1/generics.py +400 -0
- pboot_validator/v1/json.py +112 -0
- pboot_validator/v1/main.py +1130 -0
- pboot_validator/v1/mypy.py +950 -0
- pboot_validator/v1/networks.py +747 -0
- pboot_validator/v1/parse.py +66 -0
- pboot_validator/v1/py.typed +0 -0
- pboot_validator/v1/schema.py +1163 -0
- pboot_validator/v1/tools.py +92 -0
- pboot_validator/v1/types.py +1213 -0
- pboot_validator/v1/typing.py +627 -0
- pboot_validator/v1/utils.py +807 -0
- pboot_validator/v1/validators.py +768 -0
- pboot_validator/v1/version.py +38 -0
- pboot_validator/validate_call_decorator.py +121 -0
- pboot_validator/validators.py +5 -0
- pboot_validator/version.py +113 -0
- pboot_validator/warnings.py +130 -0
- pboot_validator-1.0.0.dist-info/METADATA +93 -0
- pboot_validator-1.0.0.dist-info/RECORD +112 -0
- pboot_validator-1.0.0.dist-info/WHEEL +4 -0
- pboot_validator-1.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import dataclasses
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from inspect import Parameter, Signature
|
|
6
|
+
from typing import TYPE_CHECKING, Any
|
|
7
|
+
|
|
8
|
+
from pydantic_core import PydanticUndefined
|
|
9
|
+
|
|
10
|
+
from ._typing_extra import signature_no_eval
|
|
11
|
+
from ._utils import is_valid_identifier
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from ..config import ExtraValues
|
|
15
|
+
from ..fields import FieldInfo
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Copied over from stdlib dataclasses
|
|
19
|
+
class _HAS_DEFAULT_FACTORY_CLASS:
|
|
20
|
+
def __repr__(self):
|
|
21
|
+
return '<factory>'
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _field_name_for_signature(field_name: str, field_info: FieldInfo, validate_by_alias: bool) -> str:
|
|
28
|
+
"""Extract the correct name to use for the field when generating a signature.
|
|
29
|
+
|
|
30
|
+
Assuming the field has a valid alias, this will return the alias. Otherwise, it will return the field name.
|
|
31
|
+
First priority is given to the alias, then the validation_alias, then the field name.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
field_name: The name of the field
|
|
35
|
+
field_info: The corresponding FieldInfo object.
|
|
36
|
+
validate_by_alias: The `validate_by_alias` value of the config.
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
The correct name to use when generating a signature.
|
|
40
|
+
"""
|
|
41
|
+
if validate_by_alias:
|
|
42
|
+
if isinstance(field_info.alias, str) and is_valid_identifier(field_info.alias):
|
|
43
|
+
return field_info.alias
|
|
44
|
+
if isinstance(field_info.validation_alias, str) and is_valid_identifier(field_info.validation_alias):
|
|
45
|
+
return field_info.validation_alias
|
|
46
|
+
|
|
47
|
+
return field_name
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _process_param_defaults(param: Parameter, validate_by_alias: bool) -> Parameter:
|
|
51
|
+
"""Modify the signature for a parameter in a dataclass where the default value is a FieldInfo instance.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
param (Parameter): The parameter
|
|
55
|
+
validate_by_alias: The `validate_by_alias` value of the config.
|
|
56
|
+
|
|
57
|
+
Returns:
|
|
58
|
+
Parameter: The custom processed parameter
|
|
59
|
+
"""
|
|
60
|
+
from ..fields import FieldInfo
|
|
61
|
+
|
|
62
|
+
param_default = param.default
|
|
63
|
+
if isinstance(param_default, FieldInfo):
|
|
64
|
+
annotation = param.annotation
|
|
65
|
+
# Replace the annotation if appropriate
|
|
66
|
+
# inspect does "clever" things to show annotations as strings because we have
|
|
67
|
+
# `from __future__ import annotations` in main, we don't want that
|
|
68
|
+
if annotation == 'Any':
|
|
69
|
+
annotation = Any
|
|
70
|
+
|
|
71
|
+
# Replace the field default
|
|
72
|
+
default = param_default.default
|
|
73
|
+
if default is PydanticUndefined:
|
|
74
|
+
if param_default.default_factory is None:
|
|
75
|
+
default = Signature.empty
|
|
76
|
+
else:
|
|
77
|
+
# this is used by dataclasses to indicate a factory exists:
|
|
78
|
+
default = dataclasses._HAS_DEFAULT_FACTORY # type: ignore
|
|
79
|
+
return param.replace(
|
|
80
|
+
annotation=annotation,
|
|
81
|
+
name=_field_name_for_signature(param.name, param_default, validate_by_alias),
|
|
82
|
+
default=default,
|
|
83
|
+
)
|
|
84
|
+
return param
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _generate_signature_parameters( # noqa: C901 (ignore complexity, could use a refactor)
|
|
88
|
+
init: Callable[..., None],
|
|
89
|
+
fields: dict[str, FieldInfo],
|
|
90
|
+
extra: ExtraValues | None,
|
|
91
|
+
validate_by_alias: bool,
|
|
92
|
+
) -> dict[str, Parameter]:
|
|
93
|
+
"""Generate a mapping of parameter names to Parameter objects for a pydantic BaseModel or dataclass."""
|
|
94
|
+
from itertools import islice
|
|
95
|
+
|
|
96
|
+
present_params = signature_no_eval(init).parameters.values()
|
|
97
|
+
merged_params: dict[str, Parameter] = {}
|
|
98
|
+
var_kw = None
|
|
99
|
+
use_var_kw = False
|
|
100
|
+
|
|
101
|
+
for param in islice(present_params, 1, None): # skip self arg
|
|
102
|
+
# inspect does "clever" things to show annotations as strings because we have
|
|
103
|
+
# `from __future__ import annotations` in main, we don't want that
|
|
104
|
+
if fields.get(param.name):
|
|
105
|
+
# exclude params with init=False
|
|
106
|
+
if getattr(fields[param.name], 'init', True) is False:
|
|
107
|
+
continue
|
|
108
|
+
param = param.replace(name=_field_name_for_signature(param.name, fields[param.name], validate_by_alias))
|
|
109
|
+
if param.annotation == 'Any':
|
|
110
|
+
param = param.replace(annotation=Any)
|
|
111
|
+
if param.kind is param.VAR_KEYWORD:
|
|
112
|
+
var_kw = param
|
|
113
|
+
continue
|
|
114
|
+
merged_params[param.name] = param
|
|
115
|
+
|
|
116
|
+
if var_kw: # if custom init has no var_kw, fields which are not declared in it cannot be passed through
|
|
117
|
+
for field_name, field in fields.items():
|
|
118
|
+
# when alias is a str it should be used for signature generation
|
|
119
|
+
param_name = _field_name_for_signature(field_name, field, validate_by_alias)
|
|
120
|
+
|
|
121
|
+
if field_name in merged_params or param_name in merged_params:
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
if not is_valid_identifier(param_name):
|
|
125
|
+
# Can happen with `create_model()`, where field names are not validated as identifiers:
|
|
126
|
+
use_var_kw = True
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
if field.is_required():
|
|
130
|
+
default = Parameter.empty
|
|
131
|
+
elif field.default_factory is not None:
|
|
132
|
+
# Mimics stdlib dataclasses:
|
|
133
|
+
default = _HAS_DEFAULT_FACTORY
|
|
134
|
+
else:
|
|
135
|
+
default = field.default
|
|
136
|
+
merged_params[param_name] = Parameter(
|
|
137
|
+
param_name,
|
|
138
|
+
Parameter.KEYWORD_ONLY,
|
|
139
|
+
annotation=field.rebuild_annotation(),
|
|
140
|
+
default=default,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if extra == 'allow':
|
|
144
|
+
use_var_kw = True
|
|
145
|
+
|
|
146
|
+
if var_kw and use_var_kw:
|
|
147
|
+
# Make sure the parameter for extra kwargs
|
|
148
|
+
# does not have the same name as a field
|
|
149
|
+
default_model_signature = [
|
|
150
|
+
('self', Parameter.POSITIONAL_ONLY),
|
|
151
|
+
('data', Parameter.VAR_KEYWORD),
|
|
152
|
+
]
|
|
153
|
+
if [(p.name, p.kind) for p in present_params] == default_model_signature:
|
|
154
|
+
# if this is the standard model signature, use extra_data as the extra args name
|
|
155
|
+
var_kw_name = 'extra_data'
|
|
156
|
+
else:
|
|
157
|
+
# else start from var_kw
|
|
158
|
+
var_kw_name = var_kw.name
|
|
159
|
+
|
|
160
|
+
# generate a name that's definitely unique (parameters can be named after an alias)
|
|
161
|
+
while var_kw_name in fields or var_kw_name in merged_params:
|
|
162
|
+
var_kw_name += '_'
|
|
163
|
+
merged_params[var_kw_name] = var_kw.replace(name=var_kw_name)
|
|
164
|
+
|
|
165
|
+
return merged_params
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def generate_pydantic_signature(
|
|
169
|
+
*,
|
|
170
|
+
init: Callable[..., None],
|
|
171
|
+
fields: dict[str, FieldInfo],
|
|
172
|
+
validate_by_alias: bool,
|
|
173
|
+
extra: ExtraValues | None,
|
|
174
|
+
is_dataclass: bool = False,
|
|
175
|
+
) -> Signature:
|
|
176
|
+
"""Generate signature for a pydantic BaseModel or dataclass.
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
init: The class init.
|
|
180
|
+
fields: The model fields.
|
|
181
|
+
validate_by_alias: The `validate_by_alias` value of the config.
|
|
182
|
+
extra: The `extra` value of the config.
|
|
183
|
+
is_dataclass: Whether the model is a dataclass.
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
The dataclass/BaseModel subclass signature.
|
|
187
|
+
"""
|
|
188
|
+
merged_params = _generate_signature_parameters(init, fields, extra, validate_by_alias)
|
|
189
|
+
|
|
190
|
+
if is_dataclass:
|
|
191
|
+
merged_params = {k: _process_param_defaults(v, validate_by_alias) for k, v in merged_params.items()}
|
|
192
|
+
|
|
193
|
+
return Signature(parameters=list(merged_params.values()), return_annotation=None)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Logic related to reference computation for types."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from typing import TYPE_CHECKING, Any, cast, get_args, get_origin
|
|
8
|
+
|
|
9
|
+
from typing_extensions import Sentinel, TypeAliasType
|
|
10
|
+
|
|
11
|
+
from . import _repr
|
|
12
|
+
from ._import_utils import import_cached_base_model
|
|
13
|
+
from ._utils import lenient_issubclass
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from pboot_validator import BaseModel
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _args_ref(args: tuple[Any, ...]) -> str:
|
|
20
|
+
arg_refs: list[str] = []
|
|
21
|
+
for arg in args:
|
|
22
|
+
if isinstance(arg, str):
|
|
23
|
+
# Handle string args as a special case; we may be able to remove this special handling if we
|
|
24
|
+
# wrap them in a ForwardRef at some point.
|
|
25
|
+
arg_ref = f'{arg}:str-{id(arg)}'
|
|
26
|
+
else:
|
|
27
|
+
arg_ref = f'{_repr.display_as_type(arg)}:{id(arg)}'
|
|
28
|
+
arg_refs.append(arg_ref)
|
|
29
|
+
|
|
30
|
+
if arg_refs:
|
|
31
|
+
return f'[{",".join(arg_refs)}]'
|
|
32
|
+
else:
|
|
33
|
+
return ''
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def model_type_ref(type_: type[BaseModel], args_override: tuple[Any, ...] | None = None) -> str:
|
|
37
|
+
"""Produce a reference for Pydantic base models."""
|
|
38
|
+
try:
|
|
39
|
+
generic_metadata = type_.__pydantic_generic_metadata__
|
|
40
|
+
except AttributeError:
|
|
41
|
+
# `type_` is `BaseModel`:
|
|
42
|
+
return f'{type_.__module__}.{type_.__qualname__}:{id(type_)}'
|
|
43
|
+
|
|
44
|
+
origin = generic_metadata['origin'] or type_
|
|
45
|
+
args = generic_metadata['args']
|
|
46
|
+
if not args and args_override is not None:
|
|
47
|
+
args = args_override
|
|
48
|
+
|
|
49
|
+
# While it is typed as `str`, `__module__` could in theory be `None`:
|
|
50
|
+
module_name = cast(str | None, origin.__module__)
|
|
51
|
+
if module_name is None:
|
|
52
|
+
module_name = '<No __module__>'
|
|
53
|
+
|
|
54
|
+
return f'{module_name}.{origin.__qualname__}:{id(origin)}{_args_ref(args)}'
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def class_type_ref(type_: Any, origin: type[Any] | None) -> str:
|
|
58
|
+
"""Produce a reference for an arbitrary class (dataclasses, typed dictionaries, named tuples)."""
|
|
59
|
+
tp: type[Any] = origin if origin is not None else type_
|
|
60
|
+
|
|
61
|
+
# While it is typed as `str`, `__module__` could in theory be `None`:
|
|
62
|
+
module_name = cast(str | None, tp.__module__)
|
|
63
|
+
if module_name is None:
|
|
64
|
+
module_name = '<No __module__>'
|
|
65
|
+
|
|
66
|
+
return f'{module_name}.{tp.__qualname__}:{id(tp)}{_args_ref(get_args(type_))}'
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
_NOT_PROVIDED = Sentinel('_NOT_PROVIDED')
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def any_class_type_ref(type_: Any, origin: type[Any] | None | _NOT_PROVIDED = _NOT_PROVIDED) -> str:
|
|
73
|
+
"""Produce a reference for any class kind, including Pydantic models."""
|
|
74
|
+
BaseModel_ = import_cached_base_model()
|
|
75
|
+
|
|
76
|
+
if lenient_issubclass(type_, BaseModel_):
|
|
77
|
+
return model_type_ref(type_) # pyright: ignore[reportArgumentType]
|
|
78
|
+
|
|
79
|
+
origin = get_origin(type_) if origin is _NOT_PROVIDED else origin
|
|
80
|
+
return class_type_ref(type_, origin) # pyright: ignore[reportArgumentType] (https://github.com/microsoft/pyright/issues/11115)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def any_type_ref(type_: Any, origin: Any | _NOT_PROVIDED = _NOT_PROVIDED) -> str:
|
|
84
|
+
"""Produce a reference for any object, including non-class objects."""
|
|
85
|
+
BaseModel_ = import_cached_base_model()
|
|
86
|
+
|
|
87
|
+
if lenient_issubclass(type_, BaseModel_):
|
|
88
|
+
return model_type_ref(type_) # pyright: ignore[reportArgumentType]
|
|
89
|
+
|
|
90
|
+
origin = get_origin(type_) if origin is _NOT_PROVIDED else origin
|
|
91
|
+
tp: Any = origin if origin is not None else type_
|
|
92
|
+
|
|
93
|
+
module_name = getattr(tp, '__module__', None)
|
|
94
|
+
if module_name is None:
|
|
95
|
+
module_name = '<No __module__>'
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
qualname = getattr(tp, '__qualname__', f'<No __qualname__: {tp}>')
|
|
99
|
+
except Exception:
|
|
100
|
+
qualname = getattr(tp, '__qualname__', '<No __qualname__>')
|
|
101
|
+
|
|
102
|
+
return f'{module_name}.{qualname}:{id(tp)}{_args_ref(get_args(type_))}'
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def enum_type_ref(type_: type[Enum]) -> str:
|
|
106
|
+
"""Produce a reference for an enum class."""
|
|
107
|
+
# While it is typed as `str`, `__module__` could in theory be `None`:
|
|
108
|
+
module_name = cast(str | None, type_.__module__)
|
|
109
|
+
if module_name is None:
|
|
110
|
+
module_name = '<No __module__>'
|
|
111
|
+
|
|
112
|
+
return f'{module_name}.{type_.__qualname__}:{id(type_)}'
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def type_alias_type_ref(type_: TypeAliasType) -> str:
|
|
116
|
+
"""Produce a reference for a type alias."""
|
|
117
|
+
origin: TypeAliasType = get_origin(type_) or type_
|
|
118
|
+
|
|
119
|
+
module_name = type_.__module__
|
|
120
|
+
if module_name is None:
|
|
121
|
+
module_name = '<No __module__>'
|
|
122
|
+
|
|
123
|
+
if sys.version_info >= (3, 15):
|
|
124
|
+
type_ref = f'{module_name}.{origin.__qualname__}:{id(origin)}'
|
|
125
|
+
else:
|
|
126
|
+
type_ref = f'{module_name}.{origin.__name__}:{id(origin)}'
|
|
127
|
+
|
|
128
|
+
return f'{type_ref}{_args_ref(get_args(type_))}'
|