U
    ad5                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZdddddddd	d
dddgZ	G dd de
ZG dd dZe ZG dd dZe Zei ZG dd dZedZedZedZdZdZdZe dZG dd deZG dd dedZG d d dZG d!d" d"Zeed#d#dd#dd$d%dZ d&d' Z!d(d) Z"dded*d+d,Z#d-d. Z$d/d0 Z%d1d2 Z&d3d4 Z'd5d6 Z(d7d8 Z)d9d: Z*d;d< Z+d=d> Z,d?d@ Z-dAdB Z.dCdD Z/dEdF Z0dGdH Z1dIdJ Z2dKdL Z3dddde1de2de2e3e2e3e2e3e2e3dMZ4dNdO Z5ddd#d#d#dPdPdPdQdRdZ6dSd Z7dTdU Z8dVd Z9e:dWdXd	Z;dYdZ Z<e=d[d\d
Z>d]d^ Z?d_dd#d#d#dPdPdPd`dadZ@dbd ZAdceA_BdS )e    N	dataclassfieldFieldFrozenInstanceErrorInitVarMISSINGfieldsasdictastuplemake_dataclassreplaceis_dataclassc                   @   s   e Zd ZdS )r   N__name__
__module____qualname__ r   r   !/usr/lib/python3.8/dataclasses.pyr      s    c                   @   s   e Zd Zdd ZdS )_HAS_DEFAULT_FACTORY_CLASSc                 C   s   dS )Nz	<factory>r   selfr   r   r   __repr__   s    z#_HAS_DEFAULT_FACTORY_CLASS.__repr__N)r   r   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )_MISSING_TYPENr   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd Zdd ZdS )_FIELD_BASEc                 C   s
   || _ d S Nnamer   r   r   r   r   __init__   s    z_FIELD_BASE.__init__c                 C   s   | j S r   r   r   r   r   r   r      s    z_FIELD_BASE.__repr__N)r   r   r   r   r   r   r   r   r   r      s   r   _FIELD_FIELD_CLASSVAR_FIELD_INITVARZ__dataclass_fields__Z__dataclass_params__Z__post_init__z^(?:\s*(\w+)\s*\.)?\s*(\w+)c                   @   s   e Zd Zdd ZdS )_InitVarMetac                 C   s   t |S r   )r   )r   Zparamsr   r   r   __getitem__   s    z_InitVarMeta.__getitem__N)r   r   r   r#   r   r   r   r   r"      s   r"   c                   @   s    e Zd ZdZdd Zdd ZdS )r   typec                 C   s
   || _ d S r   r$   )r   r%   r   r   r   r      s    zInitVar.__init__c                 C   s,   t | jtr| jj}n
t| j}d| dS )Nzdataclasses.InitVar[])
isinstancer%   r   repr)r   Z	type_namer   r   r   r      s    

zInitVar.__repr__Nr   r   r   	__slots__r   r   r   r   r   r   r      s   )	metaclassc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   )
r   r%   defaultdefault_factoryr(   hashinitcomparemetadata_field_typec                 C   sR   d | _ d | _|| _|| _|| _|| _|| _|| _|d kr<tnt	
|| _d | _d S r   )r   r%   r,   r-   r/   r(   r.   r0   _EMPTY_METADATAtypesMappingProxyTyper1   r2   )r   r,   r-   r/   r(   r.   r0   r1   r   r   r   r      s    zField.__init__c                 C   sV   d| j d| jd| jd| jd| jd| jd| jd| jd	| jd
| j	 dS )NzField(name=z,type=z	,default=z,default_factory=z,init=,repr=z,hash=z	,compare=z
,metadata=z,_field_type=))
r   r%   r,   r-   r/   r(   r.   r0   r1   r2   r   r   r   r   r     s    zField.__repr__c                 C   s(   t t| jdd }|r$|| j|| d S )N__set_name__)getattrr%   r,   )r   ownerr   funcr   r   r   r8     s    zField.__set_name__N)r   r   r   r*   r   r   r8   r   r   r   r   r      s   c                   @   s    e Zd ZdZdd Zdd ZdS )_DataclassParamsr/   r(   eqorderunsafe_hashfrozenc                 C   s(   || _ || _|| _|| _|| _|| _d S r   r=   )r   r/   r(   r>   r?   r@   rA   r   r   r   r   *  s    z_DataclassParams.__init__c                 C   s6   d| j d| jd| jd| jd| jd| jdS )Nz_DataclassParams(init=r6   z,eq=z,order=z,unsafe_hash=z,frozen=r7   r=   r   r   r   r   r   2  s    z_DataclassParams.__repr__Nr)   r   r   r   r   r<   !  s   r<   Tr,   r-   r/   r(   r.   r0   r1   c                 C   s,   | t k	r|t k	rtdt| ||||||S )a  Return an object to identify dataclass fields.

    default is the default value of the field.  default_factory is a
    0-argument function called to initialize a field's value.  If init
    is True, the field will be a parameter to the class's __init__()
    function.  If repr is True, the field will be included in the
    object's repr().  If hash is True, the field will be included in
    the object's hash().  If compare is True, the field will be used
    in comparison functions.  metadata, if specified, must be a
    mapping which is stored but not otherwise examined by dataclass.

    It is an error to specify both default and default_factory.
    z/cannot specify both default and default_factory)r   
