
%![c           @   s  d  Z  d d l Z d d l Z d d l 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
 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 e j   Z d d  Z e e j _ e e j _ e e j _ e e j _ e e j _ e e j _ d d  Z d d  Z e e j _ e e j _ e e j  _ d d  Z! d   Z" e! e j# _ d   Z$ d d  Z% e	 j& e%  e j' _ e% e j( _) e	 j* e	 j& d d    Z+ e+ e j, _ e	 j& d e- d   Z. e. e j/ _ d   Z0 e0 e j/ _0 e	 j& d e- d   Z1 e1 e j2 _ e	 j* d d   Z3 e	 j& e3  e j4 _ e3 e j5 _) e	 j& d d   Z6 e6 e j7 _ e8   Z9 e	 j* d d   Z: e	 j& e:  e j; _ e: e j; _) e	 j* e	 j& d d    Z< e< e j= _ d   Z> d d  Z? e	 j* e	 j& d d    Z@ e? e jA _? e@ e jA _ d   ZB d    ZC eD d!  ZE eD d"  ZF d#   ZG d$   ZH d%   ZI d&   ZJ d'   ZK d(   ZL e	 jM e	 j& d d)    ZN eL e jO _L eN e jO _ d d*  ZP e	 j& d d+   ZQ eP e jR _P eQ e jR _ d d,  ZS eS e jT _ e	 j& d d-   ZU eU e j( _ eU e j5 _ e	 j& d d.   ZV eV e jW _ d d/  ZX eX e jY _ d d0  ZZ eZ e j[ _\ d S(1   sM   this module contains a set of functions to handle inference on astroid trees
iN(   t   bases(   t   context(   t
   exceptions(   t
   decorators(   t   helpers(   t   manager(   t   nodes(   t   dunder_lookup(   t	   protocols(   t   utilc         c   s	   |  Vd S(   sR   inference's end for node such as Module, ClassDef, FunctionDef,
    Const...

    N(    (   t   selfR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt	   infer_end"   s    c         c   sg   t  d   |  j D  s! |  VnB t |  |  } t |   |  j |  j |  j  } | j |  | Vd  S(   Nc         s   s!   |  ] } t  | t j  Vq d  S(   N(   t
   isinstanceR   t   Starred(   t   .0t   e(    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>1   s    (   t   anyt   eltst
   _infer_seqt   typet   linenot
   col_offsett   parentt   postinit(   R
   R   t   valuest   new_seq(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt	   infer_seq0   s    !c         C   s   g  } x |  j  D] } t | t j  r t j | j |  } | d t j	 f k rm t
 j d |  d |   n  t | d  s t
 j d |  d |   n  | j t |   q | j |  q W| S(   s-   Infer all values based on _BaseContainer.eltst   nodeR   R   N(   R   R   R   R   R   t
   safe_infert   valuet   NoneR	   t   UninferableR   t   InferenceErrort   hasattrt   extendR   t   append(   R   R   R   t   eltt   starred(    (    s0   lib/python2.7/site-packages/astroid/inference.pyR   :   s    c         c   ss   t  d   |  j D  s! |  VnN t |  |  } t |   |  j |  j |  j  } | j t | j     | Vd  S(   Nc         s   s'   |  ] \ } } t  | t j  Vq d  S(   N(   R   R   t
   DictUnpack(   R   t   kt   _(    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>S   s    (	   R   t   itemst
   _infer_mapR   R   R   R   R   t   list(   R
   R   R)   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt	   infer_mapR   s    !c         C   s  i  } x|  j  D]\ } } t | t j  r t j | |  } | d t j f k ra t	 j
  n  t | t j  s t	 j
 d |  d |   n  | j t | |   q t j | d | } t j | d | } t d   | | f D  rt	 j
 d |  d |   n  | | | <q W| S(   s$   Infer all values based on Dict.itemsR   R   c         s   s$   |  ] } | d  t j f k Vq d  S(   N(   R   R	   R   (   R   t   elem(    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>k   s    N(   R)   R   R   R&   R   R   R   R	   R   R   R    t   Dictt   updateR*   R   (   R   R   R   t   nameR   t   double_starredt   key(    (    s0   lib/python2.7/site-packages/astroid/inference.pyR*   \   s"    c         C   sO   |  } x, | j  r4 t | j  t j  r4 | j  } q	 W| rK | j  rK | j  Sd S(   s   Search for the first function which encloses the given
    scope. This can be used for looking up in that function's
    scope, in case looking up in a lower scope for a particular
    name fails.

    :param node: A scope node.
    :returns:
        ``None``, if no parent function scope was found,
        otherwise an instance of :class:`astroid.scoped_nodes.Function`,
        which encloses the given node.
    N(   R   R   R   t   FunctionDefR   (   R   t   current(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _higher_function_scopeu   s    "c         C   s   |  j  |  j  \ } } | s t |  j    } | rQ | j  |  j  \ } } n  | s t j d |  j d |  j   d |   q n  | j   } |  j | _ t j	 | | |  S(   s#   infer a Name: use name lookup rulesR0   t   scopeR   (
   t   lookupR0   R5   R6   R   t   NameInferenceErrort   clonet
   lookupnameR    t   _infer_stmts(   R
   R   t   framet   stmtst   parent_functionR(   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt
   infer_name   s    c         c   s   | j    } t j d |  j d |  j  | _ d | _ x |  j j	 |  D]r } | t
 j k ri | VqI n  y8 t | d  r x" | j |  |  D] } | Vq Wn  WqI t j k
 r qI qI XqI Wt t d |  d |    d S(   s>   infer a Call node by trying to guess what the function returnst   argst   keywordst   infer_call_resultR   R   N(   R9   t
   contextmodt   CallContextR@   RA   t   callcontextR   t	   boundnodet   funct   inferR	   R   R!   RB   R   R    t   StopIterationt   dict(   R
   R   RE   t   calleet   inferred(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt
   infer_call   s    	c         c   s   | j  } | d k r0 t j d |  d |   n  y2 | rS |  j |  j |   Vn |  j |  VWn; t j k
 r } t j t j d |  d | d |   n Xd S(   s7   infer an Import node: return the imported module/objectR   R   t   errorN(	   R:   R   R   R    t   do_import_modulet	   real_namet   AstroidBuildingErrorR	   t   reraise(   R
   R   t   asnameR0   t   exc(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_import   s    	c         C   s(   t  j   } | | _ |  j | d t S(   NRS   (   RC   t   InferenceContextR:   RH   t   False(   R
   R0   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_name_module   s    	c      	   C   s+  | j  } | d k r0 t j d |  d |   n  | rH |  j |  } n  y |  j   } Wn; t j k
 r } t j t j d |  d | d |   n XyM t	 j
 |  } | | _  | j | d | |  j   k } t j | |  SWnA t j k
 r&} t j t j | j d |  d | d |  n Xd S(   s:   infer a ImportFrom node: return the imported module/objectR   R   RN   t   ignore_localst   targett	   attributeN(   R:   R   R   R    RP   RO   RQ   R	   RR   RC   t   copy_contextt   getattrt   rootR    R;   t   AttributeInferenceErrort   message(   R
   R   RS   R0   t   moduleRT   R=   RN   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_import_from   s$    		!c         c   s]  x;|  j  j |  D]'} | t j k r3 | Vq n  | r | j r t | t j  r t | j t j  r y7 t j	 t j
 | j  t j
 |   r | j } n  Wq t j k
 r q Xq n  y; | | _ x" | j |  j |  D] } | Vq Wd | _ Wq t j t j f k
 r d | _ q t k
 r9d | _ q Xq Wt t d |  d |    d S(   sA   infer an Attribute node by using getattr on the associated objectR   R   N(   t   exprRH   R	   R   RF   R   R    t   InstanceR   t
   is_subtypet   object_typeR   t   _NonDeducibleTypeHierarchyt   igetattrt   attrnameR   R_   R    t   AttributeErrorRI   RJ   (   R
   R   t   ownert   obj(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_attribute   s.    
		c      	   C   s   | j  d  k r* t j d |  d |   n  y& t j |  j   j | j   |  SWnD t j k
 r } t	 j
 t j | j d |  d | j  d |  n Xd  S(   NR   R   RZ   R[   (   R:   R   R   R    R    R;   R^   R]   R_   R	   RR   R`   (   R
   R   RN   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_global  s    c   	      c   s  t  |  j j |   } | t j k r3 t j Vd St  |  j j |   } | t j k rf t j Vd St } | j t j	 k r | } n9 | j t j	 k r t
 j |  } | r | } q n | } | t k r t j d |  d |   n  y | j | |  } WnM t j t j t j t f k
 rL} t j t j d |  d | d |   n X|  | k sh| t j k rtt j Vd Sx | j |  D] } | VqWt t d |  d |    d S(   s   Inference for subscripts

    We're understanding if the index is a Const
    or a slice, passing the result of inference
    to the value's `getitem` method, which should
    handle each supported index type accordingly.
    NR   R   RN   (   t   nextR   RH   R	   R   t   slicet   _SUBSCRIPT_SENTINELt	   __class__R    Rd   R   t   class_instance_as_indexR   R    t   getitemt   AstroidTypeErrort   AstroidIndexErrorR_   Rj   RR   RI   RJ   (	   R
   R   R   t   indext   index_valuet   instance_as_indext   assignedRT   RL   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_subscript#  s@    
		c   	      c   sY  |  j  } |  j d k r$ t j } n	 t j } y) g  | D] } | j d |  ^ q7 } Wn t j k
 ru t j	 Vd SXx t
 j |   D] } t d   | D  r t j	 Vq n  g  | D] } | j   ^ q } t d   | D  r t j	 Vq n  t j	 } x7 t | |  D]! \ } } | |  r| VPqqW| Vq Wt t d |  d |    d S(   s   Infer a boolean operation (and / or / not).

    The function will calculate the boolean operation
    for all pairs generated through inference for each component
    node.
    t   orR   Nc         s   s   |  ] } | t  j k Vq d  S(   N(   R	   R   (   R   t   item(    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>t  s    c         s   s   |  ] } | t  j k Vq d  S(   N(   R	   R   (   R   R}   (    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>z  s    R   (   R   t   opt   operatort   trutht   not_RH   R   R    R	   R   t	   itertoolst   productR   t
   bool_valuet   zipRI   RJ   (	   R
   R   R   t	   predicateR   t   pairR}   t   bool_valuesR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _infer_boolop^  s0    			)		c         c   s=   x6 | |  |  D]% } t  | |  r0 t j Vq | Vq Wd  S(   N(   R   R	   R   (   R
   t   infer_callableR   RN   t   result(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _filter_operation_errors  s    c   
      c   s?  x8|  j  j |  D]$} y | j |  j  VWq t k
 rZ } t j | |  j |  Vq t k
 r6} t j	 |  j } | d k r | j   } | t j k	 r t j |  Vq3t j Vq7t | t j t j f  s t j | |  j |  Vq n  y y t j | |  } Wn. t j k
 r>t j | |  j |  Vw n X| d } t | j d |   } | t j k s}| j   rw n  t j |  } t j d | g  | _ | j |  d | } t | d  }	 |	 d k r| Vn |	 VWq7t j k
 r} t j | |  j |  Vq7t j k
 r2t j Vq7Xq Xq Wd S(   s3   Infer what an UnaryOp should return when evaluated.i    R   R@   N(   t   operandRH   t   infer_unary_opR~   t	   TypeErrorR	   t   BadUnaryOperationMessageRj   R   t   UNARY_OP_METHODR   R   R   R   t   const_factoryR   R    Rd   t   ClassDefR   R7   R   R_   Ro   t   callableRC   R\   RD   RE   RB   R    (
   R
   R   R   RT   t   methR   t   methodsRL   t   call_resultsR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _infer_unaryop  sH    
	c         c   sG   x% t  |  t | t j  D] } | Vq Wt t d |  d |    d S(   s3   Infer what an UnaryOp should return when evaluated.R   R   N(   R   R   R	   R   RI   RJ   (   R
   R   RL   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_unaryop  s    	c         C   s   t  |  t j  o |  j t k S(   s0   Check if the given const node is NotImplemented.(   R   R   t   ConstR   t   NotImplemented(   t   const(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _is_not_implemented  s    c   	      C   sh   t  j |  |  } | d } t | j d |   } | t j k rO t j  n  |  j | | | | |  S(   s8   Invoke binary operation inference on the given instance.i    R   (	   R   R7   Ro   RH   R	   R   R   R    t   infer_binary_op(	   t   instancet   opnodeR~   t   otherR   t   method_nameR   t   methodRL   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _invoke_binop_inference  s    
c         C   s>   t  j | } t j t d |  d | d | d | d | d | S(   s<   Get an inference callable for an augmented binary operation.R   R~   R   R   R   R   (   R   t   AUGMENTED_OP_METHODt	   functoolst   partialR   (   R   R   R~   R   R   t   reverseR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _aug_op  s    c         C   sT   | r t  j | } n t  j | } t j t d |  d | d | d | d | d | S(   s   Get an inference callable for a normal binary operation.

    If *reverse* is True, then the reflected method will be used instead.
    R   R~   R   R   R   R   (   R   t   REFLECTED_BIN_OP_METHODt   BIN_OP_METHODR   R   R   (   R   R   R~   R   R   R   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _bin_op  s    c         c   sM   xF | | f D]8 } |  j    } t j d | g  | _ d | _ | Vq Wd S(   s   Get contexts for binary operations.

    This will return two inferrence contexts, the first one
    for x.__op__(y), the other one for y.__rop__(x), where
    only the arguments are inversed.
    R@   N(   R9   RC   RD   RE   R   RF   (   R   t   leftt   rightt   argt   new_context(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _get_binop_contexts  s
    		c         C   s   |  j    | j    k S(   s$   Check if type1 is the same as type2.(   t   qname(   t   type1t   type2(    (    s0   lib/python2.7/site-packages/astroid/inference.pyt
   _same_type"  s    c   	   	   C   s   | j  } t | |  r6 t |  | | | |  g } n t j | |  rf t |  | | | |  g } n t j | |  r t | | | |  | d t t |  | | | |  g } n6 t |  | | | |  t | | | |  | d t g } | S(   sV  Get the flow for binary operations.

    The rules are a bit messy:

        * if left and right have the same type, then only one
          method will be called, left.__op__(right)
        * if left and right are unrelated typewise, then first
          left.__op__(right) is tried and if this does not exist
          or returns NotImplemented, then right.__rop__(left) is tried.
        * if left is a subtype of right, then only left.__op__(right)
          is tried.
        * if left is a supertype of right, then right.__rop__(left)
          is first tried and then left.__op__(right)
    R   (   R~   R   R   R   Re   t   is_supertypet   True(	   R   t	   left_typet   binary_opnodeR   t
   right_typeR   t   reverse_contextR~   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _get_binop_flow'  s    	!c   
   
   C   sQ  | j  j d  } | j  } t | |  r] t |  | | | |  t |  | | | |  g }	 n t j | |  r t |  | | | |  t |  | | | |  g }	 n t j | |  rt |  | | | |  t | | | |  | d t t |  | | | |  g }	 nK t |  | | | |  t |  | | | |  t | | | |  | d t g }	 |	 S(   s  Get the flow for augmented binary operations.

    The rules are a bit messy:

        * if left and right have the same type, then left.__augop__(right)
          is first tried and then left.__op__(right).
        * if left and right are unrelated typewise, then
          left.__augop__(right) is tried, then left.__op__(right)
          is tried and then right.__rop__(left) is tried.
        * if left is a subtype of right, then left.__augop__(right)
          is tried and then left.__op__(right).
        * if left is a supertype of right, then left.__augop__(right)
          is tried, then right.__rop__(left) and then
          left.__op__(right)
    t   =R   (	   R~   t   stripR   R   R   R   Re   R   R   (
   R   R   t
   aug_opnodeR   R   R   R   t   bin_opt   aug_opR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _get_aug_flowE  s     	!c         c   sm  t  | |  |  \ } } t j |   } t j |  } | |  | | | | | |  } x | D] }	 y t |	    }
 WnF t k
 r q[ q[ t j k
 r q[ q[ t j k
 r t j	 Vd SXt
 d   |
 D  r t j	 Vd St t t |
   r q[ n  t d   |
 D  } | r4| t |
  k r4t j	 Vd Sx |
 D] } | Vq;Wd Sq[ Wt j | | j |  Vd S(   s   Infer a binary operation between a left operand and a right operand

    This is used by both normal binary operations and augmented binary
    operations, the only difference is the flow factory used.
    Nc         s   s   |  ] } | t  j k Vq d  S(   N(   R	   R   (   R   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>  s    c         s   s!   |  ] } t  |  r d  Vq d S(   i   N(   R   (   R   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pys	   <genexpr>  s    (   R   R   Rf   R+   Rj   R   R_   R    R	   R   R   t   allt   mapR   t   sumt   lent   BadBinaryOperationMessageR~   (   R   R   R   R   t   flow_factoryR   R   R   R   R   t   resultst   not_implementedR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _infer_binary_operationi  s8    	c   	      c   s  | d k r t j   } n  |  j } |  j } | j   } | j   } x | j d |  D] } | t j k ry t j Vd Sx | j d |  D]l } | t j k r t j Vd Sy, x% t	 | | |  | t
  D] } | Vq WWq t j k
 r t j Vq Xq WqX Wd S(   s"   Binary operation inferrence logic.R   N(   R   RC   RV   R   R   R9   RH   R	   R   R   R   R   Rg   (	   R
   R   R   R   t   lhs_contextt   rhs_contextt   lhst   rhsR   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _infer_binop  s(    		c         C   s   t  |  t | t j  S(   N(   R   R   R	   R   (   R
   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_binop  s    c         c   s   | d k r t j   } n  x |  j j d |  D] } | t j k rR t j Vd S| j   } x |  j j	 d |  D]l } | t j k r t j Vd Sy, x% t
 | | |  | t  D] } | Vq WWqt t j k
 r t j Vqt Xqt Wq1 Wd S(   s0   Inference logic for augmented binary operations.R   N(   R   RC   RV   RZ   t	   infer_lhsR	   R   R9   R   RH   R   R   R   Rg   (   R
   R   R   R   R   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   _infer_augassign  s     c         C   s   t  |  t | t j  S(   N(   R   R   R	   R   (   R
   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_augassign  s    c         C   sC   | j  } | d  k r0 t j d |  d |   n  t j |  | |  S(   NR   R   (   R:   R   R   R    R   t   _arguments_infer_argname(   R
   R   R0   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_arguments  s    	c         C   sS   |  j    } t | t j  r+ | j |  St |  j d |   } t j | |  S(   sW   infer a AssignName/AssignAttr: need to inspect the RHS part of the
    assign node
    R   (	   t	   statementR   R   t	   AugAssignRH   R+   t   assigned_stmtsR    R;   (   R
   R   t   stmtR=   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_assign  s
    c         c   sf   |  j    s t j VnK y, x% t j |  j d | D] } | Vq3 WWn t j k
 ra t j Vn Xd  S(   NR   (   t   has_underlying_objectR	   R   t   MANAGERt   infer_ast_from_somethingt   objectR   t   AstroidError(   R
   R   RL   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_empty_node   s    c         C   s   |  j  j |  S(   N(   R   RH   (   R
   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   infer_index  s    c      
   C   s   | r | j    } n t j   } } t j d | g  | _ |  | _ t |  j d d |  } t | t	 j
  s t j d d |  d |  n  y t | j |  |   SWn< t k
 r t j t j d d d |  d | d |   n Xd  S(	   NR@   t   __getitem__R   s(   Could not find __getitem__ for {node!r}.R   R`   s)   Inference for {node!r}[{index!s}] failed.Rw   (   R9   RC   RV   RD   RE   RF   Ro   Rh   R   R    t   BoundMethodR   R    RB   RI   R	   RR   (   R
   Rw   R   R   R   (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   instance_getitem  s     	(]   t   __doc__R   R   R   t   astroidR    R   RC   R   R   R   R   R   t   astroid.interpreterR   R   R	   t   AstroidManagerR   R   R   t   Modulet   _inferR   R3   t   LambdaR   t   SliceR   R   t   Listt   Tuplet   SetR,   R*   R.   R5   R?   t   path_wrappert   Namet
   AssignNameR   t   raise_if_nothing_inferredRM   t   CallR   RU   t   ImportRX   Rb   t
   ImportFromRm   t	   Attributet
   AssignAttrRn   t   GlobalR   Rq   R{   t	   SubscriptR   t   BoolOpR   R   R   t   UnaryOpR   R   RW   R   R   R   R   R   R   R   R   t   yes_if_nothing_inferredR   t   BinOpR   R   R   R   t	   ArgumentsR   R   t	   EmptyNodeR   t   IndexR   Rd   Rt   (    (    (    s0   lib/python2.7/site-packages/astroid/inference.pyt   <module>
   s   

			"
	64	6			

				$	/	!

