o
    tBh^                     @  sb  U d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlm Z m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' dd	l(m)Z) dd
l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddhZ5de6d< dhZ7de6d< dZ8de6d< G dd dZ9G dd dZ:d&ddZ;d'ddZ<d(d#d$Z=d%S ))z-Plugin that provides support for dataclasses.    )annotations)Final)	ARG_NAMEDARG_NAMED_OPTARG_OPTARG_POSARG_STAR	ARG_STAR2MDEFArgumentAssignmentStmtCallExprContext
ExpressionJsonDictNameExprPlaceholderNodeRefExprSymbolTableNodeTempNode	TypeAliasTypeInfoTypeVarExprVar)ClassDefContextSemanticAnalyzerPluginInterface)_get_decorator_bool_argumentadd_attribute_to_class
add_methoddeserialize_and_fixup_type)make_wildcard_trigger)state)map_type_from_supertype)
AnyTypeCallableTypeInstanceLiteralTypeNoneType	TupleTypeType	TypeOfAnyTypeVarTypeget_proper_type	dataclasszdataclasses.dataclassr   dataclass_makerszdataclasses.fieldfield_makers_DTSELF_TVAR_NAMEc                   @  sL   e Zd Zd%ddZd&ddZd'ddZd(ddZed)dd Zd*d"d#Z	d$S )+DataclassAttributenamestr
is_in_initboolis_init_varhas_defaultlineintcolumntypeType | Noneinfor   kw_onlyreturnNonec
           
      C  s:   || _ || _|| _|| _|| _|| _|| _|| _|	| _d S N	r3   r5   r7   r8   r9   r;   r<   r>   r?   )
selfr3   r5   r7   r8   r9   r;   r<   r>   r?    rE   o/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/mypy/plugins/dataclasses.py__init__@   s   
zDataclassAttribute.__init__r   c                 C  sN   t }| jr| jrt}n| jr| jst}n| js| jrt}t|  | jd |dS )N)variabletype_annotationinitializerkind)	r   r?   r8   r   r   r   r   to_varr<   )rD   arg_kindrE   rE   rF   to_argumentV   s   zDataclassAttribute.to_argumentr   c                 C  s   t | j| jS rB   )r   r3   r<   rD   rE   rE   rF   rL   b   s   zDataclassAttribute.to_varr   c              	   C  s4   | j sJ | j| j| j| j| j| j| j  | jdS )N)r3   r5   r7   r8   r9   r;   r<   r?   )	r<   r3   r5   r7   r8   r9   r;   	serializer?   rO   rE   rE   rF   rP   e   s   
zDataclassAttribute.serializedataapir   c                 C  sB   |  }|dd u rd|d< t|d|}| d||d|S )Nr?   Fr<   )r<   r>   rE   )copygetr   pop)clsr>   rQ   rR   typrE   rE   rF   deserializer   s
   zDataclassAttribute.deserializesub_typec                 C  s$   | j durt| j || j| _ dS dS )zoExpands type vars in the context of a subtype when an attribute is inherited
        from a generic super type.N)r<   r"   r>   )rD   rY   rE   rE   rF   expand_typevar_from_subtype|   s   
z.DataclassAttribute.expand_typevar_from_subtypeN)r3   r4   r5   r6   r7   r6   r8   r6   r9   r:   r;   r:   r<   r=   r>   r   r?   r6   r@   rA   )r@   r   )r@   r   )r@   r   )r>   r   rQ   r   rR   r   r@   r2   )rY   r   r@   rA   )
__name__
__module____qualname__rG   rN   rL   rP   classmethodrX   rZ   rE   rE   rE   rF   r2   ?   s    



	r2   c                   @  sn   e Zd ZdZd$ddZd%d	d
