1
0
forked from VimPlug/jedi

Merge branch 'master' into relative-import

This commit is contained in:
Dave Halter
2020-12-12 12:15:13 +01:00
24 changed files with 223 additions and 44 deletions

View File

@@ -324,8 +324,7 @@ class CompiledName(AbstractNameDefinition):
self.string_name = name
def py__doc__(self):
value, = self.infer()
return value.py__doc__()
return self.infer_compiled_value().py__doc__()
def _get_qualified_names(self):
parent_qualified_names = self.parent_context.get_qualified_names()
@@ -349,16 +348,12 @@ class CompiledName(AbstractNameDefinition):
@property
def api_type(self):
api = self.infer()
# If we can't find the type, assume it is an instance variable
if not api:
return "instance"
return next(iter(api)).api_type
return self.infer_compiled_value().api_type
@memoize_method
def infer(self):
return ValueSet([self.infer_compiled_value()])
@memoize_method
def infer_compiled_value(self):
return create_from_name(self._inference_state, self._parent_value, self.string_name)

View File

@@ -113,7 +113,7 @@ def _expand_typestr(type_str):
elif type_str.startswith('{'):
node = parse(type_str, version='3.7').children[0]
if node.type == 'atom':
for leaf in node.children[1].children:
for leaf in getattr(node.children[1], "children", []):
if leaf.type == 'number':
if '.' in leaf.value:
yield 'float'

View File

@@ -279,8 +279,8 @@ def _try_to_load_stub_from_file(inference_state, python_value_set, file_io, impo
return None
else:
return create_stub_module(
inference_state, python_value_set, stub_module_node, file_io,
import_names
inference_state, inference_state.latest_grammar, python_value_set,
stub_module_node, file_io, import_names
)
@@ -294,7 +294,8 @@ def parse_stub_module(inference_state, file_io):
)
def create_stub_module(inference_state, python_value_set, stub_module_node, file_io, import_names):
def create_stub_module(inference_state, grammar, python_value_set,
stub_module_node, file_io, import_names):
if import_names == ('typing',):
module_cls = TypingModuleWrapper
else:
@@ -306,7 +307,7 @@ def create_stub_module(inference_state, python_value_set, stub_module_node, file
string_names=import_names,
# The code was loaded with latest_grammar, so use
# that.
code_lines=get_cached_code_lines(inference_state.latest_grammar, file_io.path),
code_lines=get_cached_code_lines(grammar, file_io.path),
is_package=file_name == '__init__.pyi',
)
return stub_module_value

View File

@@ -3,7 +3,7 @@ from pathlib import Path
from jedi.inference.gradual.typeshed import TYPESHED_PATH, create_stub_module
def load_proper_stub_module(inference_state, file_io, import_names, module_node):
def load_proper_stub_module(inference_state, grammar, file_io, import_names, module_node):
"""
This function is given a random .pyi file and should return the proper
module.
@@ -27,7 +27,8 @@ def load_proper_stub_module(inference_state, file_io, import_names, module_node)
actual_value_set = inference_state.import_module(import_names, prefer_stubs=False)
stub = create_stub_module(
inference_state, actual_value_set, module_node, file_io, import_names
inference_state, grammar, actual_value_set,
module_node, file_io, import_names
)
inference_state.stub_module_cache[import_names] = stub
return stub

View File

@@ -533,8 +533,8 @@ def load_module_from_path(inference_state, file_io, import_names=None, is_packag
values = NO_VALUES
return create_stub_module(
inference_state, values, parse_stub_module(inference_state, file_io),
file_io, import_names
inference_state, inference_state.latest_grammar, values,
parse_stub_module(inference_state, file_io), file_io, import_names
)
else:
module = _load_python_module(

View File

@@ -7,6 +7,7 @@ from parso.tree import search_ancestor
from jedi.parser_utils import find_statement_documentation, clean_scope_docstring
from jedi.inference.utils import unite
from jedi.inference.base_value import ValueSet, NO_VALUES
from jedi.inference.cache import inference_state_method_cache
from jedi.inference import docstrings
from jedi.cache import memoize_method
from jedi.inference.helpers import deep_ast_copy, infer_call_of_leaf
@@ -331,6 +332,12 @@ class TreeNameDefinition(AbstractTreeName):
node = node.parent
return indexes
@property
def inference_state(self):
# Used by the cache function below
return self.parent_context.inference_state
@inference_state_method_cache(default='')
def py__doc__(self):
api_type = self.api_type
if api_type in ('function', 'class'):

View File

@@ -171,8 +171,11 @@ def _get_paths_from_buildout_script(inference_state, buildout_script_path):
def _get_parent_dir_with_file(path: Path, filename):
for parent in path.parents:
if parent.joinpath(filename).is_file():
return parent
try:
if parent.joinpath(filename).is_file():
return parent
except OSError:
continue
return None

View File

@@ -401,21 +401,10 @@ class AnonymousInstance(_BaseTreeInstance):
_arguments = None
class CompiledInstanceName(compiled.CompiledName):
def __init__(self, inference_state, instance, klass, name):
parent_value = klass.parent_context.get_value()
assert parent_value is not None, "How? Please reproduce and report"
super().__init__(
inference_state,
parent_value,
name.string_name
)
self._instance = instance
self._class_member_name = name
class CompiledInstanceName(NameWrapper):
@iterator_to_value_set
def infer(self):
for result_value in self._class_member_name.infer():
for result_value in self._wrapped_name.infer():
if result_value.api_type == 'function':
yield CompiledBoundMethod(result_value)
else:
@@ -434,11 +423,7 @@ class CompiledInstanceClassFilter(AbstractFilter):
return self._convert(self._class_filter.values())
def _convert(self, names):
klass = self._class_filter.compiled_value
return [
CompiledInstanceName(self._instance.inference_state, self._instance, klass, n)
for n in names
]
return [CompiledInstanceName(n) for n in names]
class BoundMethod(FunctionMixin, ValueWrapper):
@@ -516,6 +501,18 @@ class SelfName(TreeNameDefinition):
def get_defining_qualified_value(self):
return self._instance
def infer(self):
stmt = search_ancestor(self.tree_name, 'expr_stmt')
if stmt is not None:
if stmt.children[1].type == "annassign":
from jedi.inference.gradual.annotation import infer_annotation
values = infer_annotation(
self.parent_context, stmt.children[1].children[1]
).execute_annotation()
if values:
return values
return super().infer()
class LazyInstanceClassName(NameWrapper):
def __init__(self, instance, class_member_name):