uBFjmCddlmZddlmZddlmZddlmZmZddl m Z ddl m Z m Z mZddlmZmZddlmZmZdd lmZdd lmZdd lmZGd d ZGddeZGddZGddeeZGddeeZGddeeZGddeeZ GddeeZ!GddeZ"Gdde"Z#d Z$d!Z%d"S)#)abstractmethod)contextmanager)Path)OptionalAny)Name)ParserTreeFilter MergedFilterGlobalNameFilter)AnonymousParamNameTreeNameDefinition) NO_VALUESValueSet)get_parent_scope)debug) parser_utilsceZdZUeed<eed<dZeddZdZ ddZ d Z d Z d Z d Z d ZdZdZdZdZedZdZedZdZdZedZdS)AbstractContext tree_nodeparent_contextc"||_i|_dSN)inference_statepredefined_names)selfrs DC:\PYTHON\_runtimes\venv\Lib\site-packages\jedi/inference/context.py__init__zAbstractContext.__init__s. "NctrNotImplementedErrorruntil_position origin_scopes r get_filterszAbstractContext.get_filters!!rcddlm}t|t|tr|nd|}|||}t jd||||S)Nr)finderzcontext.goto %s in (%s): %s)jedi.inferencer(_get_global_filters_for_name isinstancer filter_namerdbg)r name_or_strpositionr(filtersnamess rgotozAbstractContext.gotosp)))))). K!>!>H++D(  ""7K88 /dEJJJ rTc ||}|||}t|tr|jn|}d}|jr|t|trg|}|ct j|sO|j}|jdvr) |j|} | |} | }n&#t$rYKwxYw|t j|O|9|r7ddl m } | ||j |} | | jurt} n!|} nt!jd|D} |s<| s:|r8t|tr#ddl m}d|z}||d ||t)jd || | r| S||||S) zZ :param position: Position of the last statement -> tuple of line, column N)if_stmtfor_stmtcomp_for sync_comp_forr) flow_analysis)context value_scopenodec3>K|]}|VdSr)infer).0names r z5AbstractContext.py__getattribute__..Ns*'G'G 'G'G'G'G'G'Gr)analysisz$NameError: name '%s' is not defined.z name-errorz context.names_to_types: %s -> %s)r2r+rvaluerris_scopeparenttypeKeyErrorr)r8reachability_checkr UNREACHABLErr from_setsrAaddrr-_check_for_additional_knowledge)rr. name_contextr/analysis_errorsr1 string_namefound_predefined_typesr; name_dicttypesr8checkvaluesrAmessages rpy__getattribute__z"AbstractContext.py__getattribute__'s  L +x00+5k4+H+HYk''k "&  Z T%B%B D"<+@+F+F"{9 TTT$($9$$? )+ 627. $!!! ! "<+@+F+F" " -% - 4 4 4 4 4 4!44 N 5E  111"/''G'G'G'G'GGGF OV O O+t,, O333333AKO \<gNNN 4eVDDD  M33KxXXXsB B%$B%c|p|}t|trn|sZ|}|jg}t d|Drt Sddlm} t|d}|||||}||S||vrn)t S)Nc3(K|] }|jdvVdS)r6r7N)rE)r>bs rr@zBAbstractContext._check_for_additional_knowledge..bs*OOq16::OOOOOOrr)check_flow_informationT) include_flows) r+r is_instanceranyrjedi.inference.finderrZr)rr.rLr/ flow_scope base_nodesrZns rrKz/AbstractContext._check_for_additional_knowledge[s#+t k4 ( ( 1I1I1K1K $J&01JOOJOOOOO !  D D D D D D -jMMM **<+6BB=H++ rc@|j}||S|Sr)rget_root_context)rrs rrcz AbstractContext.get_root_contextos',  !K..000rcdSNFrs r is_modulezAbstractContext.is_moduleuurcdSrerfrgs ris_builtins_modulez"AbstractContext.is_builtins_modulexrircdSrerfrgs ris_classzAbstractContext.is_class{rircdSrerfrgs ris_stubzAbstractContext.is_stub~rircdSrerfrgs rr\zAbstractContext.is_instancerircdSrerfrgs r is_compiledzAbstractContext.is_compiledrircdSrerfrgs ris_bound_methodzAbstractContext.is_bound_methodrirctrr rgs r py__name__zAbstractContext.py__name__r&rctrr rgs r get_valuezAbstractContext.get_values!!rcdSrrfrgs rr?zAbstractContext.namestrcdS)Nrfrfrgs rget_qualified_namesz#AbstractContext.get_qualified_namesrrcdS)Nrfrgs r py__doc__zAbstractContext.py__doc__r|rc#BK|j}|||< dV||=dS#||=wxYwr)r)rr_dct predefineds rpredefine_nameszAbstractContext.predefine_namessH* !$ : ' EEE:&&& :& & & & &sNN)NNT)__name__ __module__ __qualname__r__annotations__rrr%r2rUrKrcrhrkrmror\rrrtrvrxpropertyr?r{rrrrfrrrrsNNN###"""^"KO+/2Y2Y2Y2Yh(111 ""^""""X''^'''rrceZdZdZfdZedZedZdZdZ dZ dZ d Z d Z d Zd Zed ZdZdZdZdZxZS) ValueContextzC Should be defined, otherwise the API returns empty types. cbt|j||_dSr)superrr_value)rrB __class__s rrzValueContext.__init__s) ./// rc|jjSr)rrrgs rrzValueContext.tree_nodes {$$rc|jjSr)rrrgs rrzValueContext.parent_contexts {))rc4|jSr)rrhrgs rrhzValueContext.is_module{$$&&&rc,|j|jjkSr)rrbuiltins_modulergs rrkzValueContext.is_builtins_modules{d2BBBrc4|jSr)rrmrgs rrmzValueContext.is_classs{##%%%rc4|jSr)rrorgs rrozValueContext.is_stubs{""$$$rc4|jSr)rr\rgs rr\zValueContext.is_instance{&&(((rc4|jSr)rrrrgs rrrzValueContext.is_compiledrrc4|jSr)rrtrgs rrtzValueContext.is_bound_methods{**,,,rc4|jSr)rrvrgs rrvzValueContext.py__name__{%%'''rc|jjSr)rr?rgs rr?zValueContext.names {rc4|jSr)rr{rgs rr{z ValueContext.get_qualified_namess{..000rc4|jSr)rrrgs rrzValueContext.py__doc__rrc|jSrrrgs rrxzValueContext.get_value {rc0|jjd|jdSN())rrrrgs r__repr__zValueContext.__repr__s>222DKKK@@r)rrr__doc__rrrrrhrkrmror\rrrtrvr?r{rrxr __classcell__rs@rrrsQ%%X%**X*'''CCC&&&%%%))))))---(((  X 111'''AAAAAAArrcdeZdZUeed<eed<eed<eed<eed<eed<dZdZd Zd Zd S) TreeContextMixinrrhrxrrmrc&ddlm}|||S)Nr) infer_node)jedi.inference.syntax_treer)rr;rs rrzTreeContextMixin.infer_nodes&999999z$%%%rc\ddlm}||jkr*|sJ|S||}|jdvr|j||}| rj|j |j}| |j |j |}||||}|S|jdkr||j ||St#d|z)Nr)rB)funcdeflambdef)instance class_contextfunctionclassdefzProbably shouldn't happen: %s)r)rBrrhrxcreate_contextrE FunctionValue from_contextrmr create_valueAnonymousInstancer BoundMethod as_context ClassValuer!)rr;rBrfunc class_valuers rrzTreeContextMixin.create_valuesC(((((( 4> ! !>>## # ##>>## #,,T22 9. . .&33NDIID&&(( ,;HHIabb  22(.*GVV((%"-"8"8":":!) K Y* $ $##D$8.$OO O%&E&LMM Mrcdfd d}|jdvr]|j|jd}j|jkr(j}|jdkr |jks |}|dS) NTc@|jkrS|jdvr'|S|jdvrF|j}j|jdjkr|St||Std|z)N)rrrrXz(There's a scope that was not managed: %s) rrErrrD start_poschildrenCompForContext Exception) scope_node is_nestedrfrom_scope_noder; parent_scopers rrz8TreeContextMixin.create_context..from_scope_nodesT^++ "DDD((44??AAA$AAA!0j>O1P1P!Q!Q>Z%8%<%FFF))%njAAAFSTT Trc |j}tj|r|S|jdvr"|jdjdvr |jdSn*|jdkr|jddD]}|jdvr|cSs)NT)argument testlist_comprXdictorsetmaker)rDrrCrEr)r;ras rrz5TreeContextMixin.create_context..parent_scopes %{(.. %KY"???}Q',0MMM#}Q//NY"222!]1Q3/%%6%BBB#$HHHC %r)rr:param)r)T)rErindexrrDr?)rr;rcolonrDrrs`` @@rrzTreeContextMixin.create_contexts U U U U U U U U U % % %"\$'' ?5 5 5' (;(A(A#(F(FGE~// w..6;$3F3F!-j!9!9JzT::::rc|}|rQ|jdkrF|j|kr;|dd}||}t ||S||}t||S)Nrrr)get_definitionrEr?search_ancestorrr rr )r tree_name definitionrrr9s r create_namezTreeContextMixin.create_name*s--//  :*/W44I9U9U 00IFFG$$W--D%dI66 6)))44G%gy99 9rN) rrrrrrrrrrfrrrrsNNNNNNNNNMMM&&&NNN4$;$;$;L:::::rrceZdZddZdS)FunctionContextNc#2Kt|||VdSN)rr#r$r r"s rr%zFunctionContext.get_filters6s:)%        rrrrrr%rfrrrr5s(      rrcfeZdZdeefdZd dZdZedZ edZ dZ dS) ModuleContextreturnc4|jSrr py__file__rgs rrzModuleContext.py__file__?rrNc#K|j|}t|dtt ||||V|Ed{VdSr)rr%nextr r get_global_filter)rr#r$r0s rr%zModuleContext.get_filtersBs+)),77 Wd #-)     " " $ $      rc t|Sr)r rgs rrzModuleContext.get_global_filterPs%%%rc|jjSrr string_namesrgs rrzModuleContext.string_namesS {''rc|jjSr)r code_linesrgs rrzModuleContext.code_linesWs {%%rc|jS)z This is the only function that converts a context back to a value. This is necessary for stub -> python conversion and vice versa. However this method shouldn't be moved to AbstractContext. rrgs rrxzModuleContext.get_value[s {rr) rrrrrrr%rrrrrxrfrrrr>s(HTN((((    &&&((X(&&X&rrcJeZdZddZdZedZdeefdZ dS)NamespaceContextNc4|jSrrr%r"s rr%zNamespaceContext.get_filterserrc|jSrrrgs rrxzNamespaceContext.get_valuehrrc|jjSrrrgs rrzNamespaceContext.string_nameskrrrc4|jSrrrgs rrzNamespaceContext.py__file__orrr) rrrr%rxrrrrrrfrrrrdsq))))((X((HTN((((((rrceZdZddZddZdS) ClassContextNc#:K|||VdSr)rr"s rr%zClassContext.get_filtersts($$^\BBBBBBBrc&t|||Srrr"s rrzClassContext.get_global_filterws")%    rr)rrrr%rrfrrrrss@CCCC      rrc8eZdZfdZddZdZdZdZxZS)rcpt|j||_||_dSr)rrrrr)rrr6rs rrzCompForContext.__init__s3 7888!,rNc#,Kt|VdSrrr"s rr%zCompForContext.get_filterss t$$$$$$$rcdSrrfrgs rrxzCompForContext.get_valuestrcdS)Nzrfrgs rrvzCompForContext.py__name__s((rc0|jjd|jdSr)rrrrgs rrzCompForContext.__repr__s>222DNNNCCrr) rrrrr%rxrvrrrs@rrrs----- %%%%)))DDDDDDDrrceZdZddZdS)CompiledContextNc4|jSrrr"s rr%zCompiledContext.get_filtersrrrrrfrrrrs())))))rrcFeZdZdZdZedZdeefdZ dS)CompiledModuleContextNc|jSrrrgs rrxzCompiledModuleContext.get_valuerrc|jjSrrrgs rrz"CompiledModuleContext.string_namesrrrc4|jSrrrgs rrz CompiledModuleContext.py__file__rr) rrrrrxrrrrrrfrrr r sbJ((X((HTN((((((rr c |r|ddd}d}|dkr|}|dd}|9|jd}|*||jkr|||jdjkr|j}t|||S)Nrrr)rrrget_global_filters)r9 name_or_noner/ancestorrrs rr*r*s// :yQQ y G#33IzJJH  %b)E#5?(B(B?h1A"1E1O&O&O'1H gx > >>rc#K|}ddlm}|?|||Ed{Vt||tfrd}|j}|?t |jjd}|J|VdS)a Returns all filters in order of priority for name resolution. For global name lookups. The filters will handle name resolution themselves, but here we gather possible filters downwards. >>> from jedi import Script >>> script = Script(''' ... x = ['a', 'b', 'c'] ... def func(): ... y = None ... ''') >>> module_node = script._module_node >>> scope = next(module_node.iter_funcdefs()) >>> scope >>> context = script._get_module_context().create_context(scope) >>> filters = list(get_global_filters(context, (4, 0), None)) First we get the names from the function scope. >>> print(filters[0]) # doctest: +ELLIPSIS MergedFilter(, ) >>> sorted(str(n) for n in filters[0].values()) # doctest: +NORMALIZE_WHITESPACE ['', ''] >>> filters[0]._filters[0]._until_position (4, 0) >>> filters[0]._filters[1]._until_position Then it yields the names from one level "lower". In this example, this is the module scope (including globals). As a side note, you can see, that the position in the filter is None on the globals filter, because there the whole module is searched. >>> list(filters[1].values()) # package modules -> Also empty. [] >>> sorted(name.string_name for name in filters[2].values()) # Module attributes ['__doc__', '__name__', '__package__'] Finally, it yields the builtin filter, if `include_builtin` is true (default). >>> list(filters[3].values()) # doctest: +ELLIPSIS [...] r)BaseFunctionExecutionContextN)r#r$) jedi.inference.value.functionrr%r+rrrrr)r9r#r$ base_contextrrYs rrrs^LJJJJJJ  &&)%'          g r s%%%%%% """"""GGGGGGGG99999999......Q'Q'Q'Q'Q'Q'Q'Q'h6A6A6A6A6A?6A6A6ArT:T:T:T:T:T:T:T:n     &    #####$l###L ( ( ( ( (' ( ( (      #\    DDDDD%DDD&)))))l))) ( ( ( ( (O ( ( (???*@ @ @ @ @ r