Zd&ddZd'ddZd(ddZd)ddZ		d*d+ddZ
d,dd Zd-d!d"Zd#S ).DataclassTransformera  Implement the behavior of @dataclass.

    Note that this may be executed multiple times on the same class, so
    everything here must be idempotent.

    This runs after the main semantic analysis pass, so you can assume that
    there are no placeholders.
    ctxr   r@   rA   c                 C  s
   || _ d S rB   )_ctx)rD   r`   rE   rE   rF   rG      s   
zDataclassTransformer.__init__r6   c              	     sF   j } j jj}  }|du rdS |D ]
}|jdu r dS qt j ddt j ddt j ddt j ddt j ddt j d	dd
} j jjj}|d rd|j	vs[|j	d j
r|r fdd|D }|jr|D ]
}|jtkrut|_qktd}	t|	ttjdtg|t|	ttjdt}t|d|t d |d r|ddu s|d r|jd}
tt|jd t g |
}tt||j	t< |d r'|d s|jd|j dD ]P}|jd}
t t|jd t dg |
}|jd}ttd||dtg}||}|dur|j
s|j!sJ |jd| d|j! t||||||d q|d r9 j"|dd  #| n "| |d rM j$|||dkd  %|| |d	 rd|j	vse|j	d j
r|r|dkr|jdfd d|D }t&||jd!}t'|j|jd|  (  d"d |D |d d#|j)d$< dS )%zApply all the necessary transformations to the underlying
        dataclass so as to ensure it is fully type checked according
        to the rules in PEP 557.
        NFinitTeqorderfrozenslots
match_args)rb   rc   rd   re   rf   rg   rG   c                   s&   g | ]}|j r |js| qS rE   )r5   _is_kw_only_typer<   rN   .0attrrO   rE   rF   
<listcomp>   s    
z2DataclassTransformer.transform.<locals>.<listcomp> )argsreturn_type__eq__zbuiltins.object.z eq must be True if order is True)__lt____gt____le____ge__zbuiltins.boolotherzYou may not have a custom z method when order=True)rn   ro   	self_typetvar_def)settable)   
   )correct_version__match_args__builtins.strc                   s   g | ]}|j rt|j qS rE   )r5   r&   r3   ri   )str_typerE   rF   rl     s
    
zbuiltins.tuplec                 S  s   g | ]}|  qS rE   )rP   ri   rE   rE   rF   rl     s    )
attributesre   r-   )*ra   rV   r>   collect_attributesr<   r   rR   optionspython_versionnamesplugin_generatedfallback_to_anyrK   r   r   r   r   r#   r*   explicitr   r	   r   r'   rT   
named_typer   r1   fullnamer   r
   failr+   node_propertize_callables_freeze	add_slotsreset_init_only_varsr(   r   %_add_dataclass_fields_magic_attributemetadata)rD   r`   r>   r   rk   decorator_arguments
py_versionrn   argnameless_varobj_typeself_tvar_exprmethod_nameorder_tvar_deforder_return_type
order_argsexisting_methodliteralsmatch_args_typerE   )rD   r   rF   	transform   s   







	



zDataclassTransformer.transformr>   r   r   list[DataclassAttribute]r}   c                C  sx   |s| j jd| j j d S dd |D }|jd ur|j|ks%|jdr7| j jd| j jj	| j j d S ||_d S )NzPKeyword argument "slots" for "dataclass" is only valid in Python 3.10 and higherc                 S  s   h | ]}|j qS rE   )r3   ri   rE   rE   rF   	<setcomp>'  s    z1DataclassTransformer.add_slots.<locals>.<setcomp>	__slots__z;"{}" both defines "__slots__" and is used with "slots=True")
ra   rR   r   reasonrf   r   rT   formatrV   r3   )rD   r>   r   r}   generated_slotsrE   rE   rF   r     s$   
zDataclassTransformer.add_slotsc                 C  s|   |D ]9}|j r;|j|jv r|j|j= n|j sJ |jjjD ]}t|tr:|jr:|j	d }t|t
r:|j|jkr:d|_qqdS )zERemove init-only vars from the class and reset init var declarations.r   N)r7   r3   r   defndefsbody
isinstancer   unanalyzed_typelvaluesr   r   )rD   r>   r   rk   stmtlvaluerE   rE   rF   r   9  s   

z)DataclassTransformer.reset_init_only_varslist[DataclassAttribute] | Nonec                 C  s  | j }| j j}g }t }t|dd}|jjD ]}t|tr |js!q|j	d }t|t
s,q|jj|j}|du r9q|j}	t|	trCJ t|	trP|jd|	 qt|	tsWJ |	jr[qd}
t|	j}t|tru|jjdkrud}
|jd |	_| |r|d}t|j|\}}|d}|du rd}nt|j|}d}|rd	|v pd
|v }nt|jt sd}|sd|_!|}|d}|durt|j|}|"|j |#t$|j||
||j%|j&|j|j|d	 q|' }|jj(dd D ]}d|j)v rd|j)vr dS d|j)vrqg }|j*t+|j |j)d d D ][}|d }||vrTt$,|||j}t-.|jj/j0 |1|jj W d   n	1 sCw   Y  |"| |#| q|ro|D ]}|j|krm|2| |#|  nqYq|| }|j3dd d qd}d}|D ]]}|r|j4r|j5s|j6s||v rt7|j%|j&dn|j}|jd| |p|j5o|j4}|r| |jr||v rt7|j%|j&dn|j}|jd| |p| |j}q|S )a@  Collect all attributes declared in the dataclass and its parents.

        All assignments of the form

          a: SomeType
          b: SomeOtherType = ...

        are collected.

        Return None if some dataclass base class hasn't been processed
        yet and thus we'll need to ask for another pass.
        r?   Fr   NzFType aliases inside dataclass definitions are not supported at runtimezdataclasses.InitVarTrb   defaultdefault_factoryrC      rv   dataclass_tagr-   r   r3   c                 S  s   | j S rB   )r?   )arE   rE   rF   <lambda>  s    z9DataclassTransformer.collect_attributes.<locals>.<lambda>)key)r9   r;   z>Attributes without a default cannot follow attributes with onez:There may not be more than one field with the KW_ONLY type)8ra   rV   setr   r   r   r   r   
