
    3jTe                        % S SK rS SKrS SKrS SK Jr  S SK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   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\*S\\#   \\\#   /\\#   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r-\\SSSSS4S\.\"   \"-  S\S\S \*S!\*S"\*S-  S#\*S$\$S-  S%\S\4S& jjr/S\"4S' jr0S(\%S)\S*\SS4S+ jr1S(\%S)\SS4S, jr2S(\%S)\S-\3\S.4   SS4S/ jr4S(\%S)\S-\3\S.4   SS4S0 jr5S(\%S)\S-\3\S.4   SS4S1 jr6S(\%S)\S-\3\S.4   SS4S2 jr7S(\%S)\S-\3\S.4   SS4S3 jr8S(\%S)\S-\3\S.4   SS4S4 jr9S(\%S)\S-\3\S.4   SS4S5 jr:S(\%S)\S*\SS4S6 jr;S7\S\"4S8 jr<S\S\*4S9 jr=S:\S\*4S; jr>\\4\\5\.\6\$\7\3\8\?\9\R                  R                  \:0rB\$\\\%\\3\S.4   /S4   4   \CS<'   \4\B\R                  '   / S=QrEg! \ 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)Callable)MISSINGFieldfieldfieldsmake_dataclass)	lru_cachewraps)
	AnnotatedAny
ForwardRefLiteralTypeTypeVarUnionget_args
get_originoverload)NotRequiredRequiredr    r      )#StrictDataclassClassValidationErrorStrictDataclassDefinitionError#StrictDataclassFieldValidationErrorTTypedDictType)boundclsreturnc                     g Nr   )r   s    U/home/wildlama/miniconda3/lib/python3.13/site-packages/huggingface_hub/dataclasses.pystrictr$   0   s    %(    Faccept_kwargsr'   c                     g r"   r   r&   s    r#   r$   r$   4   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   )r.   r/   r0   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   r/   itemslenr5   __name__setattrdefaultr   default_factoryr6   __post_init__)r.   argsr;   fdataclass_fieldskvstandard_kwargsadditional_kwargsr/   r0   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rD   c                 N    UR                  5        H  u  p#[        XU5        M     g)z2Default __post_init__ to accept additional kwargs.N)r>   rA   )r.   r;   r/   r0   s       r#   rD   +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)r.   standard_reprrH   rI   rK   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,   r2   N)__class_validators__r5   r6   r   r@   )r.   r,   r7   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	r_   "__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)r_   )r.   rE   r;   r   initial_inits      r#   init_with_validate0strict.<locals>.wrap.<locals>.init_with_validate  s#     ///LLr%   rL   )r    N)$hasattrr   r@   r   append_create_type_validatormetadatar4   
isinstancelist_is_validatorr/   extendr3   __setattr__r   strrL   r
   rD   rZ   __dataclass_params__reprdir
startswithgetattrcallabler?   inspect	signature
parametersr^   r   ra   r_   rA   )r   field_validatorsrF   
validatorscustom_validatorr,   r9   original_initrL   rD   rZ   class_validatorsr/   methodr_   rd   rc   rY   r8   r'   s   `               @@@r#   wrapstrict.<locals>.wrapp   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$   8   s8    p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   )rB   ri   )rB   )_get_typed_dict_annotationsrt   r>   r   r   r   _is_required_or_notrequiredr   tuplerk   rg   r   _TYPED_DICT_DEFAULT_VALUEr$   r   r@   )r   
type_hintskeyr0   originbasemetar   s           r#   r   r   P  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ImportErrorrU   r4   r>   type)r   r   r/   r0   s       r#   r   r   o  sz    
,,V44 

  &223DbIOOQ	
  R ,%$t*<Q	
 	

s    7A8A+*A87A8Tr,   rB   rC   initrq   hashcompareri   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,   rB   rC   r   rq   r   r   ri   r   )rj   rk   r   )	r,   rB   rC   r   rq   r   r   ri   r;   s	            r#   validated_fieldr     s\    4 i&&K	%[ 	'	 	 	r%   c                    ^  [         [         SSSSS4S[        S[        S[        S[        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.
TNrB   rC   r   rq   r   r   ri   r;   c                 ,   > [        T4U UUUUUUS.UD6$ )Nr   )r   )	rB   rC   r   rq   r   r   ri   r;   r,   s	           r#   _inner"as_validated_field.<locals>._inner  s9     

