
    eO              
         U d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	 d dl
mZmZmZmZmZ d dlmZ d dlmZmZ erd d	lmZ  G d
 de      Zi d edd      d edd      d edd      d edd      d edd      d edd      d edd      d edd      d edd      d  ed!d      d" ed#d      d$ ed%d      d& ed'd      d( ed)d      d* ed+d      d, ed-d      d. ed/d      i d0 ed1d      d2 ed3d      d4 ed5d      d6 ed7d      d8 ed9d      d: ed;d      d< ed=d      d> ed?d      d@ edAd      dB edCd      dD edEd      dF edGd      dH edId      dJ edKd      dL edMd      dN edOd      dP edQd       edRd       edSd       edTd       edUd       edVd       edWd      dXZdYedZ<    ed[ eD              Zd\Z G d] d^e      Z G d_ d`e	      ZdbdaZy)c    )annotations)TYPE_CHECKING
NamedTupleN)nodes)BaseChecker)in_type_checking_block"is_node_in_type_annotation_contextis_postponed_evaluation_enabledonly_required_for_messages
safe_infer)TYPING_NORETURN)HIGH	INFERENCE)PyLinterc                  "    e Zd ZU ded<   ded<   y)TypingAliasstrnameboolname_collisionN)__name__
__module____qualname____annotations__     :/usr/lib/python3/dist-packages/pylint/extensions/typing.pyr   r      s    
Ir   r   ztyping.TupletupleFztyping.Listlistztyping.Dictdictz
typing.Setsetztyping.FrozenSet	frozensetztyping.Typetypeztyping.Dequezcollections.dequeTztyping.DefaultDictzcollections.defaultdictztyping.OrderedDictzcollections.OrderedDictztyping.Counterzcollections.Counterztyping.ChainMapzcollections.ChainMapztyping.Awaitablezcollections.abc.Awaitableztyping.Coroutinezcollections.abc.Coroutineztyping.AsyncIterablezcollections.abc.AsyncIterableztyping.AsyncIteratorzcollections.abc.AsyncIteratorztyping.AsyncGeneratorzcollections.abc.AsyncGeneratorztyping.Iterablezcollections.abc.Iterableztyping.Iteratorzcollections.abc.Iteratorztyping.Generatorzcollections.abc.Generatorztyping.Reversiblezcollections.abc.Reversibleztyping.Containerzcollections.abc.Containerztyping.Collectionzcollections.abc.Collectiontyping.Callablezcollections.abc.Callableztyping.AbstractSetzcollections.abc.Setztyping.MutableSetzcollections.abc.MutableSetztyping.Mappingzcollections.abc.Mappingztyping.MutableMappingzcollections.abc.MutableMappingztyping.Sequencezcollections.abc.Sequenceztyping.MutableSequencezcollections.abc.MutableSequenceztyping.ByteStringzcollections.abc.ByteStringztyping.MappingViewzcollections.abc.MappingViewztyping.KeysViewzcollections.abc.KeysViewztyping.ItemsViewzcollections.abc.ItemsViewztyping.ValuesViewzcollections.abc.ValuesViewz!contextlib.AbstractContextManagerz&contextlib.AbstractAsyncContextManagerz
re.Patternzre.Matchzcollections.abc.Hashablezcollections.abc.Sized)ztyping.ContextManagerztyping.AsyncContextManagerztyping.Patternztyping.Matchztyping.Hashableztyping.Sizedzdict[str, TypingAlias]DEPRECATED_TYPING_ALIASESc              #  D   K   | ]  }|j                  d       d     yw).   N)split).0keys     r   	<genexpr>r,   M   s     Oc		#q)Os    )OptionalUnionc                  :    e Zd ZU ded<   ded<   ded<   dZded<   y	)
DeprecatedTypingAliasMsgnodes.Name | nodes.Attributenoder   qnamealiasFr   parent_subscriptN)r   r   r   r   r5   r   r   r   r0   r0   Q   s    
&&JJ"d"r   r0   c                      e Zd ZU dZdZddddddd	Zd
dddddffZded<   	 ded<   	 d* fdZd+dZ	d,dZ
 eddddd      d-d       Z eddddd      d.d       Z ed      d/d       Ze	 	 	 	 	 	 d0d       Zd1d Zed2d!       Z	 d3	 	 	 	 	 d4d"Z	 	 	 	 	 	 d5d#Z	 	 	 	 	 	 d6d$Z	 	 	 	 d7d%Z edd      d8d&       Zd7d'Zd7d(Zd9d)Z xZS ):TypingCheckerz4Find issue specifically related to type annotations.typing)z$'%s' is deprecated, use '%s' insteaddeprecated-typing-aliasz/Emitted when a deprecated typing alias is used.)z@'%s' will be deprecated with PY39, consider using '%s' instead%sconsider-using-aliasz|Only emitted if 'runtime-typing=no' and a deprecated typing alias is used in a type annotation context in Python 3.7 or 3.8.)z9Consider using alternative Union syntax instead of '%s'%s!consider-alternative-union-syntaxznEmitted when 'typing.Union' or 'typing.Optional' is used instead of the alternative Union syntax 'int | None'.)z;'NoReturn' inside compound types is broken in 3.7.0 / 3.7.1broken-noreturnz``typing.NoReturn`` inside compound types is broken in Python 3.7.0 and 3.7.1. If not dependent on runtime introspection, use string annotation instead. E.g. ``Callable[..., 'NoReturn']``. https://bugs.python.org/issue34921)zo'collections.abc.Callable' inside Optional and Union is broken in 3.9.0 / 3.9.1 (use 'typing.Callable' instead)broken-collections-callablez``collections.abc.Callable`` inside Optional and Union is broken in Python 3.9.0 and 3.9.1. Use ``typing.Callable`` for these cases instead. https://bugs.python.org/issue42965)zVType `%s` is used more than once in union type annotation. Remove redundant typehints.redundant-typehint-argumentziDuplicated type arguments will be skipped by `mypy` tool, therefore should be removed to avoid confusion.)W6001R6002R6003E6004E6005R6006zruntime-typingTynz<y or n>au  Set to ``no`` if the app / library does **NOT** need to support runtime introspection of type annotations. If you use type annotations **exclusively** for type checking of an application, you're probably fine. For libraries, evaluate if some users want to access the type hints at runtime first, e.g., through ``typing.get_type_hints``. Applies to Python versions 3.7 - 3.9)defaultr#   metavarhelpr   _should_check_typing_alias&_should_check_alternative_union_syntaxc                n    t         |   |       d| _        t               | _        g | _        g | _        y)zInitialize checker instance.linterFN)super__init___found_broken_callable_locationr!   _alias_name_collisions_deprecated_typing_alias_msgs_consider_using_alias_msgs)selfrM   	__class__s     r   rO   zTypingChecker.__init__   s5    '5:,03#MO*JL'r   c                   | j                   j                  j                  }|dk\  | _        |dk\  | _        |dk\  | _        | j                  xs0 | j                  xr" | j                   j                  j                  du | _        | j
                  xs0 | j                  xr" | j                   j                  j                  du | _        |dk  | _	        |dk  | _
        y )N)      )rW   	   )rW   
   F)rW   rX      )rW   rY   r[   )rM   config
