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,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_))}'