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.
Files changed (112) hide show
  1. pboot_validator/__init__.py +461 -0
  2. pboot_validator/_internal/__init__.py +0 -0
  3. pboot_validator/_internal/_config.py +408 -0
  4. pboot_validator/_internal/_contextlib.py +84 -0
  5. pboot_validator/_internal/_core_metadata.py +97 -0
  6. pboot_validator/_internal/_core_utils.py +156 -0
  7. pboot_validator/_internal/_dataclasses.py +327 -0
  8. pboot_validator/_internal/_decorators.py +906 -0
  9. pboot_validator/_internal/_decorators_v1.py +174 -0
  10. pboot_validator/_internal/_discriminated_union.py +510 -0
  11. pboot_validator/_internal/_docs_extraction.py +128 -0
  12. pboot_validator/_internal/_fields.py +910 -0
  13. pboot_validator/_internal/_forward_ref.py +23 -0
  14. pboot_validator/_internal/_generate_schema.py +3053 -0
  15. pboot_validator/_internal/_generics.py +572 -0
  16. pboot_validator/_internal/_git.py +27 -0
  17. pboot_validator/_internal/_import_utils.py +20 -0
  18. pboot_validator/_internal/_known_annotated_metadata.py +412 -0
  19. pboot_validator/_internal/_mock_val_ser.py +241 -0
  20. pboot_validator/_internal/_model_construction.py +833 -0
  21. pboot_validator/_internal/_namespace_utils.py +300 -0
  22. pboot_validator/_internal/_repr.py +129 -0
  23. pboot_validator/_internal/_schema_gather.py +49 -0
  24. pboot_validator/_internal/_schema_generation_shared.py +126 -0
  25. pboot_validator/_internal/_serializers.py +59 -0
  26. pboot_validator/_internal/_signature.py +193 -0
  27. pboot_validator/_internal/_type_refs.py +128 -0
  28. pboot_validator/_internal/_typing_extra.py +506 -0
  29. pboot_validator/_internal/_utils.py +433 -0
  30. pboot_validator/_internal/_validate_call.py +141 -0
  31. pboot_validator/_internal/_validators.py +541 -0
  32. pboot_validator/_migration.py +317 -0
  33. pboot_validator/alias_generators.py +62 -0
  34. pboot_validator/aliases.py +136 -0
  35. pboot_validator/annotated_handlers.py +123 -0
  36. pboot_validator/class_validators.py +5 -0
  37. pboot_validator/color.py +605 -0
  38. pboot_validator/config.py +1295 -0
  39. pboot_validator/dataclasses.py +385 -0
  40. pboot_validator/datetime_parse.py +5 -0
  41. pboot_validator/decorator.py +5 -0
  42. pboot_validator/deprecated/__init__.py +0 -0
  43. pboot_validator/deprecated/class_validators.py +257 -0
  44. pboot_validator/deprecated/config.py +72 -0
  45. pboot_validator/deprecated/copy_internals.py +224 -0
  46. pboot_validator/deprecated/decorator.py +286 -0
  47. pboot_validator/deprecated/json.py +142 -0
  48. pboot_validator/deprecated/parse.py +81 -0
  49. pboot_validator/deprecated/tools.py +104 -0
  50. pboot_validator/env_settings.py +5 -0
  51. pboot_validator/error_wrappers.py +5 -0
  52. pboot_validator/errors.py +185 -0
  53. pboot_validator/experimental/__init__.py +1 -0
  54. pboot_validator/experimental/arguments_schema.py +44 -0
  55. pboot_validator/experimental/missing_sentinel.py +29 -0
  56. pboot_validator/experimental/pipeline.py +684 -0
  57. pboot_validator/fields.py +2065 -0
  58. pboot_validator/functional_serializers.py +461 -0
  59. pboot_validator/functional_validators.py +920 -0
  60. pboot_validator/generics.py +5 -0
  61. pboot_validator/json.py +5 -0
  62. pboot_validator/json_schema.py +3283 -0
  63. pboot_validator/main.py +1947 -0
  64. pboot_validator/mypy.py +1469 -0
  65. pboot_validator/networks.py +1417 -0
  66. pboot_validator/parse.py +5 -0
  67. pboot_validator/plugin/__init__.py +190 -0
  68. pboot_validator/plugin/_loader.py +56 -0
  69. pboot_validator/plugin/_schema_validator.py +152 -0
  70. pboot_validator/py.typed +0 -0
  71. pboot_validator/root_model.py +189 -0
  72. pboot_validator/schema.py +5 -0
  73. pboot_validator/tools.py +5 -0
  74. pboot_validator/type_adapter.py +780 -0
  75. pboot_validator/types.py +3405 -0
  76. pboot_validator/typing.py +5 -0
  77. pboot_validator/utils.py +5 -0
  78. pboot_validator/v1/__init__.py +131 -0
  79. pboot_validator/v1/_hypothesis_plugin.py +391 -0
  80. pboot_validator/v1/annotated_types.py +72 -0
  81. pboot_validator/v1/class_validators.py +361 -0
  82. pboot_validator/v1/color.py +494 -0
  83. pboot_validator/v1/config.py +191 -0
  84. pboot_validator/v1/dataclasses.py +503 -0
  85. pboot_validator/v1/datetime_parse.py +248 -0
  86. pboot_validator/v1/decorator.py +264 -0
  87. pboot_validator/v1/env_settings.py +350 -0
  88. pboot_validator/v1/error_wrappers.py +161 -0
  89. pboot_validator/v1/errors.py +646 -0
  90. pboot_validator/v1/fields.py +1253 -0
  91. pboot_validator/v1/generics.py +400 -0
  92. pboot_validator/v1/json.py +112 -0
  93. pboot_validator/v1/main.py +1130 -0
  94. pboot_validator/v1/mypy.py +950 -0
  95. pboot_validator/v1/networks.py +747 -0
  96. pboot_validator/v1/parse.py +66 -0
  97. pboot_validator/v1/py.typed +0 -0
  98. pboot_validator/v1/schema.py +1163 -0
  99. pboot_validator/v1/tools.py +92 -0
  100. pboot_validator/v1/types.py +1213 -0
  101. pboot_validator/v1/typing.py +627 -0
  102. pboot_validator/v1/utils.py +807 -0
  103. pboot_validator/v1/validators.py +768 -0
  104. pboot_validator/v1/version.py +38 -0
  105. pboot_validator/validate_call_decorator.py +121 -0
  106. pboot_validator/validators.py +5 -0
  107. pboot_validator/version.py +113 -0
  108. pboot_validator/warnings.py +130 -0
  109. pboot_validator-1.0.0.dist-info/METADATA +93 -0
  110. pboot_validator-1.0.0.dist-info/RECORD +112 -0
  111. pboot_validator-1.0.0.dist-info/WHEEL +4 -0
  112. pboot_validator-1.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,174 @@
