zmc
2023-12-22 9fdbf60165db0400c2e8e6be2dc6e88138ac719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
U
L±dÞPã@s´dZddlZddlZddlmZddlmZddlmZmZddl    m
Z
m Z m Z m Z ddlmZdd    lmZd
d d d dddddddddddddddgZdd„Zdd„Zedd „ƒZe d d!d!d"Gd#d$„d$ƒƒZd%d„Ze d d!d!d&Gd'd(„d(ƒƒZdPd)d„Ze d d!d!d"Gd*d+„d+ƒƒZd,d„Ze d d!d!d"Gd-d.„d.ƒƒZd/d„Ze d d!d!d"Gd0d1„d1ƒƒZd2d„Ze d d d!d"Gd3d4„d4ƒƒZ d5d„Z!e d d!d!d"Gd6d7„d7ƒƒZ"dQd8d „Z#e d d!d!d"Gd9d:„d:ƒƒZ$dRd;d „Z%e d d!d!d&Gd<d=„d=ƒƒZ&d>d„Z'd?d„Z(d@d„Z)dAd„Z*e d d!d!d&GdBdC„dCƒƒZ+dDd„Z,e d d!d!d&GdEdF„dFƒƒZ-dGd„Z.e d d!d!d"GdHdI„dIƒƒZ/dJdK„Z0e d d!d!d"GdLdM„dMƒƒZ1de2e3fdNœdOd„Z4dS)Sz
Commonly useful validators.
éN)Úcontextmanager)ÚPatterné)Úget_run_validatorsÚset_run_validators)Ú _AndValidatorÚand_ÚattribÚattrs)Údefault_if_none)ÚNotCallableErrorrÚ deep_iterableÚ deep_mappingÚdisabledÚgeÚ get_disabledÚgtÚin_Ú instance_ofÚ is_callableÚleÚltÚ
matches_reÚmax_lenÚmin_lenÚnot_ÚoptionalÚprovidesÚ set_disabledcCst| ƒdS)a
    Globally disable or enable running validators.
 
    By default, they are run.
 
    :param disabled: If ``True``, disable running all validators.
    :type disabled: bool
 
    .. warning::
 
        This function is not thread-safe!
 
    .. versionadded:: 21.3.0
    N©r)r©r úFd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\attr/validators.pyr+scCstƒ S)zÀ
    Return a bool indicating whether validators are currently disabled or not.
 
    :return: ``True`` if validators are currently disabled.
    :rtype: bool
 
    .. versionadded:: 21.3.0
    )rr r r r!r=s    ccs"tdƒz
dVW5tdƒXdS)z°
    Context manager that disables running validators within its context.
 
    .. warning::
 
        This context manager is not thread-safe!
 
    .. versionadded:: 21.3.0
    FTNrr r r r!rIs 
FT)ÚreprÚslotsÚhashc@s"eZdZeƒZdd„Zdd„ZdS)Ú_InstanceOfValidatorcCs4t||jƒs0tdj|j|j|j|d||j|ƒ‚dS)úP
        We use a callable class to be able to change the ``__repr__``.
        z?'{name}' must be {type!r} (got {value!r} that is a {actual!r}).)ÚnameÚtypeÚactualÚvalueN)Ú
