o
    DDiL                     @   s   d dl mZmZmZ d dlmZmZmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZ d dlmZmZ dZG dd	 d	eZd
S )    )SetTupleUnion)Import
ImportFrom
ImportStarModule)CodemodContextVisitorBasedCodemodCommand)GatherCommentsVisitorRemoveImportsVisitor)+get_absolute_module_from_package_for_import)PositionProvider	ProviderTzA.*\W(noqa|lint-ignore: ?unused-import|lint-ignore: ?F401)(\W.*)?$c                       s   e Zd ZU dZdZeed< efZe	e
 ed< deddf fdd	Zd
edefddZd
edefddZd
edefddZd
eeef ddfddZ  ZS )RemoveUnusedImportsCommandaV  
    Remove all unused imports from a file based on scope analysis.

    This command analyses individual files in isolation and does not attempt
    to track cross-references between them. If a symbol is imported in a file
    but otherwise unused in it, that import will be removed even if it is being
    referenced from another file.
    z\Remove all imports that are not used in a file. Note: only considers the file in isolation. DESCRIPTIONMETADATA_DEPENDENCIEScontextreturnNc                    s   t  | t | _d S )N)super__init__set_ignored_lines)selfr   	__class__ f/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/codemod/commands/remove_unused_imports.pyr   %   s   z#RemoveUnusedImportsCommand.__init__nodec                 C   s*   t | jt}|| t|j | _dS )NT)r   r   DEFAULT_SUPPRESS_COMMENT_REGEXvisitr   commentskeysr   )r   r   comment_visitorr   r   r   visit_Module)   s   
z'RemoveUnusedImportsCommand.visit_Modulec                 C      |  | dS NF_handle_importr   r   r   r   r   visit_Import1      
z'RemoveUnusedImportsCommand.visit_Importc                 C   r%   r&   r'   r)   r   r   r   visit_ImportFrom5   r+   z+RemoveUnusedImportsCommand.visit_ImportFromc                 C   s   |  t|jj}|| jv rd S |j}t|trd S |D ]K}|  t|}tt	|jj|j
jd }|| jrft|trGtj| j|j|jd qt| jj|}|d u rZtd|j tj| j||j|jd qd S )N   )moduleasnamez&Couldn't get absolute module name for )r.   objr/   )get_metadatar   startliner   names
isinstancer   r   rangeend
isdisjointr   r   remove_unused_importr   evaluated_nameevaluated_aliasr   full_package_name
ValueError)r   r   
node_startr4   aliaspositionlinesmodule_namer   r   r   r(   9   s>   



z)RemoveUnusedImportsCommand._handle_import)__name__
__module____qualname____doc__r   str__annotations__r   r   r   r   r	   r   r   boolr$   r   r*   r   r,   r   r(   __classcell__r   r   r   r   r      s   
 

"r   N)typingr   r   r   libcstr   r   r   r   libcst.codemodr	   r
   libcst.codemod.visitorsr   r   libcst.helpersr   libcst.metadatar   r   r   r   r   r   r   r   <module>   s   