B
    S\                 @   s   d Z ddlZddlmZ ddlmZ G dd dZG dd dZG d	d
 d
ZG dd deZG dd deZ	G dd dZ
G dd dZdS )z:This module contains some mixins for the different nodes.
    N)
decorators)
exceptionsc               @   s(   e Zd ZdZejdd ZdddZdS )BlockRangeMixInzoverride block range c             C   s   | j S )N)lineno)self r   -lib/python3.7/site-packages/astroid/mixins.pyblockstart_tolineno   s    z#BlockRangeMixIn.blockstart_tolinenoNc             C   sR   || j kr||fS |rD||d j kr2||d jfS ||d j d fS ||pN| jfS )z^handle block line numbers range for try/finally, for, if and while
        statements
        r      )Z
fromlinenoZtolineno)r   r   ZorelseZlastr   r   r   _elsed_block_range   s    
z"BlockRangeMixIn._elsed_block_range)N)__name__
__module____qualname____doc__r   cachedpropertyr	   r   r   r   r   r   r      s   r   c               @   s    e Zd ZdZdd Zdd ZdS )FilterStmtsMixinz1Mixin for statement filtering and assignment typec             C   s   |   |kr|gdfS |dfS )z@method used in _filter_stmts to get statements and trigger breakTF)	statement)r   _node_stmtsmystmtr   r   r   _get_filtered_stmts,   s    
z$FilterStmtsMixin._get_filtered_stmtsc             C   s   | S )Nr   )r   r   r   r   assign_type4   s    zFilterStmtsMixin.assign_typeN)r   r   r   r   r   r   r   r   r   r   r   )   s   r   c               @   s   e Zd Zdd Zdd ZdS )AssignTypeMixinc             C   s   | S )Nr   )r   r   r   r   r   9   s    zAssignTypeMixin.assign_typec             C   s.   | |kr|dfS |   |kr&|gdfS |dfS )zmethod used in filter_stmtsTF)r   )r   Zlookup_noder   r   r   r   r   r   r   <   s
    
z#AssignTypeMixin._get_filtered_stmtsN)r   r   r   r   r   r   r   r   r   r   8   s   r   c               @   s   e Zd Zdd ZdS )ParentAssignTypeMixinc             C   s
   | j  S )N)parentr   )r   r   r   r   r   H   s    z!ParentAssignTypeMixin.assign_typeN)r   r   r   r   r   r   r   r   r   G   s   r   c               @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
ImportFromMixinzMixIn for From and Import Nodesc             C   s   |S )Nr   )r   framenamer   r   r   _infer_nameO   s    zImportFromMixin._infer_nameNc             C   sP   |   }t| dd}|dkr"| j}||||jkr8|S |j|||oJ|dkdS )zOreturn the ast for a module whose name is <modname> imported by <self>
        levelNr   )r!   Zrelative_only)rootgetattrmodnameZrelative_to_absolute_namer   import_module)r   r$   Zmymoduler!   r   r   r   do_import_moduleR   s    z ImportFromMixin.do_import_modulec             C   sX   xB| j D ]8\}}|dkr|S |s4|ddd }|}||kr|S qW tjd| |ddS )zget name from 'as' name*.r   r   z:Could not find original name for {attribute} in {target!r})targetZ	attributeN)namessplitr   ZAttributeInferenceError)r   Zasnamer   Z_asnamer   r   r   	real_nameg   s    zImportFromMixin.real_name)N)r   r   r   r   r    r&   r,   r   r   r   r   r   L   s   
r   c               @   s<   e Zd ZdZejdd Zdd Zdd Zej	dd	 Z
d
S )MultiLineBlockMixina.  Mixin for nodes with multi-line blocks, e.g. For and FunctionDef.
    Note that this does not apply to every node with a `body` field.
    For instance, an If node has a multi-line body, but the body of an
    IfExpr is not multi-line, and hence cannot contain Return nodes,
    Assign nodes, etc.
    c                s   t  fdd jD S )Nc             3   s   | ]}t  |V  qd S )N)r#   ).0Zfield)r   r   r   	<genexpr>   s    z9MultiLineBlockMixin._multi_line_blocks.<locals>.<genexpr>)tupleZ_multi_line_block_fields)r   r   )r   r   _multi_line_blocks   s    z&MultiLineBlockMixin._multi_line_blocksc             c   s8   x2| j D ](}x"|D ]}|jrq| E d H  qW qW d S )N)r1   Zis_function _get_return_nodes_skip_functions)r   block
child_noder   r   r   r2      s
    
z4MultiLineBlockMixin._get_return_nodes_skip_functionsc             c   s8   x2| j D ](}x"|D ]}|jrq| E d H  qW qW d S )N)r1   Z	is_lambda_get_yield_nodes_skip_lambdas)r   r3   r4   r   r   r   r5      s
    
z1MultiLineBlockMixin._get_yield_nodes_skip_lambdasc             C   s    dd | j D }ttj|S )Nc             s   s    | ]}|D ]}|  V  q
qd S )N)_get_assign_nodes)r.   r3   r4   r   r   r   r/      s   z8MultiLineBlockMixin._get_assign_nodes.<locals>.<genexpr>)r1   list	itertoolschainfrom_iterable)r   Zchildren_assign_nodesr   r   r   r6      s    
z%MultiLineBlockMixin._get_assign_nodesN)r   r   r   r   r   r   r1   r2   r5   cachedr6   r   r   r   r   r-   x   s
   r-   c               @   s   e Zd ZdZdd ZdS )NoChildrenMixinz,Mixin for nodes with no children, e.g. Pass.c             c   s   dE d H  d S )Nr   r   )r   r   r   r   get_children   s    zNoChildrenMixin.get_childrenN)r   r   r   r   r=   r   r   r   r   r<      s   r<   )r   r8   Zastroidr   r   r   r   r   r   r   r-   r<   r   r   r   r   <module>   s   ,$