o
    7f                      @  s  U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZ ddlmZmZmZmZmZmZ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# G dd deZ$ee%e&f Z'ee' Z(ee%eee)e)f eee)e)f  f f Z*de+d< ddgZ,de+d< eG dd deZ-ee%e-f Z.G dd dZ/G dd dZ0G dd dZ1dNd&d'Z2dOd)d*Z3dPd-d.Z4dQd0d1Z5dRd2d3Z6dSd9d:Z7ej8dd;dTd>d?Z9	dUdVdEdFZ:dWdGdHZ;dXdJdKZ<dYdLdMZ=dS )ZzmLow-level infrastructure to find modules.

This builds on fscache.py; find_sources.py builds on top of this.
    )annotationsN)Enumunique)DictFinalList
NamedTupleOptionalTupleUnion)	TypeAlias)pyinfo)CompileError)FileSystemCache)MypyFile)Options)approved_stub_package_existsc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )SearchPathstuple[str, ...]python_path	mypy_pathpackage_pathtypeshed_pathN)__name__
__module____qualname____annotations__ r   r   J/home/ertert/spirit/venv/lib/python3.10/site-packages/mypy/modulefinder.pyr      s
   
 r   
_TypeAliasStdlibVersionsz.pyi.pyr   PYTHON_EXTENSIONSc                   @  s&   e Zd ZdZdZdZdZdd	d
ZdS )ModuleNotFoundReasonr            daemonboolreturntuple[str, list[str]]c                 C  s   d}| t ju rd}|g}||fS | t ju rd}dg}||fS | t ju r,d}|g}||fS | t ju rFd}dg}|s=|d || ||fS J )NzKSee https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-importszFCannot find implementation or library stub for module named "{module}"zOYou may be running mypy in a subpackage, mypy should be run on the package rootz`Skipping analyzing "{module}": module is installed, but missing library stubs or py.typed markerz*Library stubs not installed for "{module}"z*Hint: "python3 -m pip install {stub_dist}"zD(or run "mypy --install-types" to install all missing stub packages))r#   	NOT_FOUNDWRONG_WORKING_DIRECTORYFOUND_WITHOUT_TYPE_HINTSAPPROVED_STUBS_NOT_INSTALLEDappend)selfr'   doc_linkmsgnotesr   r   r   error_message_templatesC   s2   




z,ModuleNotFoundReason.error_message_templatesN)r'   r(   r)   r*   )r   r   r   r+   r-   r,   r.   r4   r   r   r   r   r#   0   s    r#   c                   @  s,   e Zd ZdZ			ddddZdddZdS )BuildSourcezA single source file.NFpath
str | Nonemoduletextbase_dirfollowedr(   r)   Nonec                 C  s&   || _ |pd| _|| _|| _|| _d S )N__main__)r6   r8   r9   r:   r;   )r0   r6   r8   r9   r:   r;   r   r   r   __init__i   s
   

zBuildSource.__init__strc                 C  s    d | j| j| jd u| j| jS )NzLBuildSource(path={!r}, module={!r}, has_text={}, base_dir={!r}, followed={}))formatr6   r8   r9   r:   r;   r0   r   r   r   __repr__w   s   zBuildSource.__repr__)NNF)r6   r7   r8   r7   r9   r7   r:   r7   r;   r(   r)   r<   )r)   r?   )r   r   r   __doc__r>   rB   r   r   r   r   r5   f   s    r5   c                   @  s$   e Zd ZdZdddZdddZdS )BuildSourceSetzIHelper to efficiently test a file's membership in a set of build sources.sourceslist[BuildSource]r)   r<   c                 C  s^   d| _ i | _t | _|D ] }|jd urd| _ |jr | j|j |jr,|jp'd| j|j< qd S )NFT )source_text_presentsource_modulessetsource_pathsr9   r6   addr8   )r0   rE   sourcer   r   r   r>      s   
zBuildSourceSet.__init__filer   r(   c                 C  s$   |j r	|j | jv p|j| jv p| jS N)r6   rK   	_fullnamerI   rH   )r0   rN   r   r   r   	is_source   s
   
zBuildSourceSet.is_sourceN)rE   rF   r)   r<   )rN   r   r)   r(   )r   r   r   rC   r>   rQ   r   r   r   r   rD      s    
rD   c                   @  s   e Zd ZdZ		d;d<ddZd=ddZd>ddZd?ddZd@ddZd d!dAd%d&Z	dBd(d)Z
dCd-d.ZdDd1d2ZdEd3d4ZdFd6d7ZdGd9d:ZdS )HFindModuleCacheaK  Module finder with integrated cache.

    Module locations and some intermediate results are cached internally
    and can be cleared with the clear() method.

    All file system accesses are performed through a FileSystemCache,
    which is not ever cleared by this class. If necessary it must be
    cleared by client code.
    Nsearch_pathsr   fscacheFileSystemCache | NoneoptionsOptions | Nonestdlib_py_versionsStdlibVersions | None
source_setBuildSourceSet | Noner)   r<   c                 C  sP   || _ || _|p
t | _i | _i | _i | _|| _d }|r|j}|p$t	|| _
d S rO   )rS   rZ   r   rT   initial_componentsresultsns_ancestorsrV   custom_typeshed_dirload_stdlib_py_versionsrX   )r0   rS   rT   rV   rX   rZ   r_   r   r   r   r>      s   
zFindModuleCache.__init__c                 C  s"   | j   | j  | j  d S rO   )r]   clearr\   r^   rA   r   r   r   ra      s   

zFindModuleCache.clearidr?   ModuleSearchResult | Nonec                   s   j sdS j j|d}|r<j|r<tj| t|	dD ]}t
 fddtD s3 dS tj  q"|S |d}|dkry|d| du sWttsYdS tj\}}t
fddtD sy|tv ryj|sytjS dS )zFast path to find modules by looking through the input sources

        This is only used when --fast-module-lookup is passed on the command line.N.c                 3  s*    | ]}j tj d | V  qdS r>   N)rT   isfileosr6   join.0x)dr0   r   r   	<genexpr>   s    
z=FindModuleCache.find_module_via_source_set.<locals>.<genexpr>c                 3  s    | ]
}  d | V  qdS re   )endswithri   )parentr   r   rm      s    )rZ   rI   getrT   rf   rg   r6   dirnamerangecountanyr"   rfindfind_module_via_source_set
isinstancer?   splitextisdirr#   r+   )r0   rb   p_idxbasenameextr   )rl   rp   r0   r   rw      s,   
z*FindModuleCache.find_module_via_source_setlib_pathr   PackageDirsc                 C  sl   | d}tj|dd }g }| ||d D ]}tjtj||}| j|r3|	|df q|S )zFind which elements of a lib_path have the directory a module needs to exist.

        This is run for the python_path, mypy_path, and typeshed_path search paths.
        rd   Nrn   r   T)
splitrg   seprh   get_toplevel_possibilitiesr6   normpathrT   rz   r/   )r0   rb   r   
components	dir_chaindirspathitemdirr   r   r   find_lib_path_dirs   s   
z"FindModuleCache.find_lib_path_dirs	list[str]c              	   C  s   || j v r| j | |g S i }|D ],}z| j|}W n ty'   g }Y nw |D ]}tj|d }||g 	| q*q|| j |< ||g S )a  Find which elements of lib_path could contain a particular top-level module.

        In practice, almost all modules can be routed to the correct entry in
        lib_path by looking at just the first component of the module name.

        We take advantage of this by enumerating the contents of all of the
        directories on the lib_path and building a map of which entries in
        the lib_path could contain each potential top-level module that appears.
        r   )
r\   rq   rT   listdirOSErrorrg   r6   ry   
setdefaultr/   )r0   r   rb   r   r   contentsnamer   r   r   r     s   

z*FindModuleCache.get_toplevel_possibilitiesF)	fast_pathr   r(   ModuleSearchResultc                C  s   || j vrJ|dd }d}|| jv r| |}n
|| jv r#| |}| ||| j |< |sJ| jdur7| jjsJ| j | tju rJ| 	|rJtj
| j |< | j | S )zReturn the path of the module source file or why it wasn't found.

        If fast_path is True, prioritize performance over generating detailed
        error descriptions.
        rd   r   TN)r]   	partitionrX   _typeshed_has_version_find_modulerV   fast_module_lookupr#   r+   _can_find_module_in_parent_dirr,   )r0   rb   r   	top_leveluse_typeshedr   r   r   find_module   s    




zFindModuleCache.find_moduler8   c                 C  s:   | j sdS t| j }| j| \}}||ko|d u p||kS )NT)rV   typeshed_py_versionrX   )r0   r8   versionmin_versionmax_versionr   r   r   r   6  s
   
z%FindModuleCache._typeshed_has_versionr   pkg_dir$OnePackageDir | ModuleNotFoundReasonc                 C  s   d}|}t |D ]D\}}tj||}| jtj|dr2tjj|g|d d R  |dkf  S |sD| j|sB| j|d rDd}| j|sL nqtt|ddD ]}t	d|d | rgt
j  S qU|rmt
jS t
jS )NFpy.typedrn   r   r!   Trd   )	enumeraterg   r6   rh   rT   rf   rz   rs   lenr   r#   r.   r-   r+   )r0   r   r   plausible_matchdir_pathindex	componentir   r   r   _find_module_non_stub_helper=  s,   (

z,FindModuleCache._find_module_non_stub_helpermatchtuple[str, bool]c                 C  s`   |\}}t dt|D ]"}d|d |  }|| jvr'| j|r'|| j|< tj|}qd S )Nr$   rd   )	rs   r   rh   r^   rT   rz   rg   r6   rr   )r0   r   r   r6   verifyr   pkg_idr   r   r   _update_ns_ancestorsU  s   
z$FindModuleCache._update_ns_ancestorsc                 C  s   t  }ttdddd| j| j| jd}tdd t |D rFt j	
|}t|fddd|_t||dts:dS tdd t |D s dS )zsTest if a module can be found by checking the parent directories
        of the current working directory.
        r   )rX   c                 s  s    | ]}t |V  qd S rO   )is_init_file)rj   rN   r   r   r   rm   h  s    zAFindModuleCache._can_find_module_in_parent_dir.<locals>.<genexpr>FT)rg   getcwdrR   r   rT   rV   rX   ru   r   r6   rr   rS   rx   r   r#   )r0   rb   working_dirparent_searchr   r   r   r   ]  s   z.FindModuleCache._can_find_module_in_parent_dirr   c           "        s  | j  | jd ur| jjr| nd }|r|S d}tj|d d }g }g }d}d}	| jj	D ]}