ValueErrorr   rB   r   r   r   r   @  s
    c                    s(   |sdS dd  fdd|D  dS )N()(,c                    s   g | ]}  d |j  qS ).r   .0fobj_namer   r   
<listcomp>_  s     z_tuple_str.<locals>.<listcomp>,))join)rL   r   r   rK   r   
_tuple_strV  s    rP   c                    s"   t   t fdd}|S )Nc              	      sD   t | t f}| krdS  | z| }W 5  | X |S )Nz...)id_thread	get_identadddiscard)r   keyresultrepr_runninguser_functionr   r   wrapperi  s    
z _recursive_repr.<locals>.wrapper)set	functoolswraps)rZ   r[   r   rX   r   _recursive_reprd  s    
r_   )globalslocalsreturn_typec          
      C   s   |d kri }d|krt |d< d}|tk	r4||d< d}d|}ddd |D }d	|  d
| d| d| }d| }d| d| d|  }i }	t|||	 |	d f |S )NZBUILTINS Z_return_typez->_return_typerF   
c                 s   s   | ]}d | V  qdS )z  Nr   )rI   br   r   r   	<genexpr>  s     z_create_fn.<locals>.<genexpr>z def rE   r7   z:
, zdef __create_fn__(z):
z	
 return Z__create_fn__)builtinsr   rO   keysexec)
r   argsZbodyr`   ra   rb   Zreturn_annotationZtxtZ
local_varsnsr   r   r   
_create_fnw  s     
rm   c                 C   s0   | rd| d|d| dS | d| d| S )NzBUILTINS.object.__setattr__(rF   r7   rG   =r   )rA   r   value	self_namer   r   r   _field_assign  s    rq   c                 C   s   d| j  }| jtk	rV| jr@| j||< | d| j  d| j  }q| j||< | d}n8| jr| jtkrn| j }q| jtk	r| j||< | j }nd S | jtkrd S t|| j ||S )NZ_dflt_z() if z is _HAS_DEFAULT_FACTORY else rD   )r   r-   r   r/   r,   r2   r!   rq   )rJ   rA   r`   rp   Zdefault_namero   r   r   r   _field_init  s"    






rr   c                 C   sV   | j tkr| jtkrd}n&| j tk	r2d| j }n| jtk	r@d}| j d| j | S )Nrc   z=_dflt_z=_HAS_DEFAULT_FACTORYz:_type_)r,   r   r-   r   )rJ   r,   r   r   r   _init_param  s    

rs   c                 C   s   d}| D ]:}|j r|jtkr&|jtks,d}q|rtd|jdqdd | D }|ttd g }| D ] }t||||}	|	rj|	|	 qj|rd
d	d
 | D }
|	| dt d|
 d |sdg}td|gdd | D  |||d dS )NFTznon-default argument z follows default argumentc                 S   s   i | ]}d |j  |jqS )Z_type_)r   r%   rH   r   r   r   
<dictcomp>  s     
 z_init_fn.<locals>.<dictcomp>)r   _HAS_DEFAULT_FACTORYrF   c                 s   s   | ]}|j tkr|jV  qd S r   )r2   r!   r   rH   r   r   r   rf   	  s    
z_init_fn.<locals>.<genexpr>rG   rE   r7   passr   c                 S   s   g | ]}|j rt|qS r   )r/   rs   rH   r   r   r   rM     s      z_init_fn.<locals>.<listcomp>)ra   r`   rb   )r/   r,   r   r-   	TypeErrorr   updateru   rr   appendrO   _POST_INIT_NAMErm   )r   rA   has_post_initrp   r`   Zseen_defaultrJ   ra   Z
body_lineslineZ
params_strr   r   r   _init_fn  s:    r}   c                 C   s2   t dddddd | D  d g|d}t|S )	Nr   r   z(return self.__class__.__qualname__ + f"(rg   c                 S   s    g | ]}|j  d |j  dqS )z={self.z!r}r   rH   r   r   r   rM     s   z_repr_fn.<locals>.<listcomp>z)"r`   )rm   rO   r_   )r   r`   fnr   r   r   _repr_fn  s    
r   c                 C   sp   | t d}|r,dddd |D  d }nd}tdd	d
| dddf||dtddd
| dddf||dfS )N)clsr   rE   rF   c                 s   s   | ]}t |jV  qd S r   )r(   r   rH   r   r   r   rf   (  s     z'_frozen_get_del_attr.<locals>.<genexpr>rN   rD   __setattr__)r   r   ro   z if type(self) is cls or name in :z> raise FrozenInstanceError(f"cannot assign to field {name!r}")z)super(cls, self).__setattr__(name, value))ra   r`   __delattr__r   z; raise FrozenInstanceError(f"cannot delete field {name!r}")z"super(cls, self).__delattr__(name))r   rO   rm   )r   r   r`   ra   Z
fields_strr   r   r   _frozen_get_del_attr$  s2    

r   c                 C   s$   t | ddd| | | dg|dS )N)r   otherz%if other.__class__ is self.__class__:z return zreturn NotImplementedr~   )rm   )r   op
self_tupleother_tupler`   r   r   r   _cmp_fn=  s    r   c                 C   s$   t d| }tddd| dg|dS )Nr   __hash__r   zreturn hash(r7   r~   )rP   rm   )r   r`   r   r   r   r   _hash_fnK  s    
r   c                 C   s$   | |j kp"t| |jko"| j|j kS r   )ClassVarr%   Z_GenericAliasZ
__origin__)a_typetypingr   r   r   _is_classvarS  s    

r   c                 C   s   | |j kpt| |j kS r   )r   r%   )r   dataclassesr   r   r   _is_initvar[  s    
r   c           	      C   s   t | }|rd }|d}|s2tj|jj}n2tj|j}|rd|j||krdtj|jj}|r|||d|rdS dS )N      TF)_MODULE_IDENTIFIER_REmatchgroupsysmodulesgetr   __dict__)	Z
annotationr   Za_moduler   Zis_type_predicater   rl   Zmodule_namemoduler   r   r   _is_typeb  s    )

r   c                 C   s8  t | |t}t|tr|}nt|tjr,t}t|d}||_||_t	|_
tjd}|rt||st|jtrt|j| ||jtrt|_
|j
t	krtjt }t||st|jtrt|j| ||jtrt|_
|j
ttfkr|jtk	rtd|j d|j
t	kr4t|jtttfr4tdt|j d|j d|S )N)r,   r   field z cannot have a default factoryzmutable default z for field z$ is not allowed: use default_factory)r9   r   r'   r   r4   MemberDescriptorTyper   r   r%   r   r2   r   r   r   r   strr   r   r    r   r   r   r!   r-   rw   r,   listdictr\   rC   )r   Za_namer   r,   rJ   r   r   r   r   r   
_get_field  sF    








	 r   c                 C   s   || j krdS t| || dS )NTF)r   setattr)r   r   ro   r   r   r   _set_new_attribute  s    
r   c                 C   s   d S r   r   r   r   r`   r   r   r   _hash_set_none  s    r   c                 C   s   dd |D }t ||S )Nc                 S   s(   g | ] }|j d kr|jrn|j r|qS r   )r.   r0   rH   r   r   r   rM     s
     
   z_hash_add.<locals>.<listcomp>)r   )r   r   r`   fldsr   r   r   	_hash_add   s    r   c                 C   s   t d| j d S )Nz-Cannot overwrite attribute __hash__ in class )rw   r   r   r   r   r   _hash_exception  s    r   ))FFFF)FFFT)FFTF)FFTT)FTFF)FTFT)FTTF)FTTT)TFFF)TFFT)TFTF)TFTT)TTFF)TTFT)TTTF)TTTTc                    s  i } j tjkr tj j  j}ni }t tt|||||| d}	d}
 jddd D ]D}t|t	d }|d k	rVd}
|
 D ]}|||j< qzt|tjrVd}	qV jdi } fdd| D }|D ]L}|||j< tt |jd tr|jtkr t |j qt |j|j qĈ j D ].\}}t|tr||krt|dq|