py_version
_py37_plus
_py39_plus_py310_plusruntime_typingrI   rJ   _should_check_noreturn_should_check_callable)rT   r]   s     r   openzTypingChecker.open   s    [[''22
$.$.%0*.// +
OOJ 2 2 A AU J 	' 7;6F6F 7
OOJ 2 2 A AU J 	3 '19&<#&09&<#r   c                V    | j                   sd|j                         j                  v ryy)z3Message hint if postponed evaluation isn't enabled.r    z2. Add 'from __future__ import annotations' as well)r`   rootfuture_importsrT   r2   s     r   _msg_postponed_eval_hintz&TypingChecker._msg_postponed_eval_hint   s$    }		0J0JJCr   r9   r:   r;   r<   r=   c                   | j                   r#|j                  t        v r| j                  |       | j                  r.|j                  t
        v r| j                  ||j                         | j                  r |j                  dk(  r| j                  |       | j                  r"|j                  dk(  r| j                  |       y y y NNoReturnCallable)rI   r   ALIAS_NAMES_check_for_typing_aliasrJ   UNION_NAMES#_check_for_alternative_union_syntaxrb   _check_broken_noreturnrc   _check_broken_callableri   s     r   
visit_namezTypingChecker.visit_name   s     **tyyK/G((.66499;S44T499E&&499
+B''-&&499
+B''- ,C&r   c                   | j                   r#|j                  t        v r| j                  |       | j                  r.|j                  t
        v r| j                  ||j                         | j                  r |j                  dk(  r| j                  |       | j                  r"|j                  dk(  r| j                  |       y y y rl   )rI   attrnamero   rp   rJ   rq   rr   rb   rs   rc   rt   ri   s     r   visit_attributezTypingChecker.visit_attribute   s     **t}}/K((.664==K;W44T4==I&&4==J+F''-&&4==J+F''- ,G&r   r>   c                   |j                   }| j                  |d      r+| j                  |      r| j                  d|dt               y | j                  |d      r;t        |j                  t        j                        r|j                  j                  }n$| j                  |      r| j                  |      }ny | j                  ||       y )Nr-   r>   Noner2   args
confidencer.   )
annotation_is_deprecated_union_annotation_is_optional_none_annotationadd_messager   
isinstanceslicer   Tupleelts_is_binop_union_annotation_parse_binops_typehints_check_union_types)rT   r2   r~   typess       r   visit_annassignzTypingChecker.visit_annassign   s    __
//
JG00<  1##	 !  //
GDekkJ
 $$))E,,Z800<Et,r   c                    t        | t        j                        xr? t        | j                  t        j                        xr | j                  j
                  |k(  S N)r   r   	SubscriptvalueNamer   )r~   
union_names     r   r   z-TypingChecker._is_deprecated_union_annotation   sI    
 z5??3 4:++UZZ84  %%3	
r   c                R    | j                   xr t        |t        j                        S r   )rJ   r   r   BinOp)rT   r~   s     r   r   z(TypingChecker._is_binop_union_annotation  s&    :: 
z@
 	
r   c                ~    t        | j                  t        j                        xr | j                  j                  d u S r   )r   r   r   Constr   )r~   s    r   r   z*TypingChecker._is_optional_none_annotation
  s4     z''5X*:J:J:P:PTX:X	
r   c                   |xs g }t        |j                  t        j                        r,|j	                  | j                  |j                  |             n|j                  |j                         |j                  |j                         |S r   )r   leftr   r   extendr   appendright)rT   
binop_nodetypehints_lists      r   r   z%TypingChecker._parse_binops_typehints  sp     (-2joou{{3!!,,Z__nM !!*//2j../r   c                    t               }|D ]A  }|j                         }||v r| j                  d||t               1|j	                  |       C y )Nr>   r{   )r!   	as_stringr   r   add)rT   r   r~   	types_settypehinttypehint_strs         r   r   z TypingChecker._check_union_types  s]     E	 
	,H#--/Ly(  1#&#	 !  l+
	,r   c                f   t        |      }t        |t        j                        r|j	                         dv s8t        |t
        j                  j                        r|j	                         dk(  sy| j                  st        |      sy| j                  d||| j                  |      ft               y)zCheck if alternative union syntax could be used.

        Requires
        - Python 3.10
        - OR: Python 3.7+ with postponed evaluation in
              a type annotation context
           typing.Uniontyping.Optionaltyping._SpecialFormNr;   r{   )r   r   r   FunctionDefr3   astroidbasesInstancer`   r	   r   rj   r   )rT   r2   r   inferreds       r   rr   z1TypingChecker._check_for_alternative_union_syntax-  s     d#x!2!23 $GG(GMM$:$:; $99  $Ft$L/55d;< 	 	 	
r   c                   t        |      }t        |t        j                        syt        j                  |j                         d      }|y| j                  rj|j                         dk(  r| j                  |      rd| _	        | j                  j                  t        ||j                         |j                               yt        |      s\t        |j                  t        j                         r8|j"                  du r)| j$                  j'                  |j                                y| j(                  j                  t        ||j                         |j                  t        |j                  t        j                                      y)a  Check if typing alias is deprecated or could be replaced.

        Requires
        - Python 3.9
        - OR: Python 3.7+ with postponed evaluation in
              a type annotation context

        For Python 3.7+: Only emit message if change doesn't create
            any name collisions, only ever used in a type annotation
            context, and can safely be replaced.
        Nr$   T)r   r   r   ClassDefr%   getr3   r_   _broken_callable_locationrP   rR   r   r0   r   r	   parentr   r   rQ   r   rS   )rT   r2   r   r4   s       r   rp   z%TypingChecker._check_for_typing_aliasJ  s4    d#(ENN3)--hnn.>E=??~~#449W9W: 8<4..55(NN$JJ  2$7JKK=
 ##t+++//0@A''..$ 

4;;8		
r   c                   | j                   rg| j                  D ]W  }| j                  r|j                  dk(  r| j	                  d|j
                  |j                  |j                  ft               Y n| j                  r| j                  |      }| j                  D ]c  }|j                  | j                  v r| j	                  d|j
                  |j                  |j                  |j                  r|ndft               e d| _        | j                  j                          | j                  j                          | j                  j                          y)zAfter parsing of module is complete, add messages for
        'consider-using-alias' check.

        Make sure results are safe to recommend / collision free.
        r$   r9   r{   r:   rf   FN)r_   rR   rP   r3   r   r2   r4   r   r^   rj   rS   rQ   r5   clear)rT   r2   msgmsg_future_imports       r   leave_modulezTypingChecker.leave_module~  s0    ??99 88		%66  -))SYY/(	 !  __ $ = =d C66 99 ; ;;  *				-0-A-A)r
  ) ! 	 05,**002##))+''--/r   c                >   t        |j                  t        j                        syt	        |      st        |      rt        |      ry|j                         D ]  }t        |t        j                  t        j                  f      r|j                         t        v sht        |t        j                  j                        sht        |j                  t        j                        s|j                  j                         dk(  s| j!                  d|t"                y y)z+Check for 'NoReturn' inside compound types.Nr   r<   r2   r}   )r   r   r   BaseContainerr   r
   r	   inferr   r   r3   r   r   r   BaseInstance_proxiedr   r   rT   r2   r   s      r   rs   z$TypingChecker._check_broken_noreturn  s    $++u':':; #4(.t4248

 	H 8e&7&7%HINN$7h(B(BCx00%..A%%++-1FF  !2) T	r   c                    t        |      }t        |t        j                        r$|j	                         dk(  r| j                  |      sy| j                  d|t               y)z?Check for 'collections.abc.Callable' inside Optional and Union.z_collections_abc.CallableNr=   r   )r   r   r   r   r3   r   r   r   r   s      r   rt   z$TypingChecker._check_broken_callable  sQ    d#x0 $??..t46TiXr   c                   t        |      st        |      rt        |      ryt        |j                  t
        j                        rt        |j                  j                  t
        j                        rgt        |j                  j                  j                        dk(  r;t        |j                  j                  j                  d   t
        j                        sy|j                  j                  }t        |t
        j                        r|j                  }t        |t
        j                        r4t        |j                  t
        j                  t
        j                  f      syt!        |j                        }t        |t
        j"                        r|j%                         dv s8t        |t&        j(                  j*                        r|j%                         dk(  syy)zFCheck if node would be a broken location for collections.abc.Callable.Fr[   r   r   r   T)r   r
   r	   r   r   r   r   r   r   lenr   Listr   r   r   	Attributer   r   r3   r   r   r   )rT   r2   r5   inferred_parents       r   r   z'TypingChecker._broken_callable_location  sL    #4(.t4248 t{{EOO44;;,,ekk:DKK%%**+q04;;,,11!4ejjA  ;;--&(;(;</66'9+11EJJ3PQ$%5%;%;<(9(9:%%'+NN/7==+A+AB%%'+@@r   rM   r   returnrz   )r   rz   )r2   nodes.NodeNGr   r   )r2   z
