
    Dd*Z                        d 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 ddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ ddlmZm Z m!Z! ddl"m#Z# ddl$m%Z%  G d d          Z& ee          d             Z' eg           d             Z(d Z)d Z*d Z+ G d d          Z,	 	 d)d Z- e%j.                    ed!                         Z/	 d*d#Z0d+d$Z1d+d%Z2d& Z3d' Z4edfd(Z5dS ),a  
:mod:`jedi.inference.imports` is here to resolve import statements and return
the modules/classes/functions/whatever, which they stand for. However there's
not any actual importing done. This module is about finding modules in the
filesystem. This can be quite tricky sometimes, because Python imports are not
always that simple.

This module also supports import autocompletion, which means to complete
statements like ``from datetim`` (cursor at the end would return ``datetime``).
    N)Path)tree)search_ancestor)debug)settings)FolderIO)get_cached_code_lines)sys_path)helpers)compiled)analysis)unite)inference_state_method_cache)
ImportNameSubModuleName)ValueSet	NO_VALUES)import_module_decoratorcreate_stub_moduleparse_stub_module)ImplicitNSInfo)plugin_managerc                        e Zd Zd Zd Zd ZdS )ModuleCachec                     i | _         d S N_name_cacheselfs    6lib/python3.11/site-packages/jedi/inference/imports.py__init__zModuleCache.__init__$   s        c                 "    ||| j         |<   d S d S r   r   )r    string_names	value_sets      r!   addzModuleCache.add'   s'     	7-6D\***	7 	7r#   c                 6    | j                             |          S r   )r   get)r    r%   s     r!   r)   zModuleCache.get+   s    ##L111r#   N)__name__
__module____qualname__r"   r'   r)    r#   r!   r   r   #   sA          7 7 72 2 2 2 2r#   r   )defaultc                     |                                  }t          ||          \  }}}}|rM|K|                    || d          }|s1||fz   }t          | j        |||          }|                                }t          j        d|           |S )NFname_contextanalysis_errorszafter import: %s)get_root_context_prepare_infer_importpy__getattribute__Importerinference_statefollowr   dbg)	context	tree_namemodule_contextfrom_import_nameimport_pathlevelvaluespathimporters	            r!   infer_importrC   1   s    --//Nni88 1k5& + 
	+.. $ % /  F  +"&6%88#G$;T>SXYY!**	I &)))Mr#   c                 p   	                                   }t          |          \  	}}}|sg S 	lt           	fd|D                       }|rt          fd|D                       s|S |	fz   }t	           j        |||          }|                                }t          d |D                       S )Nc                 @    g | ]}|                     d           S )Fr0   )goto).0cr:   r=   s     r!   
<listcomp>zgoto_import.<locals>.<listcomp>P   sG     
 
 

 	 FF $ %   
 
 
r#   c              3   *   K   | ]}|j         u V  d S r   )r;   )rG   nr;   s     r!   	<genexpr>zgoto_import.<locals>.<genexpr>X   s*      EE!Q[I5EEEEEEr#   c              3   $   K   | ]}|j         V  d S r   )name)rG   ss     r!   rL   zgoto_import.<locals>.<genexpr>^   s$      &&!qv&&&&&&r#   )r3   r4   r   anyr6   r7   r8   set)
r:   r;   r<   r>   r?   r@   namesrA   rB   r=   s
   ``       @r!   goto_importrS   G   s   --//Nni88 1k5& 	 # 
 
 
 
 

 
 
 
    	EEEEuEEEEE 	L.00G3T>5QQ""&&v&&&&&&r#   c                    t          |dd          }|                    |          }d }	 |                                }t          |          dz   t          |          k    r
|d         }|}n# t          $ r Y nw xY wt          | j        t          |          | |j                  }|t          |          |j        |	                                fS )Nimport_nameimport_from   )
r   get_path_for_nameget_from_nameslenAttributeErrorr6   r7   tupler?   r8   )r<   r;   import_noder>   r=   
from_namesrB   s          r!   r4   r4   a   s    !)]MJJK//	::K
% //11

 z??Q#k"2"22 	%  +2$K     6k8J8J&(9; ;H U;//1BHOODUDUUUs   A, ,
A98A9c                     t          |d          r| t          j        | d||           d S t          j        d|z              d S )Nparentzimport-errorzImportError without origin: )hasattrr   r'   r   warning)valuerN   messages      r!   
_add_errorrf   w   sU    tX @5 @UND':::::4w>?????r#   c                 V   t          |dz
            D ],}|}t          j                            |          }||k    r dS -|}g }	 || k    r||fS t          j                            |          }|r6|                    d|           t          j                            |          }nd|fS f)z
    In case the level is outside of the currently known package (something like
    import .....foo), we can still try our best to help the user for
    completions.
    rW   NNTr   N)rangeosrA   dirnamebasenameinsert)project_path	directoryr?   iolddlevel_import_pathsdir_names           r!   _level_to_base_import_pathru   ~   s     519  GOOI..	) 	::	 	A# 	)%q((7##A&& 	#%%a222""AA?"#r#   c                   H    e Zd Zd
dZed             Zd Zd ZddZdd	Z	dS )r6   r   c                    t          j        d|d|           || _        || _        || _        d| _        d| _        |r|                                                                }|t          |          k    r6t          |          }|dk    r|d| dz            }|t          |          z   }n|                                }| j        j        j        }t          |          }||}nt          j                            |          }t#          |||          \  }	}
|
d| _        n|
g| _        |	|rt%          ||d         d	           n|	|z   }|| _        dS )
a  
        An implementation similar to ``__import__``. Use `follow`
        to actually follow the imports.

        *level* specifies whether to use absolute or relative imports. 0 (the
        default) means only perform absolute imports. Positive values for level
        indicate the number of parent directories to search relative to the
        directory of the module calling ``__import__()`` (see PEP 328 for the
        details).

        :param import_path: List of namespaces (strings or Names).
        zimport  NTrW   Fr   z3Attempted relative import beyond top-level package.)re   )r   speed_inference_stater?   _module_context_fixed_sys_path_infer_possible	get_valuepy__package__r[   r]   
py__file__projectrA   listrj   rk   ru   rf   r>   )r    r7   r>   r<   r?   baserA   rn   ro   base_import_pathbase_directorys              r!   r"   zImporter.__init__   s    	kkk>>BCCC /
-## /	A!++--;;==D D		! $AT{{19 -%!,D"U;%7%77%0022#4<A";// 6
 !-II " 5 5I3M )U4 40 . " < ,1D((,:+;D(# A" "*KN$Y   
 #3["@K&r#   c                 >    t          d | j        D                       S )z:Returns the import path as pure strings instead of `Name`.c              3   \   K   | ]'}t          |t          j                  r|j        n|V  (d S r   
isinstancer   Namerd   )rG   rN   s     r!   rL   z,Importer._str_import_path.<locals>.<genexpr>   sM       
 
 %T4955?DJJ4
 
 
 
 
 
r#   )r]   r>   r   s    r!   _str_import_pathzImporter._str_import_path   s5      
 
(
 
 
 
 
 	
r#   c                     | j         | j         S | j                            |           d t          j        | j                  D             z   S )N)add_init_pathsc                 ,    g | ]}t          |          S r-   )str)rG   ps     r!   rI   z9Importer._sys_path_with_modifications.<locals>.<listcomp>   s+       A  r#   )r|   rz   get_sys_pathr
   check_sys_path_modificationsr{   )r    is_completions     r!   _sys_path_with_modificationsz%Importer._sys_path_with_modifications   sf     	('' !..->O.PP 89MNN  			
r#   c                 $   | j         sg| j        rYddlm} t          j                            | j        d                   f} || j        || j                  }t          |h          S t          S | j
        st          S | j        j                            | j                  }|t          |h          S | j        j                            | j                  }||S |                     d          }t!          | j        | j         || j                  S )Nr   ImplicitNamespaceValuer%   pathsFr   )r>   r|   jedi.inference.value.namespacer   rj   rA   rl   rz   r   r   r}   stub_module_cacher)   r   module_cacher   import_module_by_namesr{   )r    r   r>   ns
from_cacher
   s         r!   r8   zImporter.follow   s4    "	#  &2 RQQQQQ!w//0DQ0GHHJ++)!,.  
  ~~%# 	 *<@@AVWW
 	*ZL)))*7;;D<QRR
 	4454II%!4#3Xt?S
 
 	
r#   Nc           
          ||                      d          }n|}t          t          | j        | j        ||t
          nt          |du o|du                     S )z
        Get the names of all modules in the search_path. This means file names
        and not names defined in the files.
        NTr   )
module_clsadd_builtin_modules)r   r   iter_module_namesrz   r{   r   r   )r    search_path	in_moduler
   s       r!   _get_module_nameszImporter._get_module_names,  sx    
  	#88t8LLHH"H%!4#7%.IzzM +t 3 I	T8I
 
 
   	r#   Fc                    | j         sg S g }| j        r| j        dk    r|                                 D ]]}|j        }|                    d          r?|t          d          d         }|                    t          | j	        |                     ^| 
                    d          D ]Z}t          j                            |d          }t          j                            |          r||                     |g          z  }[|                                 }	|	D ]I}
|
j        dvr|
                                s)||
                                                                z  }J|sGdd	lm} |	 ||	          z  }|D ]0}|                                D ]}||                                z  }1n<| j        r||                     | j                  z  }n||                                 z  }|S )
z
        :param only_modules: Indicates wheter it's possible to import a
            definition that is not defined in a module.
        )flaskextflask_NTr   flaskext)module	namespacer   )convert_values)r}   r>   r   r   string_name
startswithr[   appendr   r{   r   rj   rA   joinisdirr8   api_typeis_compiledsub_modules_dictr@   !jedi.inference.gradual.conversionr   get_filtersr?   r|   )r    r7   only_modulesrR   modmodnameextnamedirr   r@   rd   r   both_valuesrH   filters                  r!   completion_nameszImporter.completion_names;  s(   
 # 	I %	2$(88 D1133 P PC!oG))(33 P")#h--.."9Z0Dg%N%NOOO<<4<PP D DC!w||C<<Hw}}X.. D!7!7
!C!CC[[]]F ? ?>)@@ ((** ?U3355<<>>>E 1LLLLLL$~~f'='==$ 1 1A"#--// 1 101 z 2//0DEEE //111r#   )r   rh   )F)
r*   r+   r,   r"   propertyr   r   r8   r   r   r-   r#   r!   r6   r6      s        D' D' D' D'L 
 
 X

 
 
3
 3
 3
j   / / / / / /r#   r6   Tc                 |   	                                   t          d |D                       	d g}t          |          D ]t\  }t          j         	fd|D                       }|sIdd                    	          z   }|t          |||           nt          j        |           t          c S u|S )Nc              3   \   K   | ]'}t          |t          j                  r|j        n|V  (d S r   r   )rG   rp   s     r!   rL   z)import_module_by_names.<locals>.<genexpr>r  sM         a++2     r#   c           	      N    g | ]!}t          d dz            |          "S )NrW   )prefer_stubs)import_module)rG   parent_module_valuerp   r7   r   str_import_namesr
   s     r!   rI   z*import_module_by_names.<locals>.<listcomp>x  sV     (
 (
 (
 &  !A#&#)  (
 (
 (
r#   zNo module named .)
r   r]   	enumerater   	from_setsr   rf   r   rc   r   )
r7   import_namesr
   r<   r   r&   rN   re   rp   r   s
   ` ` `   @@r!   r   r   m  s%    2"//11       I\**  4& (
 (
 (
 (
 (
 (
 (
 (
 *3(
 (
 (
  	  	(3884D+E+EEG '>49999g&&&	 r#   c                 <   |d         t           j        v r*t          | ||          }|t          S t	          |g          S d                    |          }|1| j                            |d         ||d          \  }}|t          S nM|                                }|t          S | j                            |d         ||d          \  }}|t          S t          |t                    r?dd	lm}	  |	| t          |j                            d                    |j        
          }n0|t          | ||          }|t          S nt#          | |||          }|t%          j        d|d         |           nt%          j        d|||           t	          |g          S )zC
    This method is very similar to importlib's `_gcd_import`.
    r   Nr   rX   T)string	full_namer
   is_global_searchF)r   rA   r   r   r   r   r   
is_packagezglobal search_module %s: %sz search_module %s in paths %s: %s)r   auto_import_modules_load_builtin_moduler   r   r   compiled_subprocessget_module_info
py__path__r   r   r   r   r]   rN   splitr   _load_python_moduler   r9   )
r7   r   r   r
   r   module_namefile_io_or_nsis_pkgr   r   s
             r!   r   r     s    A(66 "%o|XNN 	!!!((<((K  !0 C S S#!!	 !T !
 !
v  		 $..00 	 / C S S#!"	 !T !
 !
v  	-00 
IIIIII''}177<<==%
 
 

 
 	
%o|XNN 		 %]%
 
 
  R	/b1A6JJJJ	4k5&QQQVHr#   Fc           
          |                      |dt          j        t          j                  }ddlm}  || |||t          | j        |j                  |          S )NT)file_iocache
diff_cache
cache_pathr   )ModuleValue)r   r%   
code_linesr   )	parser   fast_parsercache_directoryjedi.inference.valuer   r	   grammarrA   )r7   r   r   r   module_noder   s         r!   r   r     s{    !'''+	 (  K 100000;!()@',OO   r#   c                     | j         }||                                 }|j        s#|                    |           fd|D             }d                    |          }|J t          j        | ||          }|d S |S )Nc                     g | ]}|v |	S r-   r-   )rG   r   
safe_pathss     r!   rI   z(_load_builtin_module.<locals>.<listcomp>  s"    ;;;!1
?;A;;;r#   r   )dotted_namer
   )r   r   _load_unsafe_extensions_get_base_sys_pathr   r   load_module)r7   r   r
   r   r   r   r   s         @r!   r   r     s    %G 2"//11* <//@@
;;;;x;;;((<((K"""!/{U]^^^F  tMr#   c           	      $   t          |j                  }|-|                                 }t          j        ||          \  }}nt          |t                    sJ |j        dk    }|r|                                }|j        	                    d          rt          |j        dd                   }|j        dk    r|                    d          }n|                    |d         dz             }	 t          | |||	          }	t          |	g          }
n# t          $ r
 t           }
Y nw xY wt#          | | j        |
t'          | |          ||          S t)          | |||
          }| j                            |t          |g                     |S )z
    This should pretty much only be used for get_modules_containing_name. It's
    here to ensure that a random path is still properly loaded into the Jedi
    module structure.
    Nz.pyiz-stubsiz__init__.pyiz__init__.pyrX   z.py)r   r   )r   rA   r   r
   transform_path_to_dottedr   boolsuffixget_parent_folderendswithr   rN   get_file_ioload_module_from_pathr   FileNotFoundErrorr   r   latest_grammarr   r   r   r'   )r7   r   r   r   rA   
e_sys_pathis_stub	folder_iopython_file_iovr@   r   s               r!   r   r     s    D ,$1133
#+#DZQU#V#V jj*d+++++kV#G --//	>""8,, 	6 !455I9& 	M&22=AANN&22<3Ce3KLLN	%  A qc]]FF  	 	 	FFF	 "_;Vow77,
 
 	

 %W%!
 
 

 	$((x7I7IJJJs   2#D D*)D*c                     t          j        |                                 t          |j                            \  }}ddlm}  || ||j        g          S )Nr   r   )r
   r   r   r   rA   r   r   )r7   r   r   r   r   s        r!   load_namespace_from_pathr    sd    '@$$&&Y^   L* FEEEEE!!/<).AQRRRr#   c                    t          j        |d          }|d}t          |j                  D ]"\  }}|j        |j        k    r n|dk    r|dz   }#|j        |d          }|d                                         j        }|dv r[|dk    }t          j        |||          \  }	}
t          | j
        |
|                                 |	                                          S d S )N
error_noder   ;rW   )fromimportr  )is_import_from
until_node)r   r   r   children	start_posget_first_leafrd   r   parse_dotted_namesr6   r7   r3   r8   )r:   rN   r  start_indexindexrK   nodes
first_namer  r?   rR   s              r!   %follow_error_node_imports_if_possibler  '  s   %dL99J \ !*"566 	( 	(HE1{T^+ Cx (#ai#KLL11X,,..4
 ++ 	\'61N"5-  LE5
 '0H0H0J0JES SSYSYS[S[\4r#   c              #      K   |r*| j                                         D ]} |||          V  | j                             |          D ]} |||          V  dS )zx
    Get the names of all modules in the search_path. This means file names
    and not names defined in the files.
    N)r   get_builtin_module_namesr   )r7   r<   r   r   r   rN   s         r!   r   r   D  s        3#7PPRR 	3 	3D*^T2222223EEkRR / /j....../ /r#   )NNT)NFrh   )6__doc__rj   pathlibr   parso.pythonr   
parso.treer   jedir   r   jedi.file_ior   jedi.parser_utilsr	   jedi.inferencer
   r   r   r   jedi.inference.utilsr   jedi.inference.cacher   jedi.inference.namesr   r   jedi.inference.base_valuer   r   jedi.inference.gradual.typeshedr   r   r   ,jedi.inference.compiled.subprocess.functionsr   jedi.pluginsr   r   rC   rS   r4   rf   ru   r6   r   decorater   r   r   r   r  r  r   r-   r#   r!   <module>r%     sC  	 	 
			             & & & & & &             ! ! ! ! ! ! 3 3 3 3 3 3 # # # # # # " " " " " " # # # # # # # # # # # # & & & & & & = = = = = = : : : : : : : : 9 9 9 9 9 9 9 9* * * * * * * * * * G G G G G G ' ' ' ' ' '	2 	2 	2 	2 	2 	2 	2 	2 i000  10* b)))' ' *)'2V V V,@ @ @# # #6Q Q Q Q Q Q Q Qh DH=A   < ; ;  ;~ 7<   &   $+ + + +\S S S  < ",/ / / / / /r#   