isinstancer(Ú    TypeErrorÚformatr'Ú    __class__©ÚselfÚinstÚattrr*r r r!Ú__call___s ûöz_InstanceOfValidator.__call__cCsdj|jdS)Nz)<instance_of validator for type {type!r}>©r(©r-r(©r0r r r!Ú__repr__qsÿz_InstanceOfValidator.__repr__N©Ú__name__Ú
__module__Ú __qualname__r    r(r3r7r r r r!r%[sr%cCst|ƒS)aÒ
    A validator that raises a `TypeError` if the initializer is called
    with a wrong type for this particular attribute (checks are performed using
    `isinstance` therefore it's also valid to pass a tuple of types).
 
    :param type: The type to check for.
    :type type: type or tuple of type
 
    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected type, and the value it
        got.
    )r%r4r r r!rws )r"Úfrozenr#c@s(eZdZeƒZeƒZdd„Zdd„ZdS)Ú_MatchesReValidatorcCs0| |¡s,tdj|j|jj|d||j|ƒ‚dS)r&z9'{name}' must match regex {pattern!r} ({value!r} doesn't))r'Úpatternr*N)Ú
match_funcÚ
ValueErrorr-r'r>r/r r r!r3Œs
þùz_MatchesReValidator.__call__cCsdj|jdS)Nz.<matches_re validator for pattern {pattern!r}>)r>)r-r>r6r r r!r7›sÿz_MatchesReValidator.__repr__N)r9r:r;r    r>r?r3r7r r r r!r=‡sr=c    Csœtjdtjtjf}||kr@td d tdd„t|ƒDƒƒ¡¡ƒ‚t    |t
ƒr\|rVt dƒ‚|}n t  ||¡}|tjkrz|j}n|tjkrŒ|j}n|j}t ||ƒS)aœ
    A validator that raises `ValueError` if the initializer is called
    with a string that doesn't match *regex*.
 
    :param regex: a regex string or precompiled pattern to match against
    :param int flags: flags that will be passed to the underlying re function
        (default 0)
    :param callable func: which underlying `re` function to call. Valid options
        are `re.fullmatch`, `re.search`, and `re.match`; the default ``None``
        means `re.fullmatch`. For performance reasons, the pattern is always
        precompiled using `re.compile`.
 
    .. versionadded:: 19.2.0
    .. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern.
    Nz'func' must be one of {}.z, css|]}|r|jpdVqdS)ÚNoneN)r9)Ú.0Úer r r!Ú    <genexpr>¶szmatches_re.<locals>.<genexpr>zR'flags' can only be used with a string pattern; pass flags to re.compile() instead)ÚreÚ    fullmatchÚsearchÚmatchr@r-ÚjoinÚsortedÚsetr+rr,Úcompiler=)ÚregexÚflagsÚfuncZ valid_funcsr>r?r r r!r¡s0ÿÿÿÿ
 
ÿ 
 
c@s"eZdZeƒZdd„Zdd„ZdS)Ú_ProvidesValidatorcCs0|j |¡s,tdj|j|j|d||j|ƒ‚dS)r&z<'{name}' must provide {interface!r} which {value!r} doesn't.)r'Ú    interfacer*N)rQZ
providedByr,r-r'r/r r r!r3Õs þùz_ProvidesValidator.__call__cCsdj|jdS)Nz0<provides validator for interface {interface!r}>)rQ)r-rQr6r r r!r7äsÿz_ProvidesValidator.__repr__N)r9r:r;r    rQr3r7r r r r!rPÑsrPcCs ddl}|jdtddt|ƒS)aE
    A validator that raises a `TypeError` if the initializer is called
    with an object that does not provide the requested *interface* (checks are
    performed using ``interface.providedBy(value)`` (see `zope.interface
    <https://zopeinterface.readthedocs.io/en/latest/>`_).
 
    :param interface: The interface to check for.
    :type interface: ``zope.interface.Interface``
 
    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected interface, and the
        value it got.
 
    .. deprecated:: 23.1.0
    rNzZattrs's zope-interface support is deprecated and will be removed in, or after, April 2024.é)Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningrP)rQrTr r r!rêsüc@s"eZdZeƒZdd„Zdd„ZdS)Ú_OptionalValidatorcCs|dkr dS| |||¡dS)N©Ú    validatorr/r r r!r3    sz_OptionalValidator.__call__cCsdjt|jƒdS)Nz'<optional validator for {what} or None>)Úwhat)r-r"rYr6r r r!r7sÿz_OptionalValidator.__repr__N)r9r:r;r    rYr3r7r r r r!rWsrWcCs"t|ttfƒrtt|ƒƒSt|ƒS)aü
    A validator that makes an attribute optional.  An optional attribute is one
    which can be set to ``None`` in addition to satisfying the requirements of
    the sub-validator.
 
    :param Callable | tuple[Callable] | list[Callable] validator: A validator
        (or validators) that is used for non-``None`` values.
 
    .. versionadded:: 15.1.0
    .. versionchanged:: 17.1.0 *validator* can be a list of validators.
    .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
    )r+ÚlistÚtuplerWrrXr r r!rs  c@s"eZdZeƒZdd„Zdd„ZdS)Ú _InValidatorcCsPz||jk}Wntk
