Coverage for pydantic/experimental/pipeline.py: 92.34%
355 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-13 19:35 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-13 19:35 +0000
1"""Experimental pipeline API functionality. Be careful with this API, it's subject to change."""
3from __future__ import annotations 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
5import datetime 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
6import operator 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
7import re 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
8import sys 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
9from collections import deque 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
10from collections.abc import Container 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
11from dataclasses import dataclass 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
12from decimal import Decimal 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
13from functools import cached_property, partial 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
14from re import Pattern 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
15from typing import TYPE_CHECKING, Annotated, Any, Callable, Generic, Protocol, TypeVar, Union, overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
17import annotated_types 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
19if TYPE_CHECKING: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
20 from pydantic_core import core_schema as cs
22 from pydantic import GetCoreSchemaHandler
24from pydantic._internal._internal_dataclass import slots_true as _slots_true 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
26if sys.version_info < (3, 10): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
27 EllipsisType = type(Ellipsis) 1ouevwpx
28else:
29 from types import EllipsisType 1qyrzagbhfABCDijklsEtFcmdn
31__all__ = ['validate_as', 'validate_as_deferred', 'transform'] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
33_slots_frozen = {**_slots_true, 'frozen': True} 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
36@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
37class _ValidateAs: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
38 tp: type[Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
39 strict: bool = False 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
42@dataclass 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
43class _ValidateAsDefer: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
44 func: Callable[[], type[Any]] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
46 @cached_property 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
47 def tp(self) -> type[Any]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
48 return self.func()
51@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
52class _Transform: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
53 func: Callable[[Any], Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
56@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
57class _PipelineOr: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
58 left: _Pipeline[Any, Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
59 right: _Pipeline[Any, Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
62@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
63class _PipelineAnd: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
64 left: _Pipeline[Any, Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
65 right: _Pipeline[Any, Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
68@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
69class _Eq: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
70 value: Any 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
73@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
74class _NotEq: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
75 value: Any 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
78@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
79class _In: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
80 values: Container[Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
83@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
84class _NotIn: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
85 values: Container[Any] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
88_ConstraintAnnotation = Union[ 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
89 annotated_types.Le,
90 annotated_types.Ge,
91 annotated_types.Lt,
92 annotated_types.Gt,
93 annotated_types.Len,
94 annotated_types.MultipleOf,
95 annotated_types.Timezone,
96 annotated_types.Interval,
97 annotated_types.Predicate,
98 # common predicates not included in annotated_types
99 _Eq,
100 _NotEq,
101 _In,
102 _NotIn,
103 # regular expressions
104 Pattern[str],
105]
108@dataclass(**_slots_frozen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
109class _Constraint: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
110 constraint: _ConstraintAnnotation 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
113_Step = Union[_ValidateAs, _ValidateAsDefer, _Transform, _PipelineOr, _PipelineAnd, _Constraint] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
115_InT = TypeVar('_InT') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
116_OutT = TypeVar('_OutT') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
117_NewOutT = TypeVar('_NewOutT') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
120class _FieldTypeMarker: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
121 pass 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
124# TODO: ultimately, make this public, see https://github.com/pydantic/pydantic/pull/9459#discussion_r1628197626
125# Also, make this frozen eventually, but that doesn't work right now because of the generic base
126# Which attempts to modify __orig_base__ and such.
127# We could go with a manual freeze, but that seems overkill for now.
128@dataclass(**_slots_true) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
129class _Pipeline(Generic[_InT, _OutT]): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
130 """Abstract representation of a chain of validation, transformation, and parsing steps."""
132 _steps: tuple[_Step, ...] 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
134 def transform( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
135 self,
136 func: Callable[[_OutT], _NewOutT],
137 ) -> _Pipeline[_InT, _NewOutT]:
138 """Transform the output of the previous step.
140 If used as the first step in a pipeline, the type of the field is used.
141 That is, the transformation is applied to after the value is parsed to the field's type.
142 """
143 return _Pipeline[_InT, _NewOutT](self._steps + (_Transform(func),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
145 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
146 def validate_as(self, tp: type[_NewOutT], *, strict: bool = ...) -> _Pipeline[_InT, _NewOutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
148 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
149 def validate_as(self, tp: EllipsisType, *, strict: bool = ...) -> _Pipeline[_InT, Any]: # type: ignore 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
150 ...
152 def validate_as(self, tp: type[_NewOutT] | EllipsisType, *, strict: bool = False) -> _Pipeline[_InT, Any]: # type: ignore 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
153 """Validate / parse the input into a new type.
155 If no type is provided, the type of the field is used.
157 Types are parsed in Pydantic's `lax` mode by default,
158 but you can enable `strict` mode by passing `strict=True`.
159 """
160 if isinstance(tp, EllipsisType): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
161 return _Pipeline[_InT, Any](self._steps + (_ValidateAs(_FieldTypeMarker, strict=strict),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
162 return _Pipeline[_InT, _NewOutT](self._steps + (_ValidateAs(tp, strict=strict),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
164 def validate_as_deferred(self, func: Callable[[], type[_NewOutT]]) -> _Pipeline[_InT, _NewOutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
165 """Parse the input into a new type, deferring resolution of the type until the current class
166 is fully defined.
168 This is useful when you need to reference the class in it's own type annotations.
169 """
170 return _Pipeline[_InT, _NewOutT](self._steps + (_ValidateAsDefer(func),))
172 # constraints
173 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
174 def constrain(self: _Pipeline[_InT, _NewOutGe], constraint: annotated_types.Ge) -> _Pipeline[_InT, _NewOutGe]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
176 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
177 def constrain(self: _Pipeline[_InT, _NewOutGt], constraint: annotated_types.Gt) -> _Pipeline[_InT, _NewOutGt]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
179 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
180 def constrain(self: _Pipeline[_InT, _NewOutLe], constraint: annotated_types.Le) -> _Pipeline[_InT, _NewOutLe]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
182 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
183 def constrain(self: _Pipeline[_InT, _NewOutLt], constraint: annotated_types.Lt) -> _Pipeline[_InT, _NewOutLt]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
185 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
186 def constrain( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
187 self: _Pipeline[_InT, _NewOutLen], constraint: annotated_types.Len 1agbhefijklcmdn
188 ) -> _Pipeline[_InT, _NewOutLen]: ... 1agbhefijklcmdn
190 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
191 def constrain( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
192 self: _Pipeline[_InT, _NewOutT], constraint: annotated_types.MultipleOf 1agbhefijklcmdn
193 ) -> _Pipeline[_InT, _NewOutT]: ... 1agbhefijklcmdn
195 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
196 def constrain( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
197 self: _Pipeline[_InT, _NewOutDatetime], constraint: annotated_types.Timezone 1agbhefijklcmdn
198 ) -> _Pipeline[_InT, _NewOutDatetime]: ... 1agbhefijklcmdn
200 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
201 def constrain(self: _Pipeline[_InT, _OutT], constraint: annotated_types.Predicate) -> _Pipeline[_InT, _OutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
203 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
204 def constrain( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
205 self: _Pipeline[_InT, _NewOutInterval], constraint: annotated_types.Interval 1agbhefijklcmdn
206 ) -> _Pipeline[_InT, _NewOutInterval]: ... 1agbhefijklcmdn
208 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
209 def constrain(self: _Pipeline[_InT, _OutT], constraint: _Eq) -> _Pipeline[_InT, _OutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
211 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
212 def constrain(self: _Pipeline[_InT, _OutT], constraint: _NotEq) -> _Pipeline[_InT, _OutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
214 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
215 def constrain(self: _Pipeline[_InT, _OutT], constraint: _In) -> _Pipeline[_InT, _OutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
217 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
218 def constrain(self: _Pipeline[_InT, _OutT], constraint: _NotIn) -> _Pipeline[_InT, _OutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
220 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
221 def constrain(self: _Pipeline[_InT, _NewOutT], constraint: Pattern[str]) -> _Pipeline[_InT, _NewOutT]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
223 def constrain(self, constraint: _ConstraintAnnotation) -> Any: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
224 """Constrain a value to meet a certain condition.
226 We support most conditions from `annotated_types`, as well as regular expressions.
228 Most of the time you'll be calling a shortcut method like `gt`, `lt`, `len`, etc
229 so you don't need to call this directly.
230 """
231 return _Pipeline[_InT, _OutT](self._steps + (_Constraint(constraint),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
233 def predicate(self: _Pipeline[_InT, _NewOutT], func: Callable[[_NewOutT], bool]) -> _Pipeline[_InT, _NewOutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
234 """Constrain a value to meet a certain predicate."""
235 return self.constrain(annotated_types.Predicate(func)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
237 def gt(self: _Pipeline[_InT, _NewOutGt], gt: _NewOutGt) -> _Pipeline[_InT, _NewOutGt]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
238 """Constrain a value to be greater than a certain value."""
239 return self.constrain(annotated_types.Gt(gt)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
241 def lt(self: _Pipeline[_InT, _NewOutLt], lt: _NewOutLt) -> _Pipeline[_InT, _NewOutLt]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
242 """Constrain a value to be less than a certain value."""
243 return self.constrain(annotated_types.Lt(lt)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
245 def ge(self: _Pipeline[_InT, _NewOutGe], ge: _NewOutGe) -> _Pipeline[_InT, _NewOutGe]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
246 """Constrain a value to be greater than or equal to a certain value."""
247 return self.constrain(annotated_types.Ge(ge)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
249 def le(self: _Pipeline[_InT, _NewOutLe], le: _NewOutLe) -> _Pipeline[_InT, _NewOutLe]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
250 """Constrain a value to be less than or equal to a certain value."""
251 return self.constrain(annotated_types.Le(le)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
253 def len(self: _Pipeline[_InT, _NewOutLen], min_len: int, max_len: int | None = None) -> _Pipeline[_InT, _NewOutLen]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
254 """Constrain a value to have a certain length."""
255 return self.constrain(annotated_types.Len(min_len, max_len)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
257 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
258 def multiple_of(self: _Pipeline[_InT, _NewOutDiv], multiple_of: _NewOutDiv) -> _Pipeline[_InT, _NewOutDiv]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
260 @overload 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
261 def multiple_of(self: _Pipeline[_InT, _NewOutMod], multiple_of: _NewOutMod) -> _Pipeline[_InT, _NewOutMod]: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
263 def multiple_of(self: _Pipeline[_InT, Any], multiple_of: Any) -> _Pipeline[_InT, Any]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
264 """Constrain a value to be a multiple of a certain number."""
265 return self.constrain(annotated_types.MultipleOf(multiple_of)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
267 def eq(self: _Pipeline[_InT, _OutT], value: _OutT) -> _Pipeline[_InT, _OutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
268 """Constrain a value to be equal to a certain value."""
269 return self.constrain(_Eq(value)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
271 def not_eq(self: _Pipeline[_InT, _OutT], value: _OutT) -> _Pipeline[_InT, _OutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
272 """Constrain a value to not be equal to a certain value."""
273 return self.constrain(_NotEq(value)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
275 def in_(self: _Pipeline[_InT, _OutT], values: Container[_OutT]) -> _Pipeline[_InT, _OutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
276 """Constrain a value to be in a certain set."""
277 return self.constrain(_In(values)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
279 def not_in(self: _Pipeline[_InT, _OutT], values: Container[_OutT]) -> _Pipeline[_InT, _OutT]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
280 """Constrain a value to not be in a certain set."""
281 return self.constrain(_NotIn(values)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
283 # timezone methods
284 def datetime_tz_naive(self: _Pipeline[_InT, datetime.datetime]) -> _Pipeline[_InT, datetime.datetime]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
285 return self.constrain(annotated_types.Timezone(None)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
287 def datetime_tz_aware(self: _Pipeline[_InT, datetime.datetime]) -> _Pipeline[_InT, datetime.datetime]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
288 return self.constrain(annotated_types.Timezone(...)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
290 def datetime_tz( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
291 self: _Pipeline[_InT, datetime.datetime], tz: datetime.tzinfo
292 ) -> _Pipeline[_InT, datetime.datetime]:
293 return self.constrain(annotated_types.Timezone(tz)) # type: ignore
295 def datetime_with_tz( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
296 self: _Pipeline[_InT, datetime.datetime], tz: datetime.tzinfo | None
297 ) -> _Pipeline[_InT, datetime.datetime]:
298 return self.transform(partial(datetime.datetime.replace, tzinfo=tz))
300 # string methods
301 def str_lower(self: _Pipeline[_InT, str]) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
302 return self.transform(str.lower) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
304 def str_upper(self: _Pipeline[_InT, str]) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
305 return self.transform(str.upper) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
307 def str_title(self: _Pipeline[_InT, str]) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
308 return self.transform(str.title) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
310 def str_strip(self: _Pipeline[_InT, str]) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
311 return self.transform(str.strip) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
313 def str_pattern(self: _Pipeline[_InT, str], pattern: str) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
314 return self.constrain(re.compile(pattern)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
316 def str_contains(self: _Pipeline[_InT, str], substring: str) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
317 return self.predicate(lambda v: substring in v) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
319 def str_starts_with(self: _Pipeline[_InT, str], prefix: str) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
320 return self.predicate(lambda v: v.startswith(prefix)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
322 def str_ends_with(self: _Pipeline[_InT, str], suffix: str) -> _Pipeline[_InT, str]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
323 return self.predicate(lambda v: v.endswith(suffix)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
325 # operators
326 def otherwise(self, other: _Pipeline[_OtherIn, _OtherOut]) -> _Pipeline[_InT | _OtherIn, _OutT | _OtherOut]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
327 """Combine two validation chains, returning the result of the first chain if it succeeds, and the second chain if it fails."""
328 return _Pipeline((_PipelineOr(self, other),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
330 __or__ = otherwise 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
332 def then(self, other: _Pipeline[_OutT, _OtherOut]) -> _Pipeline[_InT, _OtherOut]: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
333 """Pipe the result of one validation chain into another."""
334 return _Pipeline((_PipelineAnd(self, other),)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
336 __and__ = then 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
338 def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> cs.CoreSchema: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
339 from pydantic_core import core_schema as cs 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
341 queue = deque(self._steps) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
343 s = None 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
345 while queue: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
346 step = queue.popleft() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
347 s = _apply_step(step, s, handler, source_type) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
349 s = s or cs.any_schema() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
350 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
352 def __supports_type__(self, _: _OutT) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
353 raise NotImplementedError
356validate_as = _Pipeline[Any, Any](()).validate_as 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
357validate_as_deferred = _Pipeline[Any, Any](()).validate_as_deferred 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
358transform = _Pipeline[Any, Any]((_ValidateAs(_FieldTypeMarker),)).transform 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
361def _check_func( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
362 func: Callable[[Any], bool], predicate_err: str | Callable[[], str], s: cs.CoreSchema | None
363) -> cs.CoreSchema:
364 from pydantic_core import core_schema as cs 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
366 def handler(v: Any) -> Any: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
367 if func(v): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
368 return v 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
369 raise ValueError(f'Expected {predicate_err if isinstance(predicate_err, str) else predicate_err()}') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
371 if s is None: 371 ↛ 372line 371 didn't jump to line 372 because the condition on line 371 was never true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
372 return cs.no_info_plain_validator_function(handler)
373 else:
374 return cs.no_info_after_validator_function(handler, s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
377def _apply_step(step: _Step, s: cs.CoreSchema | None, handler: GetCoreSchemaHandler, source_type: Any) -> cs.CoreSchema: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
378 from pydantic_core import core_schema as cs 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
380 if isinstance(step, _ValidateAs): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
381 s = _apply_parse(s, step.tp, step.strict, handler, source_type) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
382 elif isinstance(step, _ValidateAsDefer): 382 ↛ 383line 382 didn't jump to line 383 because the condition on line 382 was never true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
383 s = _apply_parse(s, step.tp, False, handler, source_type)
384 elif isinstance(step, _Transform): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
385 s = _apply_transform(s, step.func, handler) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
386 elif isinstance(step, _Constraint): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
387 s = _apply_constraint(s, step.constraint) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
388 elif isinstance(step, _PipelineOr): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
389 s = cs.union_schema([handler(step.left), handler(step.right)]) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
390 else:
391 assert isinstance(step, _PipelineAnd) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
392 s = cs.chain_schema([handler(step.left), handler(step.right)]) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
393 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
396def _apply_parse( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
397 s: cs.CoreSchema | None,
398 tp: type[Any],
399 strict: bool,
400 handler: GetCoreSchemaHandler,
401 source_type: Any,
402) -> cs.CoreSchema:
403 from pydantic_core import core_schema as cs 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
405 from pydantic import Strict 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
407 if tp is _FieldTypeMarker: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
408 return handler(source_type) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
410 if strict: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
411 tp = Annotated[tp, Strict()] # type: ignore 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
413 if s and s['type'] == 'any': 413 ↛ 414line 413 didn't jump to line 414 because the condition on line 413 was never true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
414 return handler(tp)
415 else:
416 return cs.chain_schema([s, handler(tp)]) if s else handler(tp) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
419def _apply_transform( 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
420 s: cs.CoreSchema | None, func: Callable[[Any], Any], handler: GetCoreSchemaHandler
421) -> cs.CoreSchema:
422 from pydantic_core import core_schema as cs 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
424 if s is None: 424 ↛ 425line 424 didn't jump to line 425 because the condition on line 424 was never true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
425 return cs.no_info_plain_validator_function(func)
427 if s['type'] == 'str': 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
428 if func is str.strip: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
429 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
430 s['strip_whitespace'] = True 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
431 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
432 elif func is str.lower: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
433 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
434 s['to_lower'] = True 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
435 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
436 elif func is str.upper: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
437 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
438 s['to_upper'] = True 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
439 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
441 return cs.no_info_after_validator_function(func, s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
444def _apply_constraint( # noqa: C901 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
445 s: cs.CoreSchema | None, constraint: _ConstraintAnnotation
446) -> cs.CoreSchema:
447 """Apply a single constraint to a schema."""
448 if isinstance(constraint, annotated_types.Gt): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
449 gt = constraint.gt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
450 if s and s['type'] in {'int', 'float', 'decimal'}: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
451 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
452 if s['type'] == 'int' and isinstance(gt, int): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
453 s['gt'] = gt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
454 elif s['type'] == 'float' and isinstance(gt, float): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
455 s['gt'] = gt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
456 elif s['type'] == 'decimal' and isinstance(gt, Decimal): 456 ↛ 646line 456 didn't jump to line 646 because the condition on line 456 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
457 s['gt'] = gt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
458 else:
460 def check_gt(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
461 return v > gt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
463 s = _check_func(check_gt, f'> {gt}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
464 elif isinstance(constraint, annotated_types.Ge): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
465 ge = constraint.ge 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
466 if s and s['type'] in {'int', 'float', 'decimal'}: 466 ↛ 475line 466 didn't jump to line 475 because the condition on line 466 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
467 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
468 if s['type'] == 'int' and isinstance(ge, int): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
469 s['ge'] = ge 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
470 elif s['type'] == 'float' and isinstance(ge, float): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
471 s['ge'] = ge 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
472 elif s['type'] == 'decimal' and isinstance(ge, Decimal): 472 ↛ 475line 472 didn't jump to line 475 because the condition on line 472 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
473 s['ge'] = ge 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
475 def check_ge(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
476 return v >= ge 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
478 s = _check_func(check_ge, f'>= {ge}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
479 elif isinstance(constraint, annotated_types.Lt): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
480 lt = constraint.lt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
481 if s and s['type'] in {'int', 'float', 'decimal'}: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
482 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
483 if s['type'] == 'int' and isinstance(lt, int): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
484 s['lt'] = lt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
485 elif s['type'] == 'float' and isinstance(lt, float): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
486 s['lt'] = lt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
487 elif s['type'] == 'decimal' and isinstance(lt, Decimal): 487 ↛ 490line 487 didn't jump to line 490 because the condition on line 487 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
488 s['lt'] = lt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
490 def check_lt(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
491 return v < lt 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
493 s = _check_func(check_lt, f'< {lt}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
494 elif isinstance(constraint, annotated_types.Le): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
495 le = constraint.le 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
496 if s and s['type'] in {'int', 'float', 'decimal'}: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
497 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
498 if s['type'] == 'int' and isinstance(le, int): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
499 s['le'] = le 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
500 elif s['type'] == 'float' and isinstance(le, float): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
501 s['le'] = le 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
502 elif s['type'] == 'decimal' and isinstance(le, Decimal): 502 ↛ 505line 502 didn't jump to line 505 because the condition on line 502 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
503 s['le'] = le 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
505 def check_le(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
506 return v <= le 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
508 s = _check_func(check_le, f'<= {le}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
509 elif isinstance(constraint, annotated_types.Len): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
510 min_len = constraint.min_length 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
511 max_len = constraint.max_length 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
513 if s and s['type'] in {'str', 'list', 'tuple', 'set', 'frozenset', 'dict'}: 513 ↛ 528line 513 didn't jump to line 528 because the condition on line 513 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
514 assert ( 1ouefvwpx
515 s['type'] == 'str'
516 or s['type'] == 'list'
517 or s['type'] == 'tuple'
518 or s['type'] == 'set'
519 or s['type'] == 'dict'
520 or s['type'] == 'frozenset'
521 )
522 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
523 if min_len != 0: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
524 s['min_length'] = min_len 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
525 if max_len is not None: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
526 s['max_length'] = max_len 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
528 def check_len(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
529 if max_len is not None: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
530 return (min_len <= len(v)) and (len(v) <= max_len) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
531 return min_len <= len(v) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
533 s = _check_func(check_len, f'length >= {min_len} and length <= {max_len}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
534 elif isinstance(constraint, annotated_types.MultipleOf): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
535 multiple_of = constraint.multiple_of 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
536 if s and s['type'] in {'int', 'float', 'decimal'}: 536 ↛ 545line 536 didn't jump to line 545 because the condition on line 536 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
537 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
538 if s['type'] == 'int' and isinstance(multiple_of, int): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
539 s['multiple_of'] = multiple_of 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
540 elif s['type'] == 'float' and isinstance(multiple_of, float): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
541 s['multiple_of'] = multiple_of 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
542 elif s['type'] == 'decimal' and isinstance(multiple_of, Decimal): 542 ↛ 545line 542 didn't jump to line 545 because the condition on line 542 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
543 s['multiple_of'] = multiple_of 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
545 def check_multiple_of(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
546 return v % multiple_of == 0 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
548 s = _check_func(check_multiple_of, f'% {multiple_of} == 0', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
549 elif isinstance(constraint, annotated_types.Timezone): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
550 tz = constraint.tz 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
552 if tz is ...: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
553 if s and s['type'] == 'datetime': 553 ↛ 558line 553 didn't jump to line 558 because the condition on line 553 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
554 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
555 s['tz_constraint'] = 'aware' 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
556 else:
558 def check_tz_aware(v: object) -> bool:
559 assert isinstance(v, datetime.datetime)
560 return v.tzinfo is not None
562 s = _check_func(check_tz_aware, 'timezone aware', s)
563 elif tz is None: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
564 if s and s['type'] == 'datetime': 564 ↛ 569line 564 didn't jump to line 569 because the condition on line 564 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
565 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
566 s['tz_constraint'] = 'naive' 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
567 else:
569 def check_tz_naive(v: object) -> bool:
570 assert isinstance(v, datetime.datetime)
571 return v.tzinfo is None
573 s = _check_func(check_tz_naive, 'timezone naive', s)
574 else:
575 raise NotImplementedError('Constraining to a specific timezone is not yet supported')
576 elif isinstance(constraint, annotated_types.Interval): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
577 if constraint.ge: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
578 s = _apply_constraint(s, annotated_types.Ge(constraint.ge)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
579 if constraint.gt: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
580 s = _apply_constraint(s, annotated_types.Gt(constraint.gt)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
581 if constraint.le: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
582 s = _apply_constraint(s, annotated_types.Le(constraint.le)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
583 if constraint.lt: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
584 s = _apply_constraint(s, annotated_types.Lt(constraint.lt)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
585 assert s is not None 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
586 elif isinstance(constraint, annotated_types.Predicate): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
587 func = constraint.func 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
589 if func.__name__ == '<lambda>': 589 ↛ 605line 589 didn't jump to line 605 because the condition on line 589 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
590 # attempt to extract the source code for a lambda function
591 # to use as the function name in error messages
592 # TODO: is there a better way? should we just not do this?
593 import inspect 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
595 try: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
596 source = inspect.getsource(func).strip() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
597 source = source.removesuffix(')') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
598 lambda_source_code = '`' + ''.join(''.join(source.split('lambda ')[1:]).split(':')[1:]).strip() + '`' 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
599 except OSError: 1oqrabpstcd
600 # stringified annotations
601 lambda_source_code = 'lambda' 1oqrabpstcd
603 s = _check_func(func, lambda_source_code, s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
604 else:
605 s = _check_func(func, func.__name__, s)
606 elif isinstance(constraint, _NotEq): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
607 value = constraint.value 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
609 def check_not_eq(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
610 return operator.__ne__(v, value) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
612 s = _check_func(check_not_eq, f'!= {value}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
613 elif isinstance(constraint, _Eq): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
614 value = constraint.value 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
616 def check_eq(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
617 return operator.__eq__(v, value) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
619 s = _check_func(check_eq, f'== {value}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
620 elif isinstance(constraint, _In): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
621 values = constraint.values 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
623 def check_in(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
624 return operator.__contains__(values, v) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
626 s = _check_func(check_in, f'in {values}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
627 elif isinstance(constraint, _NotIn): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
628 values = constraint.values 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
630 def check_not_in(v: Any) -> bool: 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
631 return operator.__not__(operator.__contains__(values, v)) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
633 s = _check_func(check_not_in, f'not in {values}', s) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
634 else:
635 assert isinstance(constraint, Pattern) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
636 if s and s['type'] == 'str': 636 ↛ 641line 636 didn't jump to line 641 because the condition on line 636 was always true1ouqyrzagbhefvwABCDijklpxsEtFcmdn
637 s = s.copy() 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
638 s['pattern'] = constraint.pattern 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
639 else:
641 def check_pattern(v: object) -> bool:
642 assert isinstance(v, str)
643 return constraint.match(v) is not None
645 s = _check_func(check_pattern, f'~ {constraint.pattern}', s)
646 return s 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
649class _SupportsRange(annotated_types.SupportsLe, annotated_types.SupportsGe, Protocol): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
650 pass 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
653class _SupportsLen(Protocol): 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
654 def __len__(self) -> int: ... 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
657_NewOutGt = TypeVar('_NewOutGt', bound=annotated_types.SupportsGt) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
658_NewOutGe = TypeVar('_NewOutGe', bound=annotated_types.SupportsGe) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
659_NewOutLt = TypeVar('_NewOutLt', bound=annotated_types.SupportsLt) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
660_NewOutLe = TypeVar('_NewOutLe', bound=annotated_types.SupportsLe) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
661_NewOutLen = TypeVar('_NewOutLen', bound=_SupportsLen) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
662_NewOutDiv = TypeVar('_NewOutDiv', bound=annotated_types.SupportsDiv) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
663_NewOutMod = TypeVar('_NewOutMod', bound=annotated_types.SupportsMod) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
664_NewOutDatetime = TypeVar('_NewOutDatetime', bound=datetime.datetime) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
665_NewOutInterval = TypeVar('_NewOutInterval', bound=_SupportsRange) 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
666_OtherIn = TypeVar('_OtherIn') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn
667_OtherOut = TypeVar('_OtherOut') 1ouqyrzagbhefvwABCDijklpxsEtFcmdn