rz|	rf|sftd	|	sz|rztd
t t	|  jdt}|tkp|d kod jk }|r|std|rt t}dd |
 D }t dt|||d|krdnd| dd |
 D }|rHdd |D }t dt|| |rdd |D }td|}td|}t dtdd|||d |rdd |D }td|}td|}dD ]>\}}t |t|||||drtd| d j dq|r8t ||D ].}t |j|rtd|j d j qtt |t |t ||f }|rh| || _!t ds jt"t#$ %dd   _& S )!NFr   T__annotations__c                    s   g | ]\}}t  ||qS r   )r   )rI   r   r%   r   r   r   rM   ]  s   z"_process_class.<locals>.<listcomp>z& is a field but has no type annotationz5cannot inherit non-frozen dataclass from a frozen onez5cannot inherit frozen dataclass from a non-frozen oner   __eq__z eq must be true if order is truec                 S   s   g | ]}|j ttfkr|qS r   )r2   r   r!   rH   r   r   r   rM     s    r   r   Z__dataclass_self__c                 S   s   g | ]}|j tkr|qS r   r2   r   rH   r   r   r   rM     s     
 c                 S   s   g | ]}|j r|qS r   )r(   rH   r   r   r   rM     s      r   c                 S   s   g | ]}|j r|qS r   r0   rH   r   r   r   rM     s      r   z==r~   c                 S   s   g | ]}|j r|qS r   r   rH   r   r   r   rM     s      ))__lt__<)__le__z<=)__gt__>)__ge__z>=zCannot overwrite attribute z
 in class z). Consider using functools.total_ordering__doc__z -> Nonerc   )'r   r   r   r   r   _PARAMSr<   __mro__r9   _FIELDSvaluesr   rA   r   itemsr'   r   r,   r   delattrrw   rC   hasattrrz   r   r}   r   rP   r   r   r   _hash_actionboolr   r   inspectZ	signaturer   r   )r   r/   r(   r>   r?   r@   rA   r   r`   Zany_frozen_baseZhas_dataclass_basesre   Zbase_fieldsrJ   Zcls_annotationsZ
cls_fieldsr   ro   Z
class_hashZhas_explicit_hashr{   r   Z
field_listr   r   r   r   Zhash_actionr   r   r   _process_class'  s     





 


r   Fr=   c                  s*    fdd}| dkr"|S || S )a  Returns the same class as was passed in, with dunder methods
    added based on the fields defined in the class.

    Examines PEP 526 __annotations__ to determine fields.

    If init is true, an __init__() method is added to the class. If
    repr is true, a __repr__() method is added. If order is true, rich
    comparison dunder methods are added. If unsafe_hash is true, a
    __hash__() method function is added. If frozen is true, fields may
    not be assigned to after instance creation.
    c                    s   t |  S r   )r   r   r>   rA   r/   r?   r(   r@   r   r   wrap  s    zdataclass.<locals>.wrapNr   )r   r/   r(   r>   r?   r@   rA   r   r   r   r   r     s    c                 C   sB   zt | t}W n tk
r*   tdY nX tdd | D S )zReturn a tuple describing the fields of this dataclass.

    Accepts a dataclass or an instance of one. Tuple elements are of
    type Field.
    z0must be called with a dataclass type or instancec                 s   s   | ]}|j tkr|V  qd S r   r   rH   r   r   r   rf     s     
 zfields.<locals>.<genexpr>)r9   r   AttributeErrorrw   tupler   )Zclass_or_instancer   r   r   r   r     s
    c                 C   s   t t| tS )z2Returns True if obj is an instance of a dataclass.)r   r%   r   )objr   r   r   _is_dataclass_instance  s    r   c                 C   s    t | tr| nt| }t|tS )zEReturns True if obj is a dataclass or an instance of a
    dataclass.)r'   r%   r   r   )r   r   r   r   r   r     s    dict_factoryc                C   s   t | stdt| |S )a  Return the fields of a dataclass instance as a new dictionary mapping
    field names to field values.

    Example usage:

      @dataclass
      class C:
          x: int
          y: int

      c = C(1, 2)
      assert asdict(c) == {'x': 1, 'y': 2}

    If given, 'dict_factory' will be used instead of built-in dict.
    The function applies recursively to field values that are
    dataclass instances. This will also look into built-in containers:
    tuples, lists, and dicts.
    z0asdict() should be called on dataclass instances)r   rw   _asdict_inner)r   r   r   r   r   r	     s    c                    s   t | rDg }t| D ]&}tt| |j }||j|f q |S t| trrt| drrt	|  fdd| D  S t| t
tfrt	|  fdd| D S t| trt	|  fdd|  D S t| S d S )N_fieldsc                    s   g | ]}t | qS r   r   rI   vr   r   r   rM   O  s     z!_asdict_inner.<locals>.<listcomp>c                 3   s   | ]}t | V  qd S r   r   r   r   r   r   rf   T  s     z _asdict_inner.<locals>.<genexpr>c                 3   s&   | ]\}}t | t | fV  qd S r   r   rI   kr   r   r   r   rf   V  s   )r   r   r   r9   r   ry   r'   r   r   r%   r   r   r   copydeepcopy)r   r   rW   rJ   ro   r   r   r   r   4  s    
r   tuple_factoryc                C   s   t | stdt| |S )a  Return the fields of a dataclass instance as a new tuple of field values.

    Example usage::

      @dataclass
      class C:
          x: int
          y: int

    c = C(1, 2)
    assert astuple(c) == (1, 2)

    If given, 'tuple_factory' will be used instead of built-in tuple.
    The function applies recursively to field values that are
    dataclass instances. This will also look into built-in containers:
    tuples, lists, and dicts.
    z1astuple() should be called on dataclass instances)r   rw   _astuple_inner)r   r   r   r   r   r
   ]  s    c                    s   t | r>g }t| D ] }tt| |j }|| q |S t| trlt| drlt	|  fdd| D  S t| t
tfrt	|  fdd| D S t| trt	|  fdd|  D S t| S d S )Nr   c                    s   g | ]}t | qS r   r   r   r   r   r   rM     s     z"_astuple_inner.<locals>.<listcomp>c                 3   s   | ]}t | V  qd S r   r   r   r   r   r   rf     s     z!_astuple_inner.<locals>.<genexpr>c                 3   s&   | ]\}}t | t | fV  qd S r   r   r   r   r   r   rf     s   )r   r   r   r9   r   ry   r'   r   r   r%   r   r   r   r   r   )r   r   rW   rJ   ro   r   r   r   r   u  s    
r   r   )bases	namespacer/   r(   r>   r?   r@   rA   c             	      s   dkri  n    t }
i }|D ]}t|tr<|}d}nDt|dkrR|\}}n.t|dkrr|\}}}| |< ntd|t|tr| std|t|rtd|||
krtd||
	| |||< q$| d	< t
| |i  fd
d}t|||||||	dS )a  Return a new dynamically created dataclass.

    The dataclass name will be 'cls_name'.  'fields' is an iterable
    of either (name), (name, type) or (name, type, Field) objects. If type is
    omitted, use the string 'typing.Any'.  Field objects are created by
    the equivalent of calling 'field(name, type [, Field-info])'.

      C = make_dataclass('C', ['x', ('y', int), ('z', int, field(init=False))], bases=(Base,))

    is equivalent to:

      @dataclass
      class C(Base):
          x: 'typing.Any'
          y: int
          z: int = field(init=False)

    For the bases and namespace parameters, see the builtin type() function.

    The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to
    dataclass().
    Nz
typing.Anyr      zInvalid field: z'Field names must be valid identifiers: z"Field names must not be keywords: zField name duplicated: r   c                    s
   |   S r   )rx   )rl   r   r   r   <lambda>      z make_dataclass.<locals>.<lambda>r=   )r   r\   r'   r   lenrw   isidentifierkeyword	iskeywordrT   r4   	new_classr   )Zcls_namer   r   r   r/   r(   r>   r?   r@   rA   seenZannsitemr   tpspecr   r   r   r   r     s:    






 c                  O   s  t | dkr tdt |  d| r,| \}n4d|krX|d}ddl}|jdtdd	 ntd
t|sptdt|t	 D ]v}|j
tkrq~|js|j|kr~td|j dq~|j|kr~|j
tkr|jtkrtd|jdt||j||j< q~|jf |S )a,  Return a new object replacing specified fields with new values.

    This is especially useful for frozen classes.  Example usage:

      @dataclass(frozen=True)
      class C:
          x: int
          y: int

      c = C(1, 2)
      c1 = replace(c, x=3)
      assert c1.x == 3 and c1.y == 2
      r   z*replace() takes 1 positional argument but z were givenr   r   Nz/Passing 'obj' as keyword argument is deprecatedr   )
stacklevelz7replace() missing 1 required positional argument: 'obj'z1replace() should be called on dataclass instancesr   zC is declared with init=False, it cannot be specified with replace()zInitVar z! must be specified with replace())r   rw   popwarningswarnDeprecationWarningr   r9   r   r   r2   r    r/   r   rC   r!   r,   r   	__class__)rk   Zchangesr   r   rJ   r   r   r   r     s4    
 


z(obj, /, **kwargs))N)Crer   r   r4   r   r   rh   r]   rR   __all__r   r   r   ru   r   r   r5   r3   r   r   r    r!   r   r   rz   compiler   r%   r"   r   r   r<   r   rP   r_   rm   rq   rr   rs   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r   r
   r   r   r   __text_signature__r   r   r   r   <module>   s    

:  62;R >
 )   B<