
    QЦib                        S SK rS SKrS SKrS SKrS SKJrJrJrJ	r	J
r
Jr  S SKJrJr  S SKJrJrJrJrJrJrJrJrJrJrJrJr   S SKJrJr  S	S
K!J"r"J#r#J$r$  \\/S4   r%\" S5      r&\" S\'\(\4   S9r)\*" 5       r+\S\\&   S\\&   4S j5       r,\SS.S\-S\\\&   /\\&   4   4S jj5       r, S<SS.S\\\&      S\-S\\\&   \\\&   /\\&   4   4   4S jjjr,S\ \)   S\'SS4S jr.\S\ \)   S\4S j5       r/S\ \)   S\'\(\4   4S jr0\\SSSSS4S\\1\%   \%4   S\\\4   S\\/ \4   \4   S\-S \-S!\\-   S"\-S#\\'   S$\S\4S% jjr2S\%4S& jr3S'\(S(\S)\SS4S* jr4S'\(S(\S+\5\S,4   SS4S- jr6S'\(S(\S+\5\S,4   SS4S. jr7S'\(S(\S+\5\S,4   SS4S/ jr8S'\(S(\S+\5\S,4   SS4S0 jr9S'\(S(\S+\5\S,4   SS4S1 jr:S'\(S(\S+\5\S,4   SS4S2 jr;S'\(S(\S+\5\S,4   SS4S3 jr<S'\(S(\S)\ SS4S4 jr=S5\S\%4S6 jr>S\S\-4S7 jr?S8\S\-4S9 jr@\\6\\7\1\8\'\9\5\:\A\;\R                  R                  \<0rD\R                  S::  a  \6\D\R                  '   / S;QrGg! \ a/     S SKJrJr   GN! \ a    \ " SS0 5      r\ " SS0 5      r  GN,f = ff = f)=    N)_MISSING_TYPEMISSINGFieldfieldfieldsmake_dataclass)	lru_cachewraps)	AnnotatedAnyCallable
ForwardRefLiteralOptionalTypeTypeVarUnionget_args
get_originoverload)NotRequiredRequiredr    r      )#StrictDataclassClassValidationErrorStrictDataclassDefinitionError#StrictDataclassFieldValidationErrorTTypedDictType)boundclsreturnc                     g Nr   )r!   s    Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/huggingface_hub/dataclasses.pystrictr&   2   s    %(    Faccept_kwargsr)   c                     g r$   r   r(   s    r%   r&   r&   6   s    LOr'   c                b   ^ S[         [           S[         [           4U4S jjnU b  U" U 5      $ U$ )a  
Decorator to add strict validation to a dataclass.

This decorator must be used on top of `@dataclass` to ensure IDEs and static typing tools
recognize the class as a dataclass.

Can be used with or without arguments:
- `@strict`
- `@strict(accept_kwargs=True)`

Args:
    cls:
        The class to convert to a strict dataclass.
    accept_kwargs (`bool`, *optional*):
        If True, allows arbitrary keyword arguments in `__init__`. Defaults to False.

Returns:
    The enhanced dataclass with strict validation on field assignment.