r&d}YnX|sLtdj|j|j|d||j|ƒ‚dS)NFz/'{name}' must be in {options!r} (got {value!r}))r'Úoptionsr*)r^r,r@r-r')r0r1r2r*Z
in_optionsr r r!r3,s
ÿúz_InValidator.__call__cCsdj|jdS)Nz(<in_ validator with options {options!r}>©r^)r-r^r6r r r!r7<sÿz_InValidator.__repr__N)r9r:r;r    r^r3r7r r r r!r](sr]cCst|ƒS)a¶
    A validator that raises a `ValueError` if the initializer is called
    with a value that does not belong in the options provided.  The check is
    performed using ``value in options``.
 
    :param options: Allowed options.
    :type options: list, tuple, `enum.Enum`, ...
 
    :raises ValueError: With a human readable error message, the attribute (of
       type `attrs.Attribute`), the expected options, and the value it
       got.
 
    .. versionadded:: 17.1.0
    .. versionchanged:: 22.1.0
       The ValueError was incomplete until now and only contained the human
       readable error message. Now it contains all the information that has
       been promised since 17.1.0.
    )r]r_r r r!rBsc@seZdZdd„Zdd„ZdS)Ú_IsCallableValidatorcCs,t|ƒs(d}t|j|j||jd|d‚dS)r&z?'{name}' must be callable (got {value!r} that is a {actual!r}).)r'r*r))Úmsgr*N)Úcallabler r-r'r.)r0r1r2r*Úmessager r r!r3Zsÿÿüz_IsCallableValidator.__call__cCsdS)Nz<is_callable validator>r r6r r r!r7jsz_IsCallableValidator.__repr__N)r9r:r;r3r7r r r r!r`Xsr`cCstƒS)ax
    A validator that raises a `attrs.exceptions.NotCallableError` if the
    initializer is called with a value for this particular attribute
    that is not callable.
 
    .. versionadded:: 19.1.0
 
    :raises attrs.exceptions.NotCallableError: With a human readable error
        message containing the attribute (`attrs.Attribute`) name,
        and the value it got.
    )r`r r r r!rns c@s:eZdZeeƒdZedeeƒƒdZdd„Zdd„Z    dS)Ú _DeepIterablerXN©ÚdefaultrYcCs4|jdk    r| |||¡|D]}| |||¡qdS©r&N)Úiterable_validatorÚmember_validator)r0r1r2r*Úmemberr r r!r3„s
z_DeepIterable.__call__cCs*|jdkrdn
d|j›}dj||jdS)NÚú zJ<deep_iterable validator for{iterable_identifier} iterables of {member!r}>)Úiterable_identifierrj)rhr-ri)r0rmr r r!r7Žsÿ
ýÿûz_DeepIterable.__repr__)
r9r:r;r    rrirrhr3r7r r r r!rd}s ÿ
rdcCs t|ttfƒrt|Ž}t||ƒS)a4
    A validator that performs deep validation of an iterable.
 
    :param member_validator: Validator(s) to apply to iterable members
    :param iterable_validator: Validator to apply to iterable itself
        (optional)
 
    .. versionadded:: 19.1.0
 
    :raises TypeError: if any sub-validators fail
    )r+r[r\rrd©rirhr r r!r s c@sFeZdZeeƒdZeeƒdZedeeƒƒdZdd„Z    dd„Z
dS)Ú _DeepMappingrXNrecCsF|jdk    r| |||¡|D]$}| |||¡| ||||¡qdSrg)Úmapping_validatorÚ key_validatorÚvalue_validator)r0r1r2r*Úkeyr r r!r3´s
 
z_DeepMapping.__call__cCsdj|j|jdS)NzA<deep_mapping validator for objects mapping {key!r} to {value!r}>)rsr*)r-rqrrr6r r r!r7¿s
ÿþz_DeepMapping.__repr__) r9r:r;r    rrqrrrrpr3r7r r r r!ro®s
   rocCs t|||ƒS)a}
    A validator that performs deep validation of a dictionary.
 
    :param key_validator: Validator to apply to dictionary keys
    :param value_validator: Validator to apply to dictionary values
    :param mapping_validator: Validator to apply to top-level mapping
        attribute (optional)
 
    .. versionadded:: 19.1.0
 
    :raises TypeError: if any sub-validators fail
    )ro)rqrrrpr r r!rÅs c@s.eZdZeƒZeƒZeƒZdd„Zdd„ZdS)Ú_NumberValidatorcCs.| ||j¡s*tdj|j|j|j|dƒ‚dS)r&z&'{name}' must be {op} {bound}: {value})r'ÚopÚboundr*N)Ú compare_funcrvr@r-r'Ú
compare_opr/r r r!r3Ûsüÿz_NumberValidator.__call__cCsdj|j|jdS)Nz<Validator for x {op} {bound}>)rurv)r-rxrvr6r r r!r7ésÿz_NumberValidator.__repr__N)    r9r:r;r    rvrxrwr3r7r r r r!rtÕs
rtcCst|dtjƒS)zÇ
    A validator that raises `ValueError` if the initializer is called
    with a number larger or equal to *val*.
 
    :param val: Exclusive upper bound for values
 
    .. versionadded:: 21.3.0
    ú<)rtÚoperatorr©Úvalr r r!rïs    cCst|dtjƒS)zÁ
    A validator that raises `ValueError` if the initializer is called
    with a number greater than *val*.
 
    :param val: Inclusive upper bound for values
 
    .. versionadded:: 21.3.0
    z<=)rtrzrr{r r r!rûs    cCst|dtjƒS)zÁ
    A validator that raises `ValueError` if the initializer is called
    with a number smaller than *val*.
 
    :param val: Inclusive lower bound for values
 
    .. versionadded:: 21.3.0
    z>=)rtrzrr{r r r!rs    cCst|dtjƒS)zÈ
    A validator that raises `ValueError` if the initializer is called
    with a number smaller or equal to *val*.
 
    :param val: Exclusive lower bound for values
 
    .. versionadded:: 21.3.0
    ú>)rtrzrr{r r r!rs    c@s"eZdZeƒZdd„Zdd„ZdS)Ú_MaxLengthValidatorcCs.t|ƒ|jkr*tdj|j|jt|ƒdƒ‚dS)r&z*Length of '{name}' must be <= {max}: {len})r'ÚmaxÚlenN)r€Ú
max_lengthr@r-r'r/r r r!r3#sÿÿz_MaxLengthValidator.__call__cCsd|j›dS)Nz<max_len validator for r})rr6r r r!r7.sz_MaxLengthValidator.__repr__N)r9r:r;r    rr3r7r r r r!r~s r~cCst|ƒS)zæ
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is longer than *length*.
 
    :param int length: Maximum length of the string or iterable
 
    .. versionadded:: 21.3.0
    )r~©Úlengthr r r!r2s    c@s"eZdZeƒZdd„Zdd„ZdS)Ú_MinLengthValidatorcCs.t|ƒ|jkr*tdj|j|jt|ƒdƒ‚dS)r&z*Length of '{name}' must be => {min}: {len})r'Úminr€N)r€Ú
min_lengthr@r-r'r/r r r!r3Bsÿÿz_MinLengthValidator.__call__cCsd|j›dS)Nz<min_len validator for r})r†r6r r r!r7Msz_MinLengthValidator.__repr__N)r9r:r;r    r†r3r7r r r r!r„>s r„cCst|ƒS)zç
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is shorter than *length*.
 
    :param int length: Minimum length of the string or iterable
 
    .. versionadded:: 22.1.0
    )r„r‚r r r!rQs    c@s"eZdZeƒZdd„Zdd„ZdS)Ú_SubclassOfValidatorcCs0t||jƒs,tdj|j|j|d||j|ƒ‚dS)r&z8'{name}' must be a subclass of {type!r} (got {value!r}).)r'r(r*N)Ú
issubclassr(r,r-r'r/r r r!r3as ü÷z_SubclassOfValidator.__call__cCsdj|jdS)Nz)<subclass_of validator for type {type!r}>r4r5r6r r r!r7rsÿz_SubclassOfValidator.__repr__Nr8r r r r!r‡]sr‡cCst|ƒS)aÓ
    A validator that raises a `TypeError` if the initializer is called
    with a wrong type for this particular attribute (checks are performed using
    `issubclass` therefore it's also valid to pass a tuple of types).
 
    :param type: The type to check for.
    :type type: type or tuple of types
 
    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected type, and the value it
        got.
    )r‡r4r r r!Ú _subclass_ofxs r‰c@sJeZdZeƒZeedƒdZeeee    ƒe
e ƒddZ dd„Z dd„Zd    S)
Ú _NotValidatorzCnot_ validator child '{validator!r}' did not raise a captured error)Ú    converterrnrXcCsRz| |||¡Wn|jk
r(Yn&Xt|jj|j|jd||j||jƒ‚dS)N)rYÚ    exc_types)rYrŒr@rar-r/r r r!r3˜sþøz_NotValidator.__call__cCsdj|j|jdS)Nz;<not_ validator wrapping {what!r}, capturing {exc_types!r}>)rZrŒ)r-rYrŒr6r r r!r7©s
ÿüz_NotValidator.__repr__N)r9r:r;r    rYr rar r‰Ú    Exceptionrr\rŒr3r7r r r r!rŠˆsÿÿþÿrŠ)rarŒcCs4z t|ƒ}Wntk
r&|f}YnXt|||ƒS)a 
    A validator that wraps and logically 'inverts' the validator passed to it.
    It will raise a `ValueError` if the provided validator *doesn't* raise a
    `ValueError` or `TypeError` (by default), and will suppress the exception
    if the provided validator *does*.
 
    Intended to be used with existing validators to compose logic without
    needing to create inverted variants, for example, ``not_(in_(...))``.
 
    :param validator: A validator to be logically inverted.
    :param msg: Message to raise if validator fails.
        Formatted with keys ``exc_types`` and ``validator``.
    :type msg: str
    :param exc_types: Exception type(s) to capture.
        Other types raised by child validators will not be intercepted and
        pass through.
 
    :raises ValueError: With a human readable error message,
        the attribute (of type `attrs.Attribute`),
        the validator that failed to raise an exception,
        the value it got,
        and the expected exception types.
 
    .. versionadded:: 22.2.0
    )r\r,rŠ)rYrarŒr r r!r²s
  )rN)N)N)5Ú__doc__rzrEÚ
contextlibrrÚ_configrrÚ_makerrr    r
Ú
convertersr Ú
exceptionsr Ú__all__rrrr%rr=rrPrrWrr]rr`rrdr rorrtrrrrr~rr„rr‡r‰rŠr@r,rr r r r!Ú<module>s”    í 
 
0     
 
        )