]>
crepu.dev Git - config.git/blob - djavu-asus/elpy/rpc-venv/lib/python3.11/site-packages/jedi/inference/gradual/conversion.py
2 from jedi
.inference
.base_value
import ValueSet
, \
4 from jedi
.inference
.utils
import to_list
5 from jedi
.inference
.gradual
.stub_value
import StubModuleValue
6 from jedi
.inference
.gradual
.typeshed
import try_to_load_stub_cached
7 from jedi
.inference
.value
.decorator
import Decoratee
10 def _stub_to_python_value_set(stub_value
, ignore_compiled
=False):
11 stub_module_context
= stub_value
.get_root_context()
12 if not stub_module_context
.is_stub():
13 return ValueSet([stub_value
])
16 if isinstance(stub_value
, Decoratee
):
17 decorates
= stub_value
._original
_value
19 was_instance
= stub_value
.is_instance()
21 arguments
= getattr(stub_value
, '_arguments', None)
22 stub_value
= stub_value
.py__class__()
24 qualified_names
= stub_value
.get_qualified_names()
25 if qualified_names
is None:
28 was_bound_method
= stub_value
.is_bound_method()
30 # Infer the object first. We can infer the method later.
31 method_name
= qualified_names
[-1]
32 qualified_names
= qualified_names
[:-1]
36 values
= _infer_from_stub(stub_module_context
, qualified_names
, ignore_compiled
)
38 values
= ValueSet
.from_sets(
39 c
.execute_with_values() if arguments
is None else c
.execute(arguments
)
44 # Now that the instance has been properly created, we can simply get
46 values
= values
.py__getattribute__(method_name
)
47 if decorates
is not None:
48 values
= ValueSet(Decoratee(v
, decorates
) for v
in values
)
52 def _infer_from_stub(stub_module_context
, qualified_names
, ignore_compiled
):
53 from jedi
.inference
.compiled
.mixed
import MixedObject
54 stub_module
= stub_module_context
.get_value()
55 assert isinstance(stub_module
, (StubModuleValue
, MixedObject
)), stub_module_context
56 non_stubs
= stub_module
.non_stub_value_set
58 non_stubs
= non_stubs
.filter(lambda c
: not c
.is_compiled())
59 for name
in qualified_names
:
60 non_stubs
= non_stubs
.py__getattribute__(name
)
65 def _try_stub_to_python_names(names
, prefer_stub_to_compiled
=False):
67 module_context
= name
.get_root_context()
68 if not module_context
.is_stub():
72 if name
.api_type
== 'module':
73 values
= convert_values(name
.infer(), ignore_compiled
=prefer_stub_to_compiled
)
79 v
= name
.get_defining_qualified_value()
81 converted
= _stub_to_python_value_set(v
, ignore_compiled
=prefer_stub_to_compiled
)
83 converted_names
= converted
.goto(name
.get_public_name())
85 for n
in converted_names
:
86 if n
.get_root_context().is_stub():
87 # If it's a stub again, it means we're going in
88 # a circle. Probably some imports make it a
97 def _load_stub_module(module
):
100 return try_to_load_stub_cached(
101 module
.inference_state
,
102 import_names
=module
.string_names
,
103 python_value_set
=ValueSet([module
]),
104 parent_module_value
=None,
105 sys_path
=module
.inference_state
.get_sys_path(),
110 def _python_to_stub_names(names
, fallback_to_python
=False):
112 module_context
= name
.get_root_context()
113 if module_context
.is_stub():
117 if name
.api_type
== 'module':
119 for n
in name
.goto():
120 if n
.api_type
== 'module':
121 values
= convert_values(n
.infer(), only_stubs
=True)
126 for x
in _python_to_stub_names([n
], fallback_to_python
=fallback_to_python
):
132 v
= name
.get_defining_qualified_value()
134 converted
= to_stub(v
)
136 converted_names
= converted
.goto(name
.get_public_name())
138 yield from converted_names
140 if fallback_to_python
:
141 # This is the part where if we haven't found anything, just return
146 def convert_names(names
, only_stubs
=False, prefer_stubs
=False, prefer_stub_to_compiled
=True):
147 if only_stubs
and prefer_stubs
:
148 raise ValueError("You cannot use both of only_stubs and prefer_stubs.")
150 with debug
.increase_indent_cm('convert names'):
151 if only_stubs
or prefer_stubs
:
152 return _python_to_stub_names(names
, fallback_to_python
=prefer_stubs
)
154 return _try_stub_to_python_names(
155 names
, prefer_stub_to_compiled
=prefer_stub_to_compiled
)
158 def convert_values(values
, only_stubs
=False, prefer_stubs
=False, ignore_compiled
=True):
159 assert not (only_stubs
and prefer_stubs
)
160 with debug
.increase_indent_cm('convert values'):
161 if only_stubs
or prefer_stubs
:
162 return ValueSet
.from_sets(
164 or (ValueSet({value}
) if prefer_stubs
else NO_VALUES
)
168 return ValueSet
.from_sets(
169 _stub_to_python_value_set(stub_value
, ignore_compiled
=ignore_compiled
)
170 or ValueSet({stub_value}
)
171 for stub_value
in values
177 return ValueSet([value
])
179 was_instance
= value
.is_instance()
181 value
= value
.py__class__()
183 qualified_names
= value
.get_qualified_names()
184 stub_module
= _load_stub_module(value
.get_root_context().get_value())
185 if stub_module
is None or qualified_names
is None:
188 was_bound_method
= value
.is_bound_method()
190 # Infer the object first. We can infer the method later.
191 method_name
= qualified_names
[-1]
192 qualified_names
= qualified_names
[:-1]
195 stub_values
= ValueSet([stub_module
])
196 for name
in qualified_names
:
197 stub_values
= stub_values
.py__getattribute__(name
)
200 stub_values
= ValueSet
.from_sets(
201 c
.execute_with_values()
206 # Now that the instance has been properly created, we can simply get
208 stub_values
= stub_values
.py__getattribute__(method_name
)