o
    ԰iX)                     @   s2  d dl Z d dlZd dlZd dlmZ zd dlmZ W n ey)   d dlmZ Y nw z
d dlmZm	Z	 W n! eyU   z
d dlmZm	Z	 W n eyR   d Z	ZY nw Y nw dd Zz	ej
ddd	 W n eyp   d
d ZY nw dd Zejdkrdd Znejdk rdd ZnejZejdkrd dlmZ ndd Zdd Zdd Zdd Zi eeeeeeeeeeejeejeejeejeejeejeej eej!eej"eej#eej$eej%ee j&jee j&j ee j&j!ee j&j$ee j&jee j&j"ee j&j#eiZ'dd Z(dd Z)d d! Z*d"d# Z+dS )$    N)_AnnotatedAlias)get_type_hints)NotRequiredRequiredc                 C   s   t | ddS )NT)include_extras)_get_type_hints)obj r	   B/home/ubuntu/.local/lib/python3.10/site-packages/msgspec/_utils.pyr      s   r   FooT)is_classc                 C   s   t j| ddS )NF)is_argumenttyping
ForwardRefvaluer	   r	   r
   _forward_ref!      r   c                 C   s   t j| dddS )NFT)r   r   r   r   r	   r	   r
   r   &      )      c                 C   s   t | ||dS )Nr	   )r   
_eval_type)tglobalnslocalnsr	   r	   r
   r   -   r   r   )r   
   c                 C   sz   zt | ||W S  ty< } z(zddlm} W n ty)   td| jd|w || ||ddW  Y d }~S d }~ww )Nr   )eval_type_backportz#Unable to evaluate type annotation a  . If you are making use of the new typing syntax (unions using `|` since Python 3.10 or builtins subscripting since Python 3.9), you should either replace the use of new syntax with the existing `typing` constructs or install the `eval_type_backport` package.F)try_default)r   r   	TypeErrorr   ImportError__forward_arg__)r   r   r   er   r	   r	   r
   r   1   s(   )r      )get_annotationsc                 C   s   | j di S )N__annotations__)__dict__get)clsr	   r	   r
   _get_class_annotationsM   r   r)   c                    sd   t | tjr | | S zt| j}W n ty   |  Y S w |s#| S t fdd|D }| | S )Nc                 3   s    | ]	}  ||V  qd S N)r'   ).0pmappingr	   r
   	<genexpr>`   s    z _apply_params.<locals>.<genexpr>)
isinstancer   TypeVarr'   tuple__parameters__	Exception)r   r.   
parametersargsr	   r-   r
   _apply_paramsQ   s   r7   c                    s<   i t | tr
| }n| j} fdd  | i  |jfS )Nc                    s   t | tr
| }i }n8t| dd }|d ttjfv s|v rd S |j}t fdd| jD }t	|t	|ks7J t
t|| |< }t|tjrYt|d|j}|D ]	}|| qQd S d S )N
__origin__c                 3   s    | ]}t | V  qd S r*   )r7   )r+   ascoper	   r
   r/   u   s    zE_get_class_mro_and_typevar_mappings.<locals>.inner.<locals>.<genexpr>__orig_bases__)r0   typegetattrobjectr   Genericr3   r2   __args__lendictzip
issubclass	__bases__)cr;   r(   	new_scopeparamsr6   basesbinnerr.   r:   r
   rM   l   s    
z2_get_class_mro_and_typevar_mappings.<locals>.inner)r0   r=   r8   __mro__)r   r(   r	   rL   r
   #_get_class_mro_and_typevar_mappingsd   s   


rO   c                 C   s   i }t | \}}|D ]W}|tjtfv rq
||}tt|}ttj	|j
ddi }t|}| D ]-\}	}
|	|v r<q3t|
trEt|
}
t|
||}
|durTt|
|}
|
du r\td}
|
||	< q3q
|S )a  Get the annotations for a class.

    This is similar to ``typing.get_type_hints``, except:

    - We maintain it
    - It leaves extras like ``Annotated``/``ClassVar`` alone
    - It resolves any parametrized generics in the class mro. The returned
      mapping may still include ``TypeVar`` values, but those should be treated
      as their unparametrized variants (i.e. equal to ``Any`` for the common case).

    Note that this function doesn't check that Generic types are being used
    properly - invalid uses of `Generic` may slip through without complaint.

    The assumption here is that the user is making use of a static analysis
    tool like ``mypy``/``pyright`` already, which would catch misuse of these
    APIs.
    Nr&   )rO   r   r@   r?   r'   rC   varsr>   sysmodules
__module__r)   itemsr0   strr   r   r7   r=   )r   hintsmrotypevar_mappingsr(   r.   
cls_localscls_globalsannnamer   r	   r	   r
   get_class_annotations   s,   



r]   c                    s   t | tr| }n| j}t| }t|drt|j}n|jr"t|}nt }i  | D ]0\}}t	|dd}|t
u rF|| |jd  |< q+|tu rW|| |jd  |< q+| |< q+t fdd|D sstd| d    |fS )	N__required_keys__r8   Fr   c                 3   s    | ]}| v V  qd S r*   r	   )r+   krV   r	   r
   r/      s    z%get_typeddict_info.<locals>.<genexpr>zRequired set z% contains keys that are no in hints: )r0   r=   r8   r]   hasattrsetr^   	__total__rT   r>   r   addrA   r   discardallRuntimeErrorkeys)r   r(   	raw_hintsrequiredr_   voriginr	   r`   r
   get_typeddict_info   s0   