+

 

 
	
r%   )r   r   booldict)r,   r   s   ` r#   as_validated_fieldr     s     &  $


 
 	

 Tk
 
 +
 
 
, Mr%   r/   r0   expected_typec                    [        U5      n[        U5      nU[        L a  gUc  [        X5        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   _validate_none_BASIC_TYPE_VALIDATORSr4   rj   r   _validate_simple_typer   ro   r   r   r6   type_validatorr   )r/   r0   r   r   rE   r,   s         r#   r   r     s    &FM"D		t#,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%   c                 R    Ub$  [        SU  S[        U5      R                   35      eg)zValidate None type.

'None' is not a type, it's a special value. Type should be `NoneType` instead.
But in type annotations 'None' is accepted so we must support it.
Nr   z' expected None, got )r6   r   r@   )r/   r0   s     r#   r   r     s3     '$'<T%[=Q=Q<RSTT r%   rE   .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   r6   rg   ro   rq   rV   )r/   r0   rE   errorstr7   s         r#   _validate_unionr     s    F	"4*  
$}T%[M1LTFR\]a]f]fgm]n\op   	"MM#a&!!	"s   A
A7A22A7c                    [        U[        5      (       aB  X Vs/ s H  n[        U[        5      (       d  M  UPM     sn;  a  [        SU  SU SU 35      eg[        U[        5      (       aY  X Vs/ s H3  n[        U[        5      (       d  M  [        U[        5      (       a  M1  UPM5     sn;  a  [        SU  SU SU 35      egX;  a  [        SU  SU SU 35      egs  snf s  snf )zValidate Literal type.r   z' expected one of , got N)rj   r   r6   int)r/   r0   rE   args       r#   _validate_literalr      s    %F
30EFFgdV+=dV6%QRR G	E3		c
30DZX[]aMbccgdV+=dV6%QRR d		'$'9$veWMNN 
 G ds   C C 1C%C%%C%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)rj   rk   r6   r   r@   	enumerater   r/   r0   rE   	item_typeiitemr7   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)rj   r   r6   r   r@   r>   r   )r/   r0   rE   key_type
value_typerH   rI   r7   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   )
rj   r   r6   r   r@   r?   Ellipsisr   r   zip)r/   r0   rE   r   r   r7   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)rj   setr6   r   r@   r   r   r   s          r#   _validate_setr   @  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=   )	rj   collectionsabcSequencer6   r   r@   r   r   r   s          r#   _validate_sequencer   N  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   U[         L aS  [        U[        5      (       a>  [        SU  SUR                   S[        U5      R                   S[        U5       S3	5      e[        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: rT   N)r   rj   r   r6   r@   r   rq   )r/   r0   r   s      r#   r   r   `  s    
5$ 7 7dV;}'='=&>fT%[EYEYDZZcdhindocppqr
 	
 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.r0   r    Nc                 H   > [        TR                  U TR                  5        g r"   )r   r/   r   )r0   r   s    r#   r,   )_create_type_validator.<locals>.validatorp  s    uzz5%**5r%   )r   )r   r,   s   ` r#   rh   rh   l  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)ru   rv   rw   rk   rx   valuesr?   kind	ParameterPOSITIONAL_OR_KEYWORDPOSITIONAL_ONLYVAR_POSITIONALrB   empty)r,   rw   rx   	parameters       r#   rl   rl   v  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   r   Validator_Tr   r   r   r"   )Fcollections.abcr   rv   typesr   dataclassesr   r   r   r   r   	functoolsr	   r
   typingr   r   r   r   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   r   r   r   r   r   r   ro   r   objectr   r$   r   r   r   r   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   rl   r   r   r   r   r   r   	UnionType__all__r   r%   r#   <module>r      s      $ E E &  
2,  ud{#CLtCH~>"H  
 (Q (DG ( 
 ( 
$) OT OhQy$q'7I.J O 
 Oc2 c2Q$ c2 c2$q'T\^bcd^e]fhlmnho]oTpJp c2L/] 3 /4 /D /d ;d=.A ;d ; ;<
](; 
S#X 
$ " (K ;.(( ( 	(
 ( +( ( Tk( ( 	(V+ DQ QS Q Q Q6U US UT U# 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R S(CeCHo+F+L"MMN  +: u 'Y  22;; 2
B+="b12	2s*   I I:II61I:5I66I: