TL i~dZddlZddlmZddlZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd lmZdd lmZdd lmZdd lmZddlmZmZddlmZddlmZddlmZmZddlm Z ddlm!Z!ddl"m#Z#m$Z$ddl%m&Z&ddl%m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9m:Z:dd l;mZ>ej~d"Gd#d$Z@Gd%d&e@ZAd'ZBe jd(d(d(fd)ZDy)*aQ The API basically only provides one class. You can create a :class:`Script` and use its methods. Additionally you can add a debug function with :func:`set_debug_function`. Alternatively, if you don't need a custom function and are happy with printing debug messages to stdout, simply call :func:`set_debug_function` without arguments. N)Path)tree)get_executable_nodes)debug)settings)cache)KnownContentFileIO)classes) interpreter)helpers)validate_line_column) Completionsearch_in_module) KeywordName)InterpreterEnvironment)get_default_projectProject)parso_to_jedi_errors) refactoring)extract_functionextract_variable)InferenceState)imports)find_references)try_iter_content)infer_call_of_leaf)transform_path_to_dotted)tree_name_to_values) ModuleValue)ValueSet)unpack_tuple_to_dict) convert_namesconvert_values)load_proper_stub_module)to_listi cheZdZdZd dddddZej dZdZdZ e d!dd d Z e d!ddd d Z e d!ddddd dZ dddZed"dZdZe d!dZe d!dZe d!dZe d!dZdZdZdZd#dZd!dZe ddddZe ddddZd!dZy)$ScriptaK A Script is the base for completions, goto or whatever you want to do with Jedi. The counter part of this class is :class:`Interpreter`, which works with actual dictionaries and can work with a REPL. This class should be used when a user edits code in an editor. You can either use the ``code`` parameter or ``path`` to read a file. Usually you're going to want to use both of them (in an editor). The Script's ``sys.path`` is very customizable: - If `project` is provided with a ``sys_path``, that is going to be used. - If `environment` is provided, its ``sys.path`` will be used (see :func:`Environment.get_sys_path `); - Otherwise ``sys.path`` will match that of the default environment of Jedi, which typically matches the sys path that was used at the time when Jedi was imported. Most methods have a ``line`` and a ``column`` parameter. Lines in Jedi are always 1-based and columns are always zero based. To avoid repetition they are not always documented. You can omit both line and column. Jedi will then just do whatever action you are calling at the end of the file. If you provide only the line, just will complete at the end of that line. .. warning:: By default :attr:`jedi.settings.fast_parser` is enabled, which means that parso reuses modules (i.e. they are not immutable). With this setting Jedi is **not thread safe** and it is also not safe to use multiple :class:`.Script` instances and its definitions at the same time. If you are a normal plugin developer this should not be an issue. It is an issue for people that do more complex stuff with Jedi. This is purely a performance optimization and works pretty well for all typical usages, however consider to turn the setting off if it causes you problems. See also `this discussion `_. :param code: The source code of the current file, separated by newlines. :type code: str :param path: The path of the file in the file system, or ``''`` if it hasn't been saved yet. :type path: str or pathlib.Path or None :param Environment environment: Provide a predefined :ref:`Environment ` to work with a specific Python version or virtualenv. :param Project project: Provide a :class:`.Project` to make sure finding references works well, because the right folder is searched. There are also ways to modify the sys path and other things. N)path environmentprojectc>||_t|tr t|}|r|j nd|_|2| t dt|d5}|j}ddd|-t|j dn|j j}t|||j |_ tjd|jj||j |xr|j dk(dt"j$t"j&\|_}tjdt+j,|d |_||_t3j4tj6y#1swY,xYw) Nz)Must provide at least one of code or pathrb)r) script_pathinit.pyiF)coder(use_latest_grammarr diff_cache cache_pathparsedT)keepends) _orig_path isinstancestrrabsoluter( ValueErroropenreadrparentr_inference_staterspeedparse_and_get_codesuffixr fast_parsercache_directory _module_nodeparso split_lines _code_lines_coderclear_time_caches reset_time)selfr0r(r)r*fs W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/jedi/api/__init__.py__init__zScript.__init__dsS dC :D'+DMMO <| !LMMdD! Qvvx  ?)$))2C$IYIYZG . $))!   F"&"7"7"J"J#= v(=++// #K# 4  H ,,TDA  ! 1  s FFcd}d}|j9t|jjd|j\}}||}|}|jd}n t |j|j }|jT|jj dk(r;t|j|jj|||j}||S|d}t|j|j|||j|}|ddvr0|jjj|t|g|S)NF)add_parent_pathsr/__main__)file_io string_names code_lines is_packager)builtinstyping)r(rr> get_sys_pathr rHrAr$latest_grammarrDrrG module_cacheaddr )rKnamesrV import_namesis_prS stub_modulemodules rM _get_modulezScript._get_modules@ 99 !9%%22E2J " L$'$! 99 G(DJJ?G 99 TYY%5%5%?1%%%%44!! K&"" =!E  ! !4#4#4''!   81 1  ! ! . . 2 25(F8:L M c>|jjSN)rb as_contextrKs rM_get_module_contextzScript._get_module_contexts!,,..rccd|jjdt|jd|jj dS)N) __class____name__reprr6r>r)rgs rM__repr__zScript.__repr__s4 NN # #  !  ! ! - -  rcFfuzzyc *|jjtjd5t |j|j |j ||f|j|}|jcdddS#1swYyxYw)a+ Completes objects under the cursor. Those objects contain information about the completions, more than just names. :param fuzzy: Default False. Will return fuzzy completions, which means that e.g. ``ooa`` will match ``foobar``. :return: Completion objects, sorted by name. Normal names appear before "private" names that start with ``_`` and those appear before magic methods and name mangled names that start with ``__``. :rtype: list of :class:`.Completion` completerqN) r>reset_recursion_limitationsrincrease_indent_cmrrhrGget_signaturesrt)rKlinecolumnrr completions rMrtzScript.completes~ 99;  % %j 1 )#%%t'?'?'A4CSCSv 3 35J&&(  ) ) )s AB  B only_stubs prefer_stubsc|jj||f}|jj|}||jj |}||j dk(rgS|j ||fk(rH|j dk(r9|j}|j|j k(r|j dvr|}|jj|}tj|j||} t| ||} | D cgc],} tj|j| j .} } tj"t%| Scc} w)a Return the definitions of under the cursor. It is basically a wrapper around Jedi's type inference. This method follows complicated paths and returns the end, not the first definition. The big difference between :meth:`goto` and :meth:`infer` is that :meth:`goto` doesn't follow imports and statements. Multiple objects may be returned, because depending on an option you can have two different versions of a function. :param only_stubs: Only return stubs for this method. :param prefer_stubs: Prefer stubs to Python objects for this method. :rtype: list of :class:`.Name` stringoperator)numberrkeywordr{)r>rurDget_name_of_positionget_leaf_for_positiontypeend_pos get_next_leaf start_posrhcreate_contextr inferr#r Namenamesorted_definitionsset) rKrxryr|r}posleafnext_contextvaluescdefss rMrz Script.infers=" 99;Fl  55c: <$$::3?D|tyyH4 ||f~-$))z2I**,??dll2!JJ*II D**,;;DAt44gtD !%  FLL T22AFF;LL))#d)44 Ms1E&)follow_importsfollow_builtin_importsr|r}cz|jj|jj||f}||j ||||S|j j |}g} |jjr|jjrv|jj} |j j| } | j} t| | D]!} | j|j } | s!n| st#|j} |rt%j&| |} t)| ||} t+| Dcgc]"}t-j.|j|$}}t#t+t%j0|Scc}w)a~ Goes to the name that defined the object under the cursor. Optionally you can follow imports. Multiple objects may be returned, depending on an if you can have two different versions of a function. :param follow_imports: The method will follow imports. :param follow_builtin_imports: If ``follow_imports`` is True will try to look up names in builtins (i.e. compiled or extension modules). :param only_stubs: Only return stubs for this method. :param prefer_stubs: Prefer stubs to Python objects for this method. :rtype: list of :class:`.Name` r{)r>rurDrrrh create_name tree_name is_definitionparent_contextis_class tree_node create_value py__mro__nextgotovaluelistr filter_follow_importsr"rr rr)rKrxryrrr|r}rrr] class_node class_valuemroclsdrs rMrz Script.gotos 99;%%::D&>J  ::dFzP\:] ]'')55i@ >> ' ' )d.A.A.J.J.L,,66J224AA*MK'')C I 1  %E 11%9OPE !%  ADE K1 T22A6KKC22489::Ls('F8 all_scopesc(|j||S)a Searches a name in the current file. For a description of how the search string should look like, please have a look at :meth:`.Project.search`. :param bool all_scopes: Default False; searches not only for definitions on the top level of a module level, but also in functions and classes. :yields: :class:`.Name` r _search_func)rKrrs rMsearchz Script.search:s  J ??rcc |j|}tj|\}}t|j|j |||||S)Nr)r] wanted_type wanted_namesrtrr)_namesr split_search_stringrr>rh)rKrrrtrrr]rrs rMrzScript._search_funcGsX z 2$+$?$?$G! \  ! !  $ $ &#%  rcc ,|j|fddi|S)a Like :meth:`.Script.search`, but completes that string. If you want to have all possible definitions in a file you can also provide an empty string. :param bool all_scopes: Default False; searches not only for definitions on the top level of a module level, but also in functions and classes. :param fuzzy: Default False. Will return fuzzy completions, which means that e.g. ``ooa`` will match ``foobar``. :yields: :class:`.Completion` rtTr)rKrkwargss rMcomplete_searchzScript.complete_searchUs!!t  A$A&AArccjjj||d}|r|Sjj ||fMj ||fk(r<j dk(r-j}||jj k(r|\j dvrNfd}|rAtjj}tjj|gSgS)a* Used to display a help window to users. Uses :meth:`.Script.goto` and returns additional definitions for keywords and operators. Typically you will want to display :meth:`.BaseName.docstring` to the user for all the returned definitions. The additional definitions are ``Name(...).type == 'keyword'``. These definitions do not have a lot of value apart from their docstring attribute, which contains the output of Python's :func:`help` function. :rtype: list of :class:`.Name` T)rnewline)rr error_leafcjdvr4jjdk(ryjjdk(ryjj}|j j j}j|vS)N)()[]trailerFatom)rr=rr>grammar _pgen_grammarreserved_syntax_stringskeys)rreservedrrKs rM need_pydoczScript.help..need_pydocsv::!55{{''94${{''61$//77#00HHMMOzzX--rc) r>rurrDrrrrrrrr r)rKrxry definitionsrrrrs` @rMhelpz Script.helpds 99;iifTiB    66f~F   v >499PYCY&&(E U__ %D   -R R .|"4#8#8$**E T%:%:DABB rcc \jjdfd }|di|S)a Lists all references of a variable in a project. Since this can be quite hard to do for Jedi, if it is too complicated, Jedi will stop searching. :param include_builtins: Default ``True``. If ``False``, checks if a definition is a builtin (e.g. ``sys``) and in that case does not return it. :param scope: Default ``'project'``. If ``'file'``, include references in the current module only. :rtype: list of :class:`.Name` c|dvr td jjf}|gSt j ||dk(}|Dcgc]"}t j j|$}}|r|dk(r |Dcgc]}|jr|}}tj|Scc}wcc}w)N)r*filez0Only the scopes "file" and "project" are allowedr) r:rDrrrhr rr>in_builtin_moduler r) include_builtinsscoperr]nrrryrxrKs rM _referencesz*Script.get_references.._referencess// !STT))>>f~NI  #D$<$<$> 5TZ?[EKPQa7<<(=(=qAQKQ#u*5SQQ=P=P=RqS S--k: :RSs'B9B>B>)Tr*)r>ru)rKrxryrrs``` rMget_referenceszScript.get_referencess, 99; ;$V$$rcc|jj||f}tj|j|}|gS|j j |j}tj|j||j|j|}tjd|jDcgc]#}tj|j||%c}Scc}w)aZ Return the function object of the call under the cursor. E.g. if the cursor is here:: abs(# <-- cursor is here This would return the ``abs`` function. On the other hand:: abs()# <-- cursor is here This would return an empty list.. :rtype: list of :class:`.Signature` zfunc_call followed)r>rur get_signature_detailsrDrhr bracket_leafcache_signaturesrGrr?rwr Signature)rKrxryr call_detailsrr signatures rMrwzScript.get_signaturess" 99;Fl44T5F5FL  I**,;;L_namerget_definitionr=) rKrxryrr previous_leafmodule_contextrr definitionrrrs rM get_contextzScript.get_contextsyVn  66sT6R >>C 499 #; 224M($113  y* = =Q[[3J!**R.2J2JJ %11!4??AG$33D9Gll",,Gll"\\$"7"7F oo)##DI$!002??1%.$**,Joo)rccd|j_|jg|j_|j } t |jD]n}|j |}|jdvr&t|j||jdn t|tjrjt|j}|jr#|td|j!Dz}|D]}t#j$||n|jdk(r6|j'|}|jdddD]}t)|||nC|jdk(r|jj+||}n t-||}t/||jj1q|jj2D cgc]} |j4| j4k(s|  } } t7t| d d |j_Scc} w#d |j_wxYw) NT)rrrc3&K|] }|d yw)rNr).0r(s rM z#Script._analysis.. s+RDH+Rs expr_stmtrrc|jSre)rxxs rMz"Script._analysis..s !&&rckeyF)r> is_analysisrDanalysis_modulesrhrrrrrr7rImportrget_defined_names is_nested get_pathsr infer_import infer_noder!rrrruanalysisr(sorted) rKranoderr^rtypestestlistraanas rM _analysiszScript._analysiss,0)262C2C1D.))+ 6,T->->? D //599 77'(=(=w VWHXYdkk2#&t'='='?#@L~~'$+RAQ+R(RR )9,,Wa89YY+-#..t4E$(MM%2a%$8G,WeXFGyyF*#44::7DI1'4@$T*%%AAC- D0#33<<T QVV@S1TCT#c((8905D ! ! -U16D ! ! -s$FH0H+=H+H0+H00Ic |jdi|}|Dcgc]"}tj|j|$c}Scc}w)a Returns names defined in the current file. :param all_scopes: If True lists the names of all scopes instead of only the module namespace. :param definitions: If True lists the names that have been defined by a class, function or a statement (``a = b`` returns ``a``). :param references: If True lists all the names that are not listed by ``definitions=True``. E.g. ``a = b`` returns ``b``. :rtype: list of :class:`.Name` r)rr rr>)rKrr]rs rM get_nameszScript.get_names s< %f%@EF1 T22A6FFFs'AcVt|jj|jS)zm Lists all syntax errors in the current file. :rtype: list of :class:`.SyntaxError` )rr>rrDrgs rMget_syntax_errorszScript.get_syntax_errors/s# $D$9$9$A$A4CTCTUUrcc|jj|j}tj|j |||Dcgc]}|j |}}t|dScc}w)N)rr referencesc|jSre)rrs rMrzScript._names..Ds !++rcr)r>rurhr get_module_namesrDrr)rKrrr rrrs rMrz Script._names7s~ 99;113 00!!%'%    & &t ,  d 566 sA8cl|j||d}tj|j||S)z Renames all references of the variable under the cursor. :param new_name: The variable under the cursor will be renamed to this string. :raises: :exc:`.RefactoringError` :rtype: :class:`.Refactoring` Fr)rrrenamer>)rKrxrynew_namers rMrz Script.renameFs6))$)O !!$"7"7hOOrc) until_line until_columnc||d}n%||}|t|j|dz }||f}t|j|j|j |||f|S)a: Moves an expression to a new statement. For example if you have the cursor on ``foo`` and provide a ``new_name`` called ``bar``:: foo = 3.1 x = int(foo + 1) the code above will become:: foo = 3.1 bar = foo + 1 x = int(bar) :param new_name: The expression under the cursor will be renamed to this string. :param int until_line: The the selection range ends at this line, when omitted, Jedi will be clever and try to define the range itself. :param int until_column: The the selection range ends at this column, when omitted, Jedi will be clever and try to define the range itself. :raises: :exc:`.RefactoringError` :rtype: :class:`.Refactoring` Nr)lenrGrr>r(rDrKrxryrrr until_poss rMrzScript.extract_variableRsw4  ,"6I!! #"4#3#3JN#CD "L0I  ! !499d.?.? tVni  rcc||d}n%||}|t|j|dz }||f}t|j|j|j |||f|S)a Moves an expression to a new function. For example if you have the cursor on ``foo`` and provide a ``new_name`` called ``bar``:: global_var = 3 def x(): foo = 3.1 x = int(foo + 1 + global_var) the code above will become:: global_var = 3 def bar(foo): return int(foo + 1 + global_var) def x(): foo = 3.1 x = bar(foo) :param new_name: The expression under the cursor will be replaced with a function with this name. :param int until_line: The the selection range ends at this line, when omitted, Jedi will be clever and try to define the range itself. :param int until_column: The the selection range ends at this column, when omitted, Jedi will be clever and try to define the range itself. :raises: :exc:`.RefactoringError` :rtype: :class:`.Refactoring` Nr)rrGrr>r(rhrs rMrzScript.extract_functionys{D  ,"6I!! #"4#3#3JN#CD "L0I  ! !499d.F.F.H tVni  rcc|j||dDcgc]}|j}}tj|j|Scc}w)a Inlines a variable under the cursor. This is basically the opposite of extracting a variable. For example with the cursor on bar:: foo = 3.1 bar = foo + 1 x = int(bar) the code above will become:: foo = 3.1 x = int(foo + 1) :raises: :exc:`.RefactoringError` :rtype: :class:`.Refactoring` Tr)rrrinliner>)rKrxryrr]s rMrz Script.inlinesM"#'"5"5dFUY"5"Z[Q[[!!$"7"7??\sA re)NN)FFF)FTF)rn __module__ __qualname____doc__rNrmemoize_methodrbrhrpr rtrrrr%rrrrrwrrr r rrrrrrrcrMr'r'3sl/`$$D$$P ))V/ ))),*5%e*5*5X1;U[`E1;1;f,1 @       B))V%%<#?#?J&&P 6D GV 7 PEIX\$ $ LEIX\, , \@rcr'cLeZdZdZddfd Zej dZxZS) Interpretera  Jedi's API for Python REPLs. Implements all of the methods that are present in :class:`.Script` as well. In addition to completions that normal REPL completion does like ``str.upper``, Jedi also supports code completion based on static code analysis. For example Jedi will complete ``str().upper``. >>> from os.path import join >>> namespace = locals() >>> script = Interpreter('join("").up', [namespace]) >>> print(script.complete()[0].name) upper All keyword arguments are same as the arguments for :class:`.Script`. :param str code: Code to parse. :type namespaces: typing.List[dict] :param namespaces: A list of namespace dictionaries such as the one returned by :func:`globals` and :func:`locals`. N)r*c  |Dcgc] }t|}}|jdd}| t }nt |ts td|t tj}t|(|f||d|||_ tj|j_d|j_ycc}w#t$r tdwxYw)Nz-namespaces must be a non-empty list of dicts.r)z?The environment needs to be an InterpreterEnvironment subclass.)r)r*F)dict Exception TypeErrorgetrr7rrcwdsuperrN namespacesr#allow_unsafe_interpreter_executionsr>allow_unsafe_executionsdo_dynamic_params_search)rKr0r*r*kwdsrr)rms rMrNzInterpreter.__init__s M+56a$q'6J6hh}d3  02Kk+AB abb ?dhhj)G P;P4P$  8 8 5:?657 MKL L MsCB?C?CCc|jd}n t|j|j}t|j|j |d|j }tj||jS)NrQ)rSrTrU) r(r rHrr>rDrGr MixedModuleContextr*)rKrStree_module_values rMrhzInterpreter._get_module_contextso 99 G(DJJ?G'  ! !4#4#4&''   --  OO  rc) rnrrrrNrr rh __classcell__)rms@rMr"r"s,.59?<   rcr"cd|D]+}d|z}t|jdt|-y)z Preloading modules tells Jedi to load a module now, instead of lazy parsing of modules. This can be useful for IDEs, to control which modules to load on startup. :param modules: different module names, list of string. zimport %s as x; x.rN)r'rtr)modulesmss rMpreload_moduler7s5& 1 $q 1c!f%&rcTc\|t_|t_|t_|t_y)z Define a callback debug function to get all the debug messages. If you don't specify any arguments, debug messages will be printed to stdout. :param func_cb: The callback function for debug messages. N)rdebug_functionenable_warning enable_notice enable_speed)func_cbwarningsnoticesr?s rMset_debug_functionr@s$#E#E!EErc)ErsyspathlibrrE parso.pythonrjedi.parser_utilsrjedirrr jedi.file_ior jedi.apir r r jedi.api.helpersr jedi.api.completionrrjedi.api.keywordsrjedi.api.environmentrjedi.api.projectrrjedi.api.errorsrrjedi.api.refactoring.extractrrjedi.inferencerrjedi.inference.referencesrjedi.inference.argumentsrjedi.inference.helpersrjedi.inference.sys_pathrjedi.inference.syntax_treerjedi.inference.valuerjedi.inference.base_valuer jedi.inference.value.iterabler!!jedi.inference.gradual.conversionr"r#jedi.inference.gradual.utilsr$jedi.inference.utilsr%setrecursionlimitr'r"r7print_to_stdoutr@rrcrMr]s  2+ 1<)790 K)"555<:,.>K@(dG @G @TE &E P & %44t#4 rc