rm   c                 C   s  t | tr| }n| j}t| }g }g }g }t|drddlm}m}m} |j	
 D ]H}	|	j|ur:|	j|u r9tdq)|	j}
||
 }|	j|urU||	j ||
|df q)|	j|uri||	j ||
|df q)||
|df q)|| d }t|dd }nqddlm}m} g }|jD ]K}	|	j}
||
 }|	j}||urt ||r|jrtd	||j ||
|df n|| ||
|df n||
|df |	jd ur||	 q|| t|d
d }t|dd }|rt||}|t|t|||fS )N__dataclass_fields__r   )_FIELD_FIELD_INITVARMISSINGz3dataclasses with `InitVar` fields are not supportedFT__post_init__)NOTHINGFactoryzvSupport for default factories with `takes_self=True` is not implemented. File a GitHub issue if you need this feature!__attrs_pre_init____attrs_post_init__)r0   r=   r8   r]   ra   dataclassesro   rp   rq   rn   values_field_typer   r\   defaultappenddefault_factoryextendr>   attrsrs   rt   __attrs_attrs__
takes_selfNotImplementedErrorfactory	validator_wrap_attrs_validatorsr2   )r   r(   rV   rj   optionaldefaultsro   rp   rq   fieldr\   typpre_init	post_initrs   rt   fields_with_validatorsrz   r	   r	   r
   get_dataclass_info   sl   













r   c                    s    fdd}|S )Nc                    s8    D ]}| | |t| |j qd ur|  d S d S r*   )r   r>   r\   )r   r   fieldsr   r	   r
   rM   B  s
   z%_wrap_attrs_validators.<locals>.innerr	   )r   r   rM   r	   r   r
   r   A  s   r   c                 C   s   | di |S )z1Used to unpickle Structs with keyword-only fieldsNr	   r	   )r(   kwargsr	   r	   r
   rebuildK  s   r   ),collectionsrQ   r   r   typing_extensionsr   r   r4   r   r   r   r   r   version_infor   annotationlibr$   r)   r7   rO   r]   listr2   rb   	frozensetrC   ListTupleSet	FrozenSetDict
CollectionMutableSequenceSequenceMutableMappingMapping
MutableSetAbstractSetabc_CONCRETE_TYPESrm   r   r   r   r	   r	   r	   r
   <module>   s   




/	
*J