nodes.Namer   rz   )r2   znodes.Attributer   rz   )r2   znodes.AnnAssignr   rz   )r~   r   r   r   r   r   )r~   r   r   r   )r~   znodes.Subscriptr   r   r   )r   znodes.BinOpr   zlist[nodes.NodeNG] | Noner   list[nodes.NodeNG])r   r   r~   r   r   rz   )r2   r1   r   r   r   rz   )r2   r1   r   rz   )r2   znodes.Moduler   rz   )r2   r1   r   r   )r   r   r   __doc__r   msgsoptionsr   rO   rd   rj   r   ru   rx   r   staticmethodr   r   r   r   r   rr   rp   r   rs   rt   r   __classcell__)rU   s   @r   r7   r7   X   s   >D






G)DX %;	
G( !%$ -10M= D  !+%..  !+%..   =>- ?-, 
 
.1
	
 


 
 
 TX%7P	,',5A,	, 
*
 
 
	
:2
*2
 
2
h   68QR(0 S(0T4
Y%r   r7   c                8    | j                  t        |              y r   )register_checkerr7   rL   s    r   registerr     s    
M&12r   r   ) 
__future__r   r8   r   r   astroid.basesr   r   pylint.checkersr   pylint.checkers.utilsr   r	   r
   r   r   pylint.constantsr   pylint.interfacesr   r   pylint.lintr   r   r%   r   r"   ro   rq   r0   r7   r   r   r   r   <module>r      st  
 # ,   '  - -$* 
+5K/+5;vu-+5 ;vu-+5 +eU+	+5
 K7+5 ;vu-+5 K 3T:+5 +&?F+5 +&?F+5 k"7>+5 {#94@+5 $?F+5 $?F+5 K(GN+5 K(GN+5  [)I4P!+5" {#=tD#+5$ {#=tD%+5& $?F'+5( %A4H)+5* $?F++5, %A4H-+5. {#=tD/+50 +&;UC1+52 %A4H3+54 k";TB5+56 [)I4P7+58 {#=tD9+5: k*KTR;+5< %A4H=+5> +&CTJ?+5@ {#=tDA+5B $?FC+5D %A4HE+5F ))LeT"-0%# ",5
D1"#=tD 7>U+5 1 +Z O5NOO##z #\K \~3r   