Example:
```py
>>> from dataclasses import dataclass
>>> from huggingface_hub.dataclasses import as_validated_field, strict, validated_field

>>> @as_validated_field
>>> def positive_int(value: int):
...     if not value >= 0:
...         raise ValueError(f"Value must be positive, got {value}")

>>> @strict(accept_kwargs=True)
... @dataclass
... class User:
...     name: str
...     age: int = positive_int(default=10)

# Initialize
>>> User(name="John")
User(name='John', age=10)

# Extra kwargs are accepted
>>> User(name="John", age=30, lastname="Doe")
User(name='John', age=30, *lastname='Doe')

# Invalid type => raises
>>> User(name="John", age="30")
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    TypeError: Field 'age' expected int, got str (value: '30')

# Invalid value => raises
>>> User(name="John", age=-1)
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    ValueError: Value must be positive, got -1
```
r!   r"   c           	        >^ ^^^ [        T S5      (       d  [        ST R                   S35      e0 n[        T 5       H  n/ nUR	                  [        U5      5        UR                  R                  S5      nUb^  [        U[        5      (       d  U/nU H/  n[        U5      (       a  M  [        SUR                   SU S35      e   UR                  U5        X1UR                  '   M     UT l        T R                  mS[        S	[         S
[        SS 4U4S jjnUT l        T(       a  T R"                  n[%        U5      S[        SS 4U 4S jj5       nUT l        [        T S5      (       d  S[        SS 4S jn	U	T l        T R(                  m[%        T5      S[         4U U4S jj5       n
T R*                  R,                  SL a  U
T l        / n[/        T 5       H  nUR1                  S5      (       d  M  [3        T U5      n[5        U5      (       d  M9  [7        [8        R:                  " U5      R<                  5      S:w  a  [        ST R                   SU S35      eUR	                  U5        M     UT l        S[@        SS 4U 4S jjnSUl!        [        T S5      (       a5  [3        T RD                  SS5      (       d  [        ST R                   S35      eUT l"        T R"                  m[%        T5      SU U4S jj5       n[G        T SU5        T $ )N__dataclass_fields__zClass 'z.' must be a dataclass before applying @strict.	validatorzInvalid validator for field '': z.. Must be a callable taking a single argument.selfnamevaluer"   c                    > U R                   R                  U/ 5       H  n U" U5        M     T" XU5        g! [        [        4 a  n[	        XS9UeSnAff = f)z1Custom __setattr__ method for strict dataclasses.)r   causeN)__validators__get
ValueError	TypeErrorr   )r0   r1   r2   r.   eoriginal_setattrs        r%   __strict_setattr__0strict.<locals>.wrap.<locals>.__strict_setattr__   sc     "0044T2>	Ze$ ? T/	 #I. Z=DRXYYZs   8A
AAkwargsc                 (  > [        T5       Vs1 s H  o3R                  iM     nnUR                  5        VVs0 s H  u  pVXT;   d  M  XV_M     nnn[        U5      S:  a%  [	        STR
                   S[        U5       S35      e[        T5       H  nUR                  U;   a$  [        XR                  XsR                     5        M7  UR                  [        La"  [        XR                  UR                  5        Ml  UR                  [        La&  [        XR                  UR                  5       5        M  [        SUR                   S35      e   0 nUR                  5        H  u  pX;  d  M  XU	'   M     U R                  " S0 UD6  g s  snf s  snnf )Nr   zWhen `accept_kwargs=True`, z, accepts only keyword arguments, but found `z` positional args.zMissing required field - ''r   )r   r1   itemslenr7   __name__setattrdefaultr   default_factoryr8   __post_init__)r0   argsr=   fdataclass_fieldskvstandard_kwargsadditional_kwargsr1   r2   r!   s              r%   __init__&strict.<locals>.wrap.<locals>.__init__   sT    5;3K#@KqFFK #@4:LLN"\NDAaF[414N"\ t9q=$5cll^ D&&)$i[0BD 
  Avv0ffoff.EF'1ffaii8**'9ffa.?.?.AB'*DQVVHA(NOO % %'!#)<<>KD327$/ $2 ""7%67= $A"\s   F	FFrF   c                 N    UR                  5        H  u  p#[        XU5        M     g)z2Default __post_init__ to accept additional kwargs.N)r@   rC   )r0   r=   r1   r2   s       r%   rF   +strict.<locals>.wrap.<locals>.__post_init__   s    '-||~E2 (6r'   c                    > T" U 5      nU R                   R                  5        VVs/ s H   u  p#UTR                  ;  d  M  SU SU< 3PM"     nnnSR                  U5      nU(       a  US S  SU S3$ U$ s  snnf )N*=z, ))__dict__r@   r-   join)r0   standard_reprrJ   rK   rM   additional_reprr!   original_reprs         r%   __repr__&strict.<locals>.wrap.<locals>.__repr__   s     !.d 3 !% 3 3 5% !6 8 88 "as!A5M 5 " % #')),="> GX-,-R/@Bj]jj%s   A6A6T	validate_r   z' has a class validator 'z' that takes more than one argument. Class validators must take only 'self' as an argument. Methods starting with 'validate_' are considered to be class validators.c                    > TR                    H  n U" U 5        M     g! [        [        4 a  n[        UR                  US9UeSnAff = f)z%Run class validators on the instance.)r.   r4   N)__class_validators__r7   r8   r   rB   )r0   r.   r9   r!   s      r%   validate&strict.<locals>.wrap.<locals>.validate   sP     55	ldO 6 #I. l=	HZHZbcdjkkls   A	AA	ra   "__is_defined_by_strict_decorator__Fz' already implements a method called 'validate'. This method name is reserved when using the @strict decorator on a dataclass. If you want to keep your own method, please rename it.c                 @   > T" U /UQ70 UD6  TR                  U 5        g)z*Run class validators after initialization.N)ra   )r0   rG   r=   r!   initial_inits      r%   init_with_validate0strict.<locals>.wrap.<locals>.init_with_validate  s#     ///LLr'   rN   )r"   N)$hasattrr   rB   r   append_create_type_validatormetadatar6   
isinstancelist_is_validatorr1   extendr5   __setattr__r   strrN   r
   rF   r\   __dataclass_params__reprdir
startswithgetattrcallablerA   inspect	signature
parametersr`   r   rc   ra   rC   )r!   field_validatorsrH   
validatorscustom_validatorr.   r;   original_initrN   rF   r\   class_validatorsr1   methodra   rf   re   r[   r:   r)   s   `               @@@r%   wrapstrict.<locals>.wrapt   s   s2330#,,'UV 
 :<AJ4Q78 zz~~k:+!"2D99(8'9$!1I(33<;AFF83ykQ  A  "2
 !!"23'1QVV$  . ??
	0S 
	0 
	0C 
	0D 
	0 -LLM=! 8  8  8 " 8D $CL 3003# 3$ 3
 %2!  LLM=!k# k "k  '',,4' HD??;//S$'FF##7$$V,778A=4cll^+DTF K> > 
 ##F+  $4 	l1 	l 	l 7;33
##3<<)MuUU4cll^ ,N N    ||	|		 
	
 	Z!34
r'   )r   r   )r!   r)   r   s    ` r%   r&   r&   :   s8    th$q' hd1g hV 491T1r'   schemadatac                 *    [        U 5      nU" S0 UD6  g)an  
Validate that a dictionary conforms to the types defined in a TypedDict class.

Under the hood, the typed dict is converted to a strict dataclass and validated using the `@strict` decorator.

Args:
    schema (`type[TypedDictType]`):
        The TypedDict class defining the expected structure and types.
    data (`dict`):
        The dictionary to validate.

Raises:
    `StrictDataclassFieldValidationError`:
        If any field in the dictionary does not conform to the expected type.

Example:
```py
>>> from typing import Annotated, TypedDict
>>> from huggingface_hub.dataclasses import validate_typed_dict

>>> def positive_int(value: int):
...     if not value >= 0:
...         raise ValueError(f"Value must be positive, got {value}")

>>> class User(TypedDict):
...     name: str
...     age: Annotated[int, positive_int]

>>> # Valid data
>>> validate_typed_dict(User, {"name": "John", "age": 30})

>>> # Invalid type for age
>>> validate_typed_dict(User, {"name": "John", "age": "30"})
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    TypeError: Field 'age' expected int, got str (value: '30')

>>> # Invalid value for age
>>> validate_typed_dict(User, {"name": "John", "age": -1})
huggingface_hub.errors.StrictDataclassFieldValidationError: Validation error for field 'age':
    ValueError: Value must be positive, got -1
```
Nr   )!_build_strict_cls_from_typed_dict)r   r   
strict_clss      r%   validate_typed_dictr   "  s    X 36:J r'   c                    [        U 5      n[        U SS5      (       d  UR                  5        H~  u  p#[        U5      nU[        L aH  [        U5      tpV[        U5      (       d	  [        U   n[        [        U/[        U5      -   5         X'   Ma  [        U5      (       a  Ms  [        U   X'   M     / nUR                  5        Hi  u  p#[        U5      [        L a3  [        U5      tpVUR                  " X%[        [        SUS   0S945        MJ  UR                  " X#[        [        S945        Mk     [        [        U R                  U5      5      $ )N	__total__Tr.   r   )rD   rk   )rD   )_get_typed_dict_annotationsrv   r@   r   r   r   _is_required_or_notrequiredr   tuplerm   ri   r   _TYPED_DICT_DEFAULT_VALUEr&   r   rB   )r   
type_hintskeyr2   originbasemetar   s           r%   r   r   T  s"    -V4J 6;--$**,JC&F"&uo2488&t,D"+E4&4:2E,F"G
077"-e"4
 - F &&(
e	)"5/KDMM3e4MYdfjklfmXn&opqMM3u5N'OPQ ) .&9::r'   c           	           SSK nUR                  U 5      $ ! [         aW    U R                  R	                  S0 5      R                  5        VVs0 s H  u  p#X#b  UO
[        S5      _M     Os  snnf snns $ f = f)z0Extract type annotations from a TypedDict class.r   N__annotations__)annotationlibget_annotationsImportErrorrW   r6   r@   type)r   r   r1   r2   s       r%   r   r   s  sz    
,,V44 

  &223DbIOOQ	
  R ,%$t*<Q	
 	

s    7A8A+*A87A8Tr.   rD   rE   initrs   hashcomparerk   r=   c                 j    [        U [        5      (       d  U /n Uc  0 nXS'   [        SUUUUUUUS.UD6$ )a  
Create a dataclass field with a custom validator.

Useful to apply several checks to a field. If only applying one rule, check out the [`as_validated_field`] decorator.

Args:
    validator (`Callable` or `list[Callable]`):
        A method that takes a value as input and raises ValueError/TypeError if the value is invalid.
        Can be a list of validators to apply multiple checks.
    **kwargs:
        Additional arguments to pass to `dataclasses.field()`.

Returns:
    A field with the validator attached in metadata
r.   rD   rE   r   rs   r   r   rk   r   )rl   rm   r   )	r.   rD   rE   r   rs   r   r   rk   r=   s	            r%   validated_fieldr     s\    4 i&&K	%[ 	'	 	 	r'   c                    ^  [         [         SSSSS4S[        [        [        4   S[        [        / [        4   [        4   S[
        S[
        S[        [
           S[
        S	[        [           S
[        4U 4S jjjnU$ )z
Decorates a validator function as a [`validated_field`] (i.e. a dataclass field with a custom validator).

Args:
    validator (`Callable`):
        A method that takes a value as input and raises ValueError/TypeError if the value is invalid.
TNrD   rE   r   rs   r   r   rk   r=   c                 ,   > [        T4U UUUUUUS.UD6$ )Nr   )r   )	rD   rE   r   rs   r   r   rk   r=   r.   s	           r%   _inner"as_validated_field.<locals>._inner  s9     

+

 

 
	
r'   )r   r   r   r   r   boolr   dict)r.   r   s   ` r%   as_validated_fieldr     s     .5CJ##'
sM)*
xC0-?@
 
 	

 tn
 
 4.
 
 
, Mr'   r1   r2   expected_typec                    [        U5      n[        U5      nU[        L a  g[        R	                  U5      =n(       a
  U" XU5        g[        U[        5      (       a  [        XU5        g[        U[        5      (       d  [        U[        5      (       a  gU[        L a(  U[        L a  [        SU  S35      e[        XUS   5        gU[        L a  U[        L a  g[        XUS   5        g[        SU  SU 35      e)z.Validate that 'value' matches 'expected_type'.NField 'z' is required but missing.r   zUnsupported type for field 'r/   )r   r   r   _BASIC_TYPE_VALIDATORSr6   rl   r   _validate_simple_typer   rq   r   r   r8   type_validatorr   )r1   r2   r   r   rG   r.   s         r%   r   r     s    &FM"D,008	8	8$t$	M4	(	(d=9	M:	.	.*]C2P2P	8	--gdV+EFGGtDG,	;	--tDG,6tfCOPPr'   rG   .c                     / nU H  n [        XU5          g   [        SU  S[	        U5       SU SSR                  U5       35      e! [         a%  nUR                  [        U5      5         SnAMm  SnAff = f)z8Validate that value matches one of the types in a Union.Nr   z' with value z doesn't match any type in z
. Errors: z; )r   r8   ri   rq   rs   rX   )r1   r2   rG   errorstr9   s         r%   _validate_unionr     s    F	"4*  
$}T%[M1LTFR\]a]f]fgm]n\op   	"MM#a&!!	"s   A
A7A22A7c                 6    X;  a  [        SU  SU SU 35      eg)zValidate Literal type.r   z' expected one of , got N)r8   )r1   r2   rG   s      r%   _validate_literalr     s,    '$'9$veWMNN r'   c           	         [        U[        5      (       d$  [        SU  S[        U5      R                   35      eUS   n[        U5       H  u  pE [        U  SU S3XS5        M     g	! [         a  n[        SU SU  S35      UeS	nAff = f)
zValidate list[T] type.r   z' expected a list, got r   []Invalid item at index z
 in list 'r?   N)rl   rm   r8   r   rB   	enumerater   r1   r2   rG   	item_typeiitemr9   s          r%   _validate_listr     s    eT""'$'>tE{?S?S>TUVV QIU#	RdV1QCq>4; $  	R4QCz$qIJPQQ	Rs   A''
B	1BB	c                 :   [        U[        5      (       d$  [        SU  S[        U5      R                   35      eUu  p4UR                  5        H(  u  pV [        U  S3XS5        [        U  SU< S3Xd5        M*     g! [         a  n[        SU  S35      UeSnAff = f)	zValidate dict[K, V] type.r   z' expected a dict, got z.keyr   r   zInvalid key or value in dict 'r?   N)rl   r   r8   r   rB   r@   r   )r1   r2   rG   key_type
value_typerJ   rK   r9   s           r%   _validate_dictr     s    eT""'$'>tE{?S?S>TUVV  H	MdV4=!6dV1QE+Q;   	M<TF!DE1L	Ms   "A;;
BBBc           
      x   [        U[        5      (       d$  [        SU  S[        U5      R                   35      e[        U5      S:X  a7  US   [        L a+  [        U5       H  u  p4 [        U  SU S3XBS   5        M     g[        U5      [        U5      :w  a&  [        SU  S[        U5       S[        U5       35      e[        [        X5      5       H  u  nu  pF [        U  SU S3XF5        M     g! [         a  n[        SU S	U  S
35      UeSnAff = f! [         a  n[        SU S	U  S
35      UeSnAff = f)zValidate Tuple type.r   z' expected a tuple, got    r   r   r   r   r   z in tuple 'r?   Nz' expected a tuple of length r   )
rl   r   r8   r   rB   rA   Ellipsisr   r   zip)r1   r2   rG   r   r   r9   expecteds          r%   _validate_tupler     sT   eU##'$'?U@T@T?UVWW 4yA~$q'X- 'GAW$q1~t!W= ( 
Tc%j	 '$'DSYKvVYZ_V`Uabcc#,S-=#>AW$q1~t> $?  W"8;tfA NOUVVW  W"8;tfA NOUVVWs0   &C2D2
D<DD
D9!D44D9c                    [        U[        5      (       d$  [        SU  S[        U5      R                   35      eUS   n[        U5       H  u  pE [        U  S3XS5        M     g! [         a  n[        SU  S35      UeSnAff = f)zValidate set[T] type.r   z' expected a set, got r   z itemzInvalid item in set 'r?   N)rl   setr8   r   rB   r   r   r   s          r%   _validate_setr   2  s    eS!!'$'=d5k>R>R=STUU QIU#	DdV5>4; $  	D3D6;<!C	Ds   A$$
B.A>>Bc           	      P   [        U[        R                  R                  5      (       d$  [	        SU  S[        U5      R                   35      eU(       d  gUS   n[        U5       H  u  pE [        U  SU S3XS5        M     g! [         a  n[	        SU SU  S	35      UeSnAff = f)
z&Validate Sequence or Sequence[T] type.r   z' expected a Sequence, got Nr   r   r   r   z in sequence 'r?   )	rl   collectionsabcSequencer8   r   rB   r   r   r   s          r%   _validate_sequencer   @  s    e[__5566'$'B4;CWCWBXYZZ  QIU#	VdV1QCq>4; $  	V4QC~dV1MNTUU	Vs   ,B
B%B  B%c                     [        X5      (       d>  [        SU  SUR                   S[        U5      R                   S[	        U5       S3	5      eg)z&Validate simple type (int, str, etc.).r   z' expected r   z	 (value: rV   N)rl   r8   rB   r   rs   )r1   r2   r   s      r%   r   r   R  sY    e++dV;}'='=&>fT%[EYEYDZZcdhindocppqr
 	
 ,r'   r   c                 (   ^  S[         SS4U 4S jjnU$ )z-Create a type validator function for a field.r2   r"   Nc                 H   > [        TR                  U TR                  5        g r$   )r   r1   r   )r2   r   s    r%   r.   )_create_type_validator.<locals>.validator^  s    uzz5%**5r'   )r   )r   r.   s   ` r%   rj   rj   Z  s    6 6 6 r'   c                    [        U 5      (       d  g[        R                  " U 5      n[        UR                  R                  5       5      n[        U5      S:X  a  gUS   R                  [        R                  R                  [        R                  R                  [        R                  R                  4;  a  gUSS  H-  nUR                  [        R                  R                  :X  d  M-    g   g)zCheck if a function is a validator.

A validator is a Callable that can be called with a single positional argument.
The validator can have more arguments with default values.

Basically, returns True if `validator(value)` is possible.
Fr   r   NT)rw   rx   ry   rm   rz   valuesrA   kind	ParameterPOSITIONAL_OR_KEYWORDPOSITIONAL_ONLYVAR_POSITIONALrD   empty)r.   ry   rz   	parameters       r%   rn   rn   d  s     I!!),Ii**1134J
:!!}//))((" 
 ^	 1 1 7 77 $ r'   	type_hintc                 ^    U [         [        4;   =(       d    [        U 5      [         [        4;   $ )z2Helper to check if a type is Required/NotRequired.)r   r   r   )r   s    r%   r   r     s'    ;//eJy4IhXcMd4der'   )   
   )r&   r   r   Validator_Tr   r   r   r$   )Hcollections.abcr   rx   systypesdataclassesr   r   r   r   r   r   	functoolsr	   r
   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   r   r   r   rq   r   objectr   r&   r   r   r   r   rm   r   r   r   r   r   r   r   r   r   r   r   r   rj   rn   r   r   r   r   r   version_info	UnionType__all__r   r'   r%   <module>r      s     
  T T &    
2,  ud{#CLtCH~>"H  
 (Q (DG ( 
 ( 
$) OT OhQy$q'7I.J O 
 O "e2<Ae2	$q'	e259e2
47Hd1gYQ/001e2P/] 3 /4 /D /d ;d=.A ;d ; ;<
](; 
S#X 
$ *1?F#(T+&34(3%&( 8BG,m;<( 	(
 ( 4.( ( tn( ( 	(V+ DQ QS Q Q Q2# c sCx T OC O O5c? Ot OR RS Rc3h RD RM MS Mc3h MD MW# Wc WsCx WT W.D DC DuS#X D4 DVS V VE#s(O V V$
 
C 
 
 
% K S T 6f3 f4 f 
?..	?OO0  w.=5??+s  22;; 2
B+="b12	2s*   I
 
I?II;6I?:I;;I?