Xj1TxdZddlZddlZddlmZddlmZddlmZmZddl m Z m Z m Z m Z ddlmZdd lmZgd Zd Zd Zed Ze dddGddZdZe dddGddZd=dZe dddGddZdZe dddGddZdZe dddGddZd Ze dddGd!d"Z d>d#Z!e dddGd$d%Z" d?d&Z#e dddGd'd(Z$d)Z%d*Z&d+Z'd,Z(e dddGd-d.Z)d/Z*e dddGd0d1Z+d2Z,e dddGd3d4Z-d5Z.e dddGd6d7Z/de0e1fd8d9Z2e dddGd:d;Z3d<Z4y)@z Commonly useful validators. N)contextmanager)Patternget_run_validatorsset_run_validators) _AndValidatorand_attribattrs)default_if_none)NotCallableError)r deep_iterable deep_mappingdisabledge get_disabledgtin_ instance_of is_callablelelt matches_remax_lenmin_lennot_optionalor_ set_disabledct| y)a Globally disable or enable running validators. By default, they are run. Args: disabled (bool): If `True`, disable running all validators. .. warning:: This function is not thread-safe! .. versionadded:: 21.3.0 N)r)rs c/mnt/ssd/data/Dropbox/adrian/vault-secondbrain/venv/lib/python3.12/site-packages/attr/validators.pyr r *s8|$ct S)z Return a bool indicating whether validators are currently disabled or not. Returns: bool:`True` if validators are currently disabled. .. versionadded:: 21.3.0 )rr#r"rr<s"# ##r#c#vKt}td dt|y#t|wxYww)z Context manager that disables running validators within its context. .. warning:: This context manager is not thread-safe! .. versionadded:: 21.3.0 .. versionchanged:: 26.1.0 The contextmanager is nestable. FNr)prevs r"rrHs1  Du! 4 4 s9) 9 69FT)reprslots unsafe_hashc&eZdZeZdZdZy)_InstanceOfValidatorc t||jsEd|jd|jd|d|jd }t |||j|y)P We use a callable class to be able to change the ``__repr__``. ' ' must be  (got z that is a ).N) isinstancetypename __class__ TypeErrorselfinstattrvaluemsgs r"__call__z_InstanceOfValidator.__call__`se%+dii[ 499-veYkRWRaRaQddfgC   ,r#c"d|jdS)Nz r4r9s r"__repr__z_InstanceOfValidator.__repr__m1$))a@@r#N__name__ __module__ __qualname__r r4r>rCr%r#r"r,r,\ 8D Ar#r,ct|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). Args: type (type | tuple[type]): The type to check for. Raises: TypeError: With a human readable error message, the attribute (of type `attrs.Attribute`), the expected type, and the value it got. )r,rAs r"rrq  %%r#)r(frozenr)c4eZdZeZeZdZdZy)_MatchesReValidatorc|j|sBd|jd|jjd|d}t|||j|y)r.r/z' must match regex z (z doesn't)N) match_funcr5pattern ValueErrorr8s r"r>z_MatchesReValidator.__call__s]u%dii[ 3DLL4H4H3K2eYV_`C   &r#c"d|jdS)Nz"rCr%r#r"rNrNshGJ Fr#rNc tjdtjtjf}||vrDdj dj t dt|D}t|t|tr|r d}t||}ntj||}|tjur |j}n+|tjur |j}n |j}t||S)a A validator that raises `ValueError` if the initializer is called with a string that doesn't match *regex*. Args: regex (str, re.Pattern): A regex string or precompiled pattern to match against flags (int): Flags that will be passed to the underlying re function (default 0) func (typing.Callable): 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, c3DK|]}|xr |jxsdyw)NoneN)rF).0es r" zmatches_re..s!N(ajj3V3Ns zR'flags' can only be used with a string pattern; pass flags to re.compile() instead)re fullmatchsearchmatchformatjoinsortedsetrRr3rr7compilerN)regexflagsfunc valid_funcsr=rQrPs r"rrs*<<ryy"((;K ;)00 IINS=MNN   o%! fCC. **UE* rxx]]  ^^ && w 33r#c&eZdZeZdZdZy)_OptionalValidatorc0|y|j|||yN validatorr9r:r;r<s r"r>z_OptionalValidator.__call__s =  tT5)r#c"d|jdS)NzrkrBs r"rCz_OptionalValidator.__repr__s)$..);9EEr#N)rFrGrHr rlr>rCr%r#r"rhrhsI* Fr#rhclt|ttfrtt |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. Args: validator (typing.Callable | tuple[typing.Callable] | list[typing.Callable]): 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. )r3listtuplerhr rks r"rrs-)dE]+!- ":;; i ((r#c8eZdZeZedZdZdZy) _InValidatorF)hashc ||jv}|s8d|jd|jd|d}t |||j|y#t$rd}YHwxYw)NFr/z ' must be in r1))optionsr7r5_original_optionsrR)r9r:r;r< in_optionsr=s r"r>z_InValidator.__call__sz $,,.Jdii[ d.D.D-GveYVWXC&&   J sA AAc"d|jdS)NzrCr%r#r"rsrsshGE* Jr#rscj|}t|tttfr t |}t ||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``, so *options* has to support that operation. To keep the validator hashable, dicts, lists, and sets are transparently transformed into a `tuple`. Args: options: Allowed options. 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. .. versionchanged:: 24.1.0 *options* that are a list, dict, or a set are now transformed into a tuple to keep the validator hashable. )r3rpdictrarqrs)rw repr_optionss r"rrs08L'D$,-.  ..r#ceZdZdZdZy)_IsCallableValidatorct|s5d}t|j|j||j|y)r.z?'{name}' must be callable (got {value!r} that is a {actual!r}).)r5r<actual)r=r<N)callablerr^r5r6)r9r:r;r<messages r"r>z_IsCallableValidator.__call__(sN8 #NN%#   r#cy)Nzr%rBs r"rCz_IsCallableValidator.__repr__8s(r#N)rFrGrHr>rCr%r#r"rr&s  )r#rctS)a 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. )rr%r#r"rr<s  !!r#c^eZdZeeZedeeZdZdZ y) _DeepIterablerkN)defaultrlcv|j|j||||D]}|j|||yr.Niterable_validatormember_validator)r9r:r;r<members r"r>z_DeepIterable.__call__SsC  " " .  # #D$ 6 6F  ! !$f 5 6r#cb|jdnd|j}d|d|jdS)N z*?T225Q 8 r#) rFrGrHr rrrrr>rCr%r#r"rrLs0 6 76  r#rct|ttfrt|}t|ttfrt|}t ||S)a A validator that performs deep validation of an iterable. Args: member_validator: Validator(s) to apply to iterable members. iterable_validator: Validator(s) to apply to iterable itself (optional). Raises TypeError: if any sub-validators fail .. versionadded:: 19.1.0 .. versionchanged:: 25.4.0 *member_validator* and *iterable_validator* can now be a list or tuple of validators. )r3rprqr rrrs r"rrisI&"T5M2!12$tUm4!#56 )+= >>r#ceZdZeeeZeeeZeeeZdZ dZ y) _DeepMappingrkc|j|j||||D]D}|j|j||||j/|j||||Fyr)mapping_validator key_validatorvalue_validator)r9r:r;r<keys r"r>z_DeepMapping.__call__sq  ! ! -  " "4u 5 =C!!-""4s3##/$$T4s<  =r#c<d|jd|jdS)Nz,P>P=SSWX\XlXlWoopqqr#N) rFrGrHr rrrrrr>rCr%r#r"rrsAXkm%<=Mx '>?O+-)@A =rr#rc|| d}t|t|ttfrt |}t|ttfrt |}t|ttfrt |}t |||S)a A validator that performs deep validation of a dictionary. All validators are optional, but at least one of *key_validator* or *value_validator* must be provided. Args: key_validator: Validator(s) to apply to dictionary keys. value_validator: Validator(s) to apply to dictionary values. mapping_validator: Validator(s) to apply to top-level mapping attribute. .. versionadded:: 19.1.0 .. versionchanged:: 25.4.0 *key_validator* and *value_validator* are now optional, but at least one of them must be provided. .. versionchanged:: 25.4.0 *key_validator*, *value_validator*, and *mapping_validator* can now be a list or tuple of validators. Raises: TypeError: If any sub-validator fails on validation. ValueError: If neither *key_validator* nor *value_validator* is provided on instantiation. zAAt least one of key_validator or value_validator must be provided)rRr3rprqr r)rrrr=s r"rrsD!8 O o-$/m, /D%=10#dE]3 "34  8I JJr#cBeZdZeZeZeZdZdZy)_NumberValidatorc|j||js7d|jd|jd|jd|}t |y)r.r/r0r: N) compare_funcboundr5 compare_oprRr8s r"r>z_NumberValidator.__call__sR   3dii[ 4??*;1TZZL5'RCS/ !4r#c<d|jd|jdS)NzrCr%r#r"rrs" HEJ8L"Cr#rc8t|dtjS)a  A validator that raises `ValueError` if the initializer is called with a number larger or equal to *val*. The validator uses `operator.lt` to compare the values. Args: val: Exclusive upper bound for values. .. versionadded:: 21.3.0 <)roperatorrvals r"rr Chkk 22r#c8t|dtjS)a A validator that raises `ValueError` if the initializer is called with a number greater than *val*. The validator uses `operator.le` to compare the values. Args: val: Inclusive upper bound for values. .. versionadded:: 21.3.0 z<=)rrrrs r"rr Cx{{ 33r#c8t|dtjS)a A validator that raises `ValueError` if the initializer is called with a number smaller than *val*. The validator uses `operator.ge` to compare the values. Args: val: Inclusive lower bound for values .. versionadded:: 21.3.0 z>=)rrrrs r"rrrr#c8t|dtjS)a  A validator that raises `ValueError` if the initializer is called with a number smaller or equal to *val*. The validator uses `operator.gt` to compare the values. Args: val: Exclusive lower bound for values .. versionadded:: 21.3.0 r@)rrrrs r"rr rr#c&eZdZeZdZdZy)_MaxLengthValidatorct||jkDr3d|jd|jdt|}t|y)r. Length of 'z ' must be <= rN)len max_lengthr5rRr8s r"r>z_MaxLengthValidator.__call__H u: ' {-7H3u:,WCS/ ! (r#c"d|jdS)NzrCr%r#r"rrJ"= rN)r min_lengthr5rRr8s r"r>z_MinLengthValidator.__call__;rr#c"d|jdS)NzrCr%r#r"rr7rr#rct|S)z A validator that raises `ValueError` if the initializer is called with a string or iterable that is shorter than *length*. Args: length (int): Minimum length of the string or iterable .. versionadded:: 22.1.0 )rrs r"rrGrr#c&eZdZeZdZdZy)_SubclassOfValidatorct||js8d|jd|jd|d}t|||j|y)r.r/z' must be a subclass of r1r2N) issubclassr4r5r7r8s r"r>z_SubclassOfValidator.__call__XsX%+dii[ 8 VE9TVWC   ,r#c"d|jdS)Nz z_NotValidator.__call__sw  NN4u -"nn"nn   ~~   sA''A98A9c<d|jd|jdS)Nz>*rCr%r#r"rrzsOI ! -  C ))4*51 I"]r#r)r=rcX t|}t|||S#t$r|f}YwxYw)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_(...))``. Args: validator: A validator to be logically inverted. msg (str): Message to raise if validator fails. Formatted with keys ``exc_types`` and ``validator``. exc_types (tuple[type, ...]): 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 )rqr7r)rlr=rs r"rrs::!)$  C 33 !L !s  ))c&eZdZeZdZdZy) _OrValidatorc|jD]} ||||yd|jd|}t|#t$rY9wxYw)NzNone of z satisfied for value ) validatorsrrR)r9r:r;r<vr=s r"r>z_OrValidator.__call__sc A $e$  ++@ Jo   s ; AAc"d|jdS)NzrCr%r#r"rrsJ >r#rcg}|D]0}|jt|tr |jn|g2tt |S)a A validator that composes multiple validators into one. When called on a value, it runs all wrapped validators until one of them is satisfied. Args: validators (~collections.abc.Iterable[typing.Callable]): Arbitrary number of validators. Raises: ValueError: If no validator is satisfied. Raised with a human-readable error message listing all the wrapped validators and the value that failed all of them. .. versionadded:: 24.1.0 )extendr3rrrq)rvalsrs r"rrsH& D J Jq,$?ALLaSIJ d $$r#)rNrj)NNN)5__doc__rrZ contextlibrr_configrr_maker r r r convertersr exceptionsr__all__r rrr,rrNrrhrrsrrrrrrrrrrrrrrrrrrrrRr7rrrr%r#r"rs %;55'( .%$ $!!&E40AA1A(&"E$d+FF,F*-4`E40 F F1 F)*E40JJ1J. /FED1))2)* " E40  1 8?4E40rr1r.AE/KdE$d+CC,C" 3 4 4 3E$d+ < <, < 'E$d+ < <, < 'E40AA1A(&"E40!]!]1!]H J +B!4HE40>>1>&%r#