|d d }tj
|
|} |rtj
|d}|g|dd   }tj
j|
g|d d R  } |r |r |  dkrtj
|
|}||d	f ||df n||d	f n||d	f | ||
}t|tr|tju rd	}q2|tju rd	}	q2|| | || q2| jr| jjr|  |  d}| jj| jj }| |}|r|| | jj7 }||| 7 }tj|d  }tjd
 }g }|D ]\}}|| }d}|}tt|d D ]	}tj
|}qt D ]U}|| | }|d | | } !||rYd	}|rSt" ||sS|||f q&|    S  !||rz|rtt" ||st|||f q&|    S q&| jr| jj#r|s $||r !||s|||f t D ](}|| } !||r|rt" ||s|||f q|    S qq| jr| jj#r|r fdd|D }|%t&|} ||  d S | j'(}!|!d ur|!S |	r tjS |rtjS tj)S )Nrd   rn   Fr   z-stubsr   r$   partialTr>   c                   s   g | ]\}}t  ||qS r   )highest_init_level)rj   r6   
dir_prefixrT   rb   r   r   
<listcomp>  s    z0FindModuleCache._find_module.<locals>.<listcomp>)*rT   rV   r   rw   r   rg   r   rh   rS   r   r6   rz   rf   readdecodestripr/   r   rx   r#   r-   r.   r   use_builtins_fixturesra   r   r   r   r   rs   r   rr   r"   isfile_caseverify_modulenamespace_packagesexists_caser   maxr^   rq   r+   )"r0   rb   r   r{   r   r   third_party_inline_dirsthird_party_stubs_dirs-found_possible_third_party_missing_type_hintsneed_installed_stubsr   	stub_namestub_dirstub_typed_filestub_componentsr6   runtime_pathnon_stub_matchpython_mypy_pathcandidate_base_dirsseplastsepinitnear_missesr:   r   	base_pathhas_initr   r|   	extension
path_stubslevelsr   ancestorr   r   r   r   o  s   












zFindModuleCache._find_modulerF   c                 C  s  |  |}t|trg S t||d g}d }t|r tj|}n| j	|r(|}|d u r.|S t
 }t| j|}|D ]}|dv sF|drGq;tj||}| jr`t|| jj| j| jjdkr`q;| j	|r| jrm| jjs| jtj|ds| jtj|dr|| || |d |  q;tj|\}	}
|	dkrq;|	|vrd|	vr|
tv r||	 || |d |	  q;|S )N)__pycache__zsite-packagesnode_modulesrd   r%   __init__.py__init__.pyir>   )r   rx   r#   r5   r   rg   r6   rr   rT   rz   rJ   sortedr   
startswithrh   rV   matches_excludeexclude	verbosityr   rf   rL   extendfind_modules_recursivery   r"   )r0   r8   module_pathrE   r   seennamesr   subpathstemsuffixr   r   r   r   .  sL   



z&FindModuleCache.find_modules_recursive)NN)rS   r   rT   rU   rV   rW   rX   rY   rZ   r[   r)   r<   )r)   r<   )rb   r?   r)   rc   )rb   r?   r   r   r)   r   )r   r   rb   r?   r)   r   )rb   r?   r   r(   r)   r   )r8   r?   r)   r(   )r   r   r   r?   r)   r   )r   r   r   r   r)   r<   )rb   r?   r)   r(   )rb   r?   r   r(   r)   r   )r8   r?   r)   rF   )r   r   r   rC   r>   ra   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   rR      s"    


1





 @rR   r   r?   excludesr   rT   r   verboser(   r)   c                 C  sp   |sdS t j| t jd}|| r|d7 }|D ]}t||r5|r2td| d| dt	j
d  dS qdS )NF/zTRACE: Excluding z (matches pattern )rN   T)rg   r6   relpathreplacer   rz   researchprintsysstderr)r   r   rT   r   subpath_strr   r   r   r   r   b  s   
r   r6   c                 C  s   t j| dv S )N)r   r   )rg   r6   r~   )r6   r   r   r   r   t  s   r   rb   prefixc                   sX   t r
tjt|dD ]}tjt fddtD s) dS qdS )z;Check that all packages containing id have a __init__ file.rd   c                 3  ,    | ]}  tjd | V  qdS re   r   rg   r6   rh   rj   r   rT   r6   r   r   r   rm   ~  
    
z verify_module.<locals>.<genexpr>FTr   rg   r6   rr   rs   rt   ru   r"   )rT   rb   r6   r   r   r   r   r   r   x  s   r   intc                   s^   t r
tjd}t|dD ]}tjt fddtD r,|d }q|S )z:Compute the highest level where an __init__ file is found.r   rd   c                 3  r   re   r   r   r   r   r   rm     r   z%highest_init_level.<locals>.<genexpr>r$   r   )rT   rb   r6   r   levelr   r   r   r   r     s   r   c                  C  s   t d} | s	g S | t jS )NMYPYPATH)rg   getenvr   pathsep)path_envr   r   r   r     s   
r   data_dir	pyversiontuple[int, int]r_   r7   c                 C  s   g }|r3t j|d}t j|dd}t j|d}t j|r&t j|s2td| td n t j| d}t j|rB|} t j| dd}t j| ddd}|	| |	| tj
d	krg|	d
 |sztdtj| tjd td |S )z-Return default standard library search paths.stdlibstubszmypy-extensionsVERSIONSzDerror: --custom-typeshed-dir does not point to a valid typeshed ({})r%   z
stubs-autotypeshedwin32z/usr/local/lib/mypyz~Could not resolve typeshed subdirectories. Your mypy install is broken.
Python executable is located at {}.
Mypy located at {}r   r$   )rg   r6   rh   rz   rf   r   r@   r   exitr/   platform
executabler   )r  r  r_   r6   typeshed_dirmypy_extensions_dirversions_fileautor   r   r   default_lib_path  s@   





r  )maxsizepython_executabletuple[list[str], list[str]]c              
   C  s   | du rg g fS | t jkrt \}}||fS i ttjddi}ztt	j
| tjdg|t	jd \}}W ||fS  t	jyS } zt|j t|j  d}~w typ } zt|j}td|  d| g|d}~ww )zFind package directories for given python.

    This runs a subprocess call, which generates a list of the directories in sys.path.
    To avoid repeatedly calling a subprocess (which can be slow!) we
    lru_cache the results.
    NPYTHONSAFEPATH1getsearchdirs)envr   z!mypy: Invalid python executable 'z': )r   r  r   r  dictrg   environastliteral_eval
subprocesscheck_output__file__PIPEr   CalledProcessErrorr   r   stdoutr   strerrorerrnor   )r  sys_pathsite_packagesr  errreasonr   r   r   get_search_dirs  s<   	




r)  rE   rF   rV   r   alt_lib_pathc                   s  t t||j|jd}|jr-tdd}|s!tj	tj	t
}|tj|ddd g }|sY| D ]}|jrD|j}||vrD|| q3|jrKd}nt }||vrY|d| t }	|	|j |rj|	d| t|j\}
}|D ]<  |vs{J  |	v st fd	d
|	D stjjrt fdd
|	D rt  dtjd tdtjd td qsttt|t|	t|
| t|dS )a/  Compute the search paths as specified in PEP 561.

    There are the following 4 members created:
    - User code (from `sources`)
    - MYPYPATH (set either via config or environment variable)
    - installed package directories (which will later be split into stub-only and inline)
    - typeshed
    )r_   MYPY_TEST_PREFIXNz	test-dataunitzlib-stubrd   r   c                 3  "    | ]}|  tjj V  qd S rO   )r   rg   r6   r   rj   r{   siter   r   rm   2       z'compute_search_paths.<locals>.<genexpr>c                 3  r-  rO   )r   rg   r6   altsepr.  r/  r   r   rm   3  r1  z& is in the MYPYPATH. Please remove it.r   zbSee https://mypy.readthedocs.io/en/stable/running_mypy.html#how-mypy-handles-imports for more infor$   )r   r   r   r   )collectionsdequer  python_versionr_   r   rg   r   r6   rr   r  
appendleftrh   r:   r/   bazelr   insertr   r   r)  r  ru   r2  r   r   r   r
  r   tuplereversed)rE   rV   r  r*  r   root_dirr   rM   r   mypypathr%  r&  r   r/  r   compute_search_paths  sd   




r=  c                 C  s   | pt jt jtd}t j|d}i }t j|d}t j|s*J | |tft|H}|D ]<}|dd  }|dkrAq1|d\}}|d}	t	|	d }
t
|	d	kre|	d
  ret	|	d
 nd}|
|f||< q1W d   |S 1 syw   Y  |S )zReturn dict with minimum and maximum Python versions of stdlib modules.

    The contents look like
    {..., 'secrets': ((3, 6), None), 'symbol': ((2, 7), (3, 9)), ...}

    None means there is no maximum version.
    r  r  r  #r   rG   :-r%   r$   N)rg   r6   rh   rr   r  rf   openr   r   parse_versionr   )r_   r  
stdlib_dirresultversions_pathfliner8   version_rangeversionsr   r   r   r   r   r`   E  s*   

&
r`   r   c                 C  s"   |   d\}}t|t|fS )Nrd   )r   r   r   )r   majorminorr   r   r   rB  b  s   rB  c                 C  s   t | jdS )zIReturn Python version used for checking whether module supports typeshed.)r&      )r   r5  )rV   r   r   r   r   g  s   r   )
r   r?   r   r   rT   r   r   r(   r)   r(   )r6   r?   r)   r(   )
rT   r   rb   r?   r6   r?   r   r?   r)   r(   )
rT   r   rb   r?   r6   r?   r   r?   r)   r   )r)   r   )r  r?   r  r  r_   r7   r)   r   )r  r7   r)   r  rO   )
rE   rF   rV   r   r  r?   r*  r7   r)   r   )r_   r7   r)   r    )r   r?   r)   r  )rV   r   r)   r  )>rC   
__future__r   r  r3  	functoolsrg   r   r  r   enumr   r   typingr   r   r   r   r	   r
   r   typing_extensionsr   r   mypyr   mypy.errorsr   mypy.fscacher   
mypy.nodesr   mypy.optionsr   mypy.stubinfor   r   r?   r(   OnePackageDirr   r   r    r   r"   r#   r   r5   rD   rR   r   r   r   r   r   r  	lru_cacher)  r=  r`   rB  r   r   r   r   r   <module>   sZ    $02   
N





,&
W