new_syntaxr   r   r>   r   rT   r3   r   r   r   rR   r   r   is_classvarr,   r<   r%   r   rn   rh   _collect_field_argsrvaluer6   
parse_boolr   implicitaddappendr2   r9   r;   rS   mror   add_plugin_dependencyr    rX   r!   strict_optional_setr   strict_optionalrZ   removesortr5   r8   r?   r   )rD   r`   rV   attrsknown_attrsr?   r   lhssymr   r7   	node_typehas_field_call
field_argsis_in_init_paramr5   r8   
is_kw_onlyfield_kw_only_param	all_attrsr>   super_attrsrQ   r3   rk   found_defaultfound_kw_sentinelcontextrE   rE   rF   r   J  s   










  z'DataclassTransformer.collect_attributesc                 C  s   | j jj}|D ]7}|j|j}|dur"|j}t|tsJ d|_	q|
 }||_d|_	|jd |j |_tt||j|j< qdS )zaConverts all attributes to @property methods in order to
        emulate frozen classes.
        NTrq   )ra   rV   r>   r   rT   r3   r   r   r   is_propertyrL   r   	_fullnamer   r
   )rD   r   r>   rk   sym_nodevarrE   rE   rF   r     s   
zDataclassTransformer._freezeTrz   c                 C  sf   | j jj}|D ])}tt|jtr0| }||_d|_||_	|j
d |j |_tt||j|j< qdS )a  Converts all attributes with callable types to @property methods.

        This avoids the typechecker getting confused and thinking that
        `my_dataclass_instance.callable_attr(foo)` is going to receive a
        `self` argument (it is not).

        Trq   N)ra   rV   r>   r   r,   r<   r$   rL   r   is_settable_propertyr   r3   r   r   r
   r   )rD   r   rz   r>   rk   r   rE   rE   rF   r     s   

z*DataclassTransformer._propertize_callablesr   r=   c                 C  s.   |du rdS t |}t|tsdS |jjdkS )z=Checks if the type of the node is the KW_ONLY sentinel value.NFzdataclasses.KW_ONLY)r,   r   r%   r<   r   )rD   r   r   rE   rE   rF   rh   #  s   
z%DataclassTransformer._is_kw_only_typec                 C  s   d}t tj}| jjd|gp|}| jjd| jjd|g}t||d}| jjj	|_	| jjj	j
d | |_tt|dd| jjj	j|< d S )	N__dataclass_fields__zdataclasses.Fieldzbuiltins.dictr   )r3   r<   rq   T)rK   r   r   )r#   r*   r   ra   rR   named_type_or_noner   r   rV   r>   r   r   r   r
   r   )rD   	attr_nameany_type
field_type	attr_typer   rE   rE   rF   r   ,  s   
z:DataclassTransformer._add_dataclass_fields_magic_attributeNr`   r   r@   rA   )r@   r6   )r>   r   r   r   r}   r6   r@   rA   )r>   r   r   r   r@   rA   )r@   r   )r   r   r@   rA   )T)r   r   rz   r6   r@   rA   )r   r=   r@   r6   )r@   rA   )r[   r\   r]   __doc__rG   r   r   r   r   r   r   rh   r   rE   rE   rE   rF   r_      s    
	
 


 
4
	r_   r`   r   r@   rA   c                 C  s   i | j jjd< dS )zRecord that we have a dataclass in the main semantic analysis pass.

    The later pass implemented by DataclassTransformer will use this
    to detect dataclasses in base classes.
    r   N)rV   r>   r   )r`   rE   rE   rF   dataclass_tag_callback;  s   r   r6   c                 C  s   t | }| S )zIHooks into the class typechecking process to add support for dataclasses.)r_   r   )r`   transformerrE   rE   rF   dataclass_class_maker_callbackE  s   r   exprr   "tuple[bool, dict[str, Expression]]c                 C  s   t | trLt | jtrL| jjtv rLi }t| j| j| j	D ]+\}}}|
 s=|j
ddr.d}nd}|j||  di f  S |dusCJ |||< qd|fS di fS )zReturns a tuple where the first value represents whether or not
    the expression is a call to dataclass.field and the second is a
    dictionary of the keyword arguments that field() was called with.
    T)starz0Unpacking **kwargs in "field()" is not supportedz."field()" does not accept positional argumentsNF)r   r   calleer   r   r/   zip	arg_namesrn   	arg_kindsis_namedrR   r   )r   r`   rn   r3   r   rK   messagerE   rE   rF   r   K  s"   

r   Nr   )r`   r   r@   r6   )r   r   r`   r   r@   r   )>r   
__future__r   typing_extensionsr   
mypy.nodesr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   mypy.pluginr   r   mypy.plugins.commonr   r   r   r   mypy.server.triggerr    
mypy.stater!   mypy.typeopsr"   
mypy.typesr#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r.   __annotations__r/   r1   r2   r_   r   r   r   rE   rE   rE   rF   <module>   s*    `0D   
;

