
    e                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ erddlmZ dd	Z	 	 	 	 	 	 dd
Z G d de
      ZddZy)z#Dataclass checkers for Python code.    )annotations)TYPE_CHECKING)nodes)DATACLASS_MODULES)BaseCheckerutils)	INFERENCE)PyLinterc                &    | j                   t        v S )z=Utility function to check if node is from dataclasses_module.)namer   )nodes    C/usr/lib/python3/dist-packages/pylint/checkers/dataclass_checker.py_is_dataclasses_moduler      s    99)))    c                    t        | t        j                        rt        | j                        |k(  S t        | j
                        |k(  S )zdUtility function to check either a Name/Attribute node's name/attrname with a
    given string.
    )
isinstancer   Namestrr   attrname)r   
check_withs     r   _check_name_or_attrname_eq_tor      s9     $

#499~++t}}++r   c                  j    e Zd ZdZdZddiZ ej                  d      d
d       Zd
dZ		 	 	 	 	 	 ddZ
y	)DataclassCheckerzoChecker that detects invalid or problematic usage in dataclasses.

    Checks for
    * invalid-field-call
    	dataclassE3701)zInvalid usage of field(), %sinvalid-field-callzThe dataclasses.field() specifier should only be used as the value of an assignment within a dataclass, or within the make_dataclass() function.r   c                &    | j                  |       y N)_check_invalid_field_call)selfr   s     r   
visit_callzDataclassChecker.visit_call6   s    &&t,r   c                |   t        |j                  t        j                  t        j                  f      syt        |j                  d      syt        j                  |j                        }t        |t        j                        rt        |j                               sy|j                  }|rct        |t        j                  t        j                  f      s9|j                  }|r+t        |t        j                  t        j                  f      s9t        |t        j                        r| j                  ||       y|r|j                  s| j!                  d|dt"               yt        |j                  t        j$                        r||j                  j&                  k(  s| j!                  d|dt"               yy)af  Checks for correct usage of the dataclasses.field() specifier in
        dataclasses or within the make_dataclass() function.

        Emits message
        when field() is detected to be used outside a class decorated with
        @dataclass decorator and outside make_dataclass() function, or when it
        is used improperly within a dataclass.
        Nfieldr   zFit should be used within a dataclass or the make_dataclass() function.r   args
confidence)z;it should be the value of an assignment within a dataclass.)r   funcr   r   	Attributer   r   
safe_inferFunctionDefr   rootparentClassDefCall%_check_invalid_field_call_within_callis_dataclassadd_messager	   	AnnAssignvalue)r    r   inferred_func
scope_nodes       r   r   z*DataclassChecker._check_invalid_field_call:   sQ    $))ejj%//%BC,TYY@((3}e&7&78&}'9'9';<[[
J8T!U#**J J8T!U j%**-66tZH!8!8$ %   4;;8TT[[EVEV=V$U$	   >Wr   c                v   t        j                  |j                        }t        |j                  t        j
                  t        j                  f      rM|j                  j                  dk(  r4t        |t        j                        rt        |j                               ry| j                  d|dt               y)zvChecks for special case where calling field is valid as an argument of the
        make_dataclass() function.
        make_dataclassNr   r$   r%   )r   r*   r(   r   r   r   
AssignNamer   r+   r   r,   r2   r	   )r    r   r6   r5   s       r   r0   z6DataclassChecker._check_invalid_field_call_within_callh   s     ((9zU5E5E(FG$$(88=%*;*;<&}'9'9';<  ! 	 	
r   N)r   
nodes.CallreturnNone)r   r:   r6   r:   r;   r<   )__name__
__module____qualname____doc__r   msgsr   only_required_for_messagesr!   r   r0    r   r   r   r   %   sb     D 
D &U%%&:;- <-,\

,6
	
r   r   c                8    | j                  t        |              y r   )register_checkerr   )linters    r   registerrG      s    
,V45r   N)r   znodes.Moduler;   bool)r   znodes.Name | nodes.Attributer   r   r;   rH   )rF   r
   r;   r<   )r@   
__future__r   typingr   astroidr   astroid.brain.brain_dataclassesr   pylint.checkersr   r   pylint.interfacesr	   pylint.lintr
   r   r   r   rG   rC   r   r   <module>rP      sX   
 * "    = . '$*
,
&,47,	,X
{ X
v6r   