o
    2g0                     @  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# 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 Z0G dd dZ1G dd dZ2G dd dZ3dOd'd(Z4dPd*d+Z5dQd.d/Z6dRd1d2Z7dSd3d4Z8dTd:d;Z9ej:dd<dUd?d@Z;	dVdWdFdGZ<dXdHdIZ=dYdKdLZ>dZdMdNZ?dS )[zmLow-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_exists)os_path_joinc                   @  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   I/home/garg/my-data/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_templatesD   s2   




z,ModuleNotFoundReason.error_message_templatesN)r(   r)   r*   r+   )r   r   r   r,   r.   r-   r/   r5   r   r   r   r   r$   1   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__)r7   r9   r:   r;   r<   )r1   r7   r9   r:   r;   r<   r   r   r   __init__j   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={}))formatr7   r9   r:   r;   r<   r1   r   r   r   __repr__x   s   zBuildSource.__repr__)NNF)r7   r8   r9   r8   r:   r8   r;   r8   r<   r)   r*   r=   )r*   r@   )r   r   r   __doc__r?   rC   r   r   r   r   r6   g   s    r6   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_pathsr:   r7   addr9   )r1   rF   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)r7   rL   	_fullnamerJ   rI   )r1   rO   r   r   r   	is_source   s
   
zBuildSourceSet.is_sourceN)rF   rG   r*   r=   )rO   r   r*   r)   )r   r   r   rD   r?   rR   r   r   r   r   rE      s    
rE   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 rP   )rT   r[   r   rU   initial_componentsresultsns_ancestorsrW   custom_typeshed_dirload_stdlib_py_versionsrY   )r1   rT   rU   rW   rY   r[   r`   r   r   r   r?      s   
zFindModuleCache.__init__c                 C  s"   | j   | j  | j  d S rP   )r^   clearr]   r_   rB   r   r   r   rb      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 d | V  qdS r?   N)rU   isfiler   .0x)dr1   r   r   	<genexpr>   s    
z=FindModuleCache.find_module_via_source_set.<locals>.<genexpr>c                 3  s    | ]
}  d | V  qdS rf   )endswithrh   )parentr   r   rl      s    )r[   rJ   getrU   rg   osr7   dirnamerangecountanyr#   rfindfind_module_via_source_set
isinstancer@   splitextisdirr$   r,   )r1   rc   p_idxbasenameextr   )rk   ro   r1   r   rw      s,   
z*FindModuleCache.find_module_via_source_setlib_pathr   PackageDirsc                 C  sh   | d}tj|dd }g }| ||d D ]}tjt||}| j	|r1|
|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.
        re   Nrm   r   T)splitrq   sepjoinget_toplevel_possibilitiesr7   normpathr   rU   rz   r0   )r1   rc   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]   rp   rU   listdirOSErrorrq   r7   ry   
setdefaultr0   )r1   r   rc   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.
        re   r   TN)r^   	partitionrY   _typeshed_has_version_find_modulerW   fast_module_lookupr$   r,   _can_find_module_in_parent_dirr-   )r1   rc   r   	top_leveluse_typeshedr   r   r   find_module!  s    




zFindModuleCache.find_moduler9   c                 C  s:   | j sdS t| j }| j| \}}||ko|d u p||kS )NT)rW   typeshed_py_versionrY   )r1   r9   versionmin_versionmax_versionr   r   r   r   7  s
   
z%FindModuleCache._typeshed_has_versionr   pkg_dir$OnePackageDir | ModuleNotFoundReasonc                 C  s   d}|}t |D ]@\}}t||}| jt|dr.tjj|g|d d R  |dkf  S |s@| j|s>| j|d r@d}| j|sH nqtd|rSt	j
S |rXt	jS t	jS )NFpy.typedrm   r   r"   Tre   )	enumerater   rU   rg   rq   r7   r   rz   r   r$   r/   r.   r,   )r1   r   r   plausible_matchdir_pathindex	componentr   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%   re   )	rs   lenr   r_   rU   rz   rq   r7   rr   )r1   r   r   r7   verifyi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   )rY   c                 s  s    | ]}t |V  qd S rP   )is_init_file)ri   rO   r   r   r   rl   h  s    zAFindModuleCache._can_find_module_in_parent_dir.<locals>.<genexpr>FT)rq   getcwdrS   r   rU   rW   rY   ru   r   r7   rr   rT   rx   r   r$   )r1   rc   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
|
|} |rt
|d}|g|dd   }tjj|
g|d d R  } |r |r |  dkrt
|
|}||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 | | } "||rSd	}|rMt# ||sM|||f q |    S  "||rt|rnt# ||sn|||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 |	rtjS |r tjS tj*S )Nre   rm   Fr   z-stubsr   r%   partialTr?   c                   s   g | ]\}}t  ||qS r   )highest_init_level)ri   r7   
dir_prefixrU   rc   r   r   
<listcomp>  s    z0FindModuleCache._find_module.<locals>.<listcomp>)+rU   rW   r   rw   r   rq   r   r   rT   r   r   rz   r7   rg   readdecodestripr0   r   rx   r$   r.   r/   r   use_builtins_fixturesrb   r   r   r   r   rs   r   rr   r#   isfile_caseverify_modulenamespace_packagesexists_caser   maxr_   rp   r,   )"r1   rc   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_componentsr7   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_modulerG   c                 C  s|  | j |dd}t|trg S t||d g}d }t|r"tj|}n| j	|r*|}|d u r0|S t
 }t| j|}|D ]~}|dv sH|drIq=t||}| jr`t|| jj| j| jjdkr`q=| j	|r| jrm| jjs| jt|ds| jt|dr|| || |d |  q=tj|\}	}
|	dkrq=|	|vrd|	vr|
tv r||	 || |d |	  q=|S )	NTr   )__pycache__zsite-packagesnode_modulesre   r&   __init__.py__init__.pyir?   )r   rx   r$   r6   r   rq   r7   rr   rU   rz   rK   sortedr   
startswithr   rW   matches_excludeexclude	verbosityr   rg   rM   extendfind_modules_recursivery   r#   )r1   r9   module_pathrF   r   seennamesr   subpathstemsuffixr   r   r   r   .  sL   



z&FindModuleCache.find_modules_recursive)NN)rT   r   rU   rV   rW   rX   rY   rZ   r[   r\   r*   r=   )r*   r=   )rc   r@   r*   rd   )rc   r@   r   r   r*   r   )r   r   rc   r@   r*   r   )rc   r@   r   r)   r*   r   )r9   r@   r*   r)   )r   r   r   r@   r*   r   )r   r   r   r   r*   r=   )rc   r@   r*   r)   )rc   r@   r   r)   r*   r   )r9   r@   r*   rG   )r   r   r   rD   r?   rb   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   rS      s"    


1





 @rS   r   r@   excludesr   rU   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 )rO   T)rq   r7   relpathreplacer   rz   researchprintsysstderr)r   r   rU   r   subpath_strr   r   r   r   r   b  s   
r   r7   c                 C  s   t j| dv S )N)r   r   )rq   r7   r~   )r7   r   r   r   r   t  s   r   rc   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.re   c                 3  (    | ]}  td | V  qdS rf   r   r   ri   r   rU   r7   r   r   r   rl   ~  
    
z verify_module.<locals>.<genexpr>FTr   rq   r7   rr   rs   rt   ru   r#   )rU   rc   r7   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   re   c                 3  r   rf   r   r   r   r   r   rl     r   z%highest_init_level.<locals>.<genexpr>r%   r   )rU   rc   r7   r   levelr   r   r   r   r     s   r   c                  C  s   t d} | s	g S | t jS )NMYPYPATH)rq   getenvr   pathsep)path_envr   r   r   r     s   
r   data_dir	pyversiontuple[int, int]r`   r8   c                 C  s  t j| } g }|r?t j|}t j|d}t j|dd}t j|d}t j|r2t j|s>td| t	d n t j| d}t j|rN|} t j| dd}t j| ddd}|
| |
| tjd	krs|
d
 |stdtj| tjd t	d |S )zJReturn default standard library search paths. Guaranteed to be normalised.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%   )rq   r7   abspathr   rz   rg   r   rA   r   exitr0   platform
executabler   )r  r  r`   r7   typeshed_dirmypy_extensions_dirversions_fileautor   r   r   default_lib_path  sD   





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 )a  Find package directories for given python. Guaranteed to return absolute paths.

    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  dictrq   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,  rF   rG   rW   r   alt_lib_pathc                   s  t t||j|jd}|jr3tdd}|s!tj	tj	t
}tj|}|tj|ddd g }|s_| D ]}|jrJ|j}||vrJ|| q9|jrQd}nt }||vr_|d| t }	|	|j |rp|	d| t|j\}
}|D ]<  |vsJ  |	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 qyt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-stubre   r   c                 3  "    | ]}|  tjj V  qd S rP   )r   rq   r7   r   ri   r{   siter   r   rl   6       z'compute_search_paths.<locals>.<genexpr>c                 3  r0  rP   )r   rq   r7   altsepr1  r2  r   r   rl   7  r4  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   rq   r  r7   rr   r"  r  
appendleftr   r;   r0   bazelr   insertr   r   r,  r  ru   r5  r   r   r   r  r   tuplereversed)rF   rW   r  r-  r   root_dirr   rN   r   mypypathr(  r)  r   r2  r   compute_search_paths  sf   




r@  c                 C  s   | p
t tjtd}t |d}i }t |d}tj|s$J | |tft|H}|D ]<}|dd  }|dkr;q+|d\}}|d}	t	|	d }
t
|	d	kr_|	d
  r_t	|	d
 nd}|
|f||< q+W d   |S 1 ssw   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   rH   :-r&   r%   N)r   rq   r7   rr   r"  rg   openr   r   parse_versionr   )r`   r  
stdlib_dirresultversions_pathfliner9   version_rangeversionsr   r   r   r   r   ra   J  s*   



&
ra   r   c                 C  s"   |   d\}}t|t|fS )Nre   )r   r   r   )r   majorminorr   r   r   rE  g  s   rE  c                 C  s   t | jdS )zIReturn Python version used for checking whether module supports typeshed.)r'      )r   r8  )rW   r   r   r   r   l  s   r   )
r   r@   r   r   rU   r   r   r)   r*   r)   )r7   r@   r*   r)   )
rU   r   rc   r@   r7   r@   r   r@   r*   r)   )
rU   r   rc   r@   r7   r@   r   r@   r*   r   )r*   r   )r  r@   r  r  r`   r8   r*   r   )r  r8   r*   r  rP   )
rF   rG   rW   r   r  r@   r-  r8   r*   r   )r`   r8   r*   r!   )r   r@   r*   r  )rW   r   r*   r  )@rD   
__future__r   r  r6  	functoolsrq   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   	mypy.utilr   r   r@   r)   OnePackageDirr   r   r!   r   r#   r$   r   r6   rE   rS   r   r   r   r   r   r  	lru_cacher,  r@  ra   rE  r   r   r   r   r   <module>   s\    $02   
M





/&
Y