1
+ """Logic for V1 validators, e.g. `@validator` and `@root_validator`."""
2
+
3
+ from __future__ import annotations as _annotations
4
+
5
+ from inspect import Parameter, signature
6
+ from typing import Any, TypeAlias, cast
7
+
8
+ from pydantic_core import core_schema
9
+ from typing_extensions import Protocol
10
+
11
+ from ..errors import PydanticUserError
12
+ from ._utils import can_be_positional
13
+
14
+
15
+ class V1OnlyValueValidator(Protocol):
16
+ """A simple validator, supported for V1 validators and V2 validators."""
17
+
18
+ def __call__(self, __value: Any) -> Any: ...
19
+
20
+
21
+ class V1ValidatorWithValues(Protocol):
22
+ """A validator with `values` argument, supported for V1 validators and V2 validators."""
23
+
24
+ def __call__(self, __value: Any, values: dict[str, Any]) -> Any: ...
25
+
26
+
27
+ class V1ValidatorWithValuesKwOnly(Protocol):
28
+ """A validator with keyword only `values` argument, supported for V1 validators and V2 validators."""
29
+
30
+ def __call__(self, __value: Any, *, values: dict[str, Any]) -> Any: ...
31
+
32
+
33
+ class V1ValidatorWithKwargs(Protocol):
34
+ """A validator with `kwargs` argument, supported for V1 validators and V2 validators."""
35
+
36
+ def __call__(self, __value: Any, **kwargs: Any) -> Any: ...
37
+
38
+
39
+ class V1ValidatorWithValuesAndKwargs(Protocol):
40
+ """A validator with `values` and `kwargs` arguments, supported for V1 validators and V2 validators."""
41
+
42
+ def __call__(self, __value: Any, values: dict[str, Any], **kwargs: Any) -> Any: ...
43
+
44
+
45
+ V1Validator: TypeAlias = (
46
+ V1ValidatorWithValues | V1ValidatorWithValuesKwOnly | V1ValidatorWithKwargs | V1ValidatorWithValuesAndKwargs
47
+ )
48
+
49
+
50
+ def can_be_keyword(param: Parameter) -> bool:
51
+ return param.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)
52
+
53
+
54
+ def make_generic_v1_field_validator(validator: V1Validator) -> core_schema.WithInfoValidatorFunction:
55
+ """Wrap a V1 style field validator for V2 compatibility.
56
+
57
+ Args:
58
+ validator: The V1 style field validator.
59
+
60
+ Returns:
61
+ A wrapped V2 style field validator.
62
+
63
+ Raises:
64
+ PydanticUserError: If the signature is not supported or the parameters are
65
+ not available in Pydantic V2.
66
+ """
67
+ sig = signature(validator)
68
+
69
+ needs_values_kw = False
70
+
71
+ for param_num, (param_name, parameter) in enumerate(sig.parameters.items()):
72
+ if can_be_keyword(parameter) and param_name in ('field', 'config'):
73
+ raise PydanticUserError(
74
+ 'The `field` and `config` parameters are not available in Pydantic V2, '
75
+ 'please use the `info` parameter instead.',
76
+ code='validator-field-config-info',
77
+ )
78
+ if parameter.kind is Parameter.VAR_KEYWORD:
79
+ needs_values_kw = True
80
+ elif can_be_keyword(parameter) and param_name == 'values':
81
+ needs_values_kw = True
82
+ elif can_be_positional(parameter) and param_num == 0:
83
+ # value
84
+ continue
85
+ elif parameter.default is Parameter.empty: # ignore params with defaults e.g. bound by functools.partial
86
+ raise PydanticUserError(
87
+ f'Unsupported signature for V1 style validator {validator}: {sig} is not supported.',
88
+ code='validator-v1-signature',
89
+ )
90
+
91
+ if needs_values_kw:
92
+ # (v, **kwargs), (v, values, **kwargs), (v, *, values, **kwargs) or (v, *, values)
93
+ val1 = cast(V1ValidatorWithValues, validator)
94
+
95
+ def wrapper1(value: Any, info: core_schema.ValidationInfo) -> Any:
96
+ return val1(value, values=info.data)
97
+
98
+ return wrapper1
99
+ else:
100
+ val2 = cast(V1OnlyValueValidator, validator)
101
+
102
+ def wrapper2(value: Any, _: core_schema.ValidationInfo) -> Any:
103
+ return val2(value)
104
+
105
+ return wrapper2
106
+
107
+
108
+ RootValidatorValues = dict[str, Any]
109
+ # technically tuple[model_dict, model_extra, fields_set] | tuple[dataclass_dict, init_vars]
110
+ RootValidatorFieldsTuple = tuple[Any, ...]
111
+
112
+
113
+ class V1RootValidatorFunction(Protocol):
114
+ """A simple root validator, supported for V1 validators and V2 validators."""
115
+
116
+ def __call__(self, __values: RootValidatorValues) -> RootValidatorValues: ...
117
+
118
+
119
+ class V2CoreBeforeRootValidator(Protocol):
120
+ """V2 validator with mode='before'."""
121
+
122
+ def __call__(self, __values: RootValidatorValues, __info: core_schema.ValidationInfo) -> RootValidatorValues: ...
123
+
124
+
125
+ class V2CoreAfterRootValidator(Protocol):
126
+ """V2 validator with mode='after'."""
127
+
128
+ def __call__(
129
+ self, __fields_tuple: RootValidatorFieldsTuple, __info: core_schema.ValidationInfo
130
+ ) -> RootValidatorFieldsTuple: ...
131
+
132
+
133
+ def make_v1_generic_root_validator(
134
+ validator: V1RootValidatorFunction, pre: bool
135
+ ) -> V2CoreBeforeRootValidator | V2CoreAfterRootValidator:
136
+ """Wrap a V1 style root validator for V2 compatibility.
137
+
138
+ Args:
139
+ validator: The V1 style field validator.
140
+ pre: Whether the validator is a pre validator.
141
+
142
+ Returns:
143
+ A wrapped V2 style validator.
144
+ """
145
+ if pre is True:
146
+ # mode='before' for pydantic-core
147
+ def _wrapper1(values: RootValidatorValues, _: core_schema.ValidationInfo) -> RootValidatorValues:
148
+ return validator(values)
149
+
150
+ return _wrapper1
151
+
152
+ # mode='after' for pydantic-core
153
+ def _wrapper2(fields_tuple: RootValidatorFieldsTuple, _: core_schema.ValidationInfo) -> RootValidatorFieldsTuple:
154
+ if len(fields_tuple) == 2:
155
+ # dataclass, this is easy
156
+ values, init_vars = fields_tuple
157
+ values = validator(values)
158
+ return values, init_vars
159
+ else:
160
+ # ugly hack: to match v1 behaviour, we merge values and model_extra, then split them up based on fields
161
+ # afterwards
162
+ model_dict, model_extra, fields_set = fields_tuple
163
+ if model_extra:
164
+ fields = set(model_dict.keys())
165
+ model_dict.update(model_extra)
166
+ model_dict_new = validator(model_dict)
167
+ for k in list(model_dict_new.keys()):
168
+ if k not in fields:
169
+ model_extra[k] = model_dict_new.pop(k)
170
+ else:
171
+ model_dict_new = validator(model_dict)
172
+ return model_dict_new, model_extra, fields_set
173
+
174
+ return _wrapper2