mirror of
https://github.com/davidhalter/jedi.git
synced 2026-05-24 17:28:36 +08:00
Get rid of get_defined_names in compiled modules.
This commit is contained in:
@@ -6,7 +6,6 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from itertools import chain
|
|
||||||
|
|
||||||
from jedi._compatibility import builtins as _builtins, unicode
|
from jedi._compatibility import builtins as _builtins, unicode
|
||||||
from jedi import debug
|
from jedi import debug
|
||||||
@@ -137,30 +136,21 @@ class CompiledObject(Base):
|
|||||||
return CompiledObject(c, self.parent)
|
return CompiledObject(c, self.parent)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_defined_names(self):
|
|
||||||
return list(chain.from_iterable(self.names_dict.values()))
|
|
||||||
|
|
||||||
# TODO still used?
|
|
||||||
if inspect.ismodule(self.obj):
|
|
||||||
return self.instance_names()
|
|
||||||
else:
|
|
||||||
return type_names + self.instance_names()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@underscore_memoization
|
|
||||||
def names_dict(self):
|
def names_dict(self):
|
||||||
return LazyNamesDict(self._cls())
|
# For compatibility with `representation.Class`.
|
||||||
|
return self.names_dicts(False)[0]
|
||||||
|
|
||||||
def names_dicts(self, search_global):
|
def names_dicts(self, search_global, is_instance=False):
|
||||||
yield self.names_dict
|
return self._names_dict_ensure_one_dict(is_instance)
|
||||||
|
|
||||||
@underscore_memoization
|
@memoize_method
|
||||||
def instance_names(self):
|
def _names_dict_ensure_one_dict(self, is_instance):
|
||||||
names = []
|
"""
|
||||||
cls = self._cls()
|
search_global shouldn't change the fact that there's one dict, this way
|
||||||
for name in dir(cls.obj):
|
there's only one `object`.
|
||||||
names.append(CompiledName(cls, name))
|
"""
|
||||||
return names
|
return [LazyNamesDict(self._cls(), is_instance)]
|
||||||
|
|
||||||
def get_subscope_by_name(self, name):
|
def get_subscope_by_name(self, name):
|
||||||
if name in dir(self._cls().obj):
|
if name in dir(self._cls().obj):
|
||||||
@@ -250,8 +240,9 @@ class LazyNamesDict(object):
|
|||||||
"""
|
"""
|
||||||
A names_dict instance for compiled objects, resembles the parser.tree.
|
A names_dict instance for compiled objects, resembles the parser.tree.
|
||||||
"""
|
"""
|
||||||
def __init__(self, compiled_obj):
|
def __init__(self, compiled_obj, is_instance):
|
||||||
self._compiled_obj = compiled_obj
|
self._compiled_obj = compiled_obj
|
||||||
|
self._is_instance = is_instance
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return (v[0].value for v in self.values())
|
return (v[0].value for v in self.values())
|
||||||
@@ -275,8 +266,9 @@ class LazyNamesDict(object):
|
|||||||
# The dir function can be wrong.
|
# The dir function can be wrong.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not inspect.ismodule(obj):
|
# dir doesn't include the type names.
|
||||||
values.append(type_names)
|
if not inspect.ismodule(obj) and obj != type and not self._is_instance:
|
||||||
|
values += _type_names_dict.values()
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
|
||||||
@@ -486,8 +478,7 @@ def _create_from_name(module, parent, name):
|
|||||||
builtin = Builtin(_builtins)
|
builtin = Builtin(_builtins)
|
||||||
magic_function_class = CompiledObject(type(load_module), parent=builtin)
|
magic_function_class = CompiledObject(type(load_module), parent=builtin)
|
||||||
generator_obj = CompiledObject(_a_generator(1.0))
|
generator_obj = CompiledObject(_a_generator(1.0))
|
||||||
type_names = [] # Need this, because its part of the result of get_defined_names.
|
_type_names_dict = builtin.get_by_name('type').names_dict
|
||||||
type_names = builtin.get_by_name('type').get_defined_names()
|
|
||||||
none_obj = builtin.get_by_name('None')
|
none_obj = builtin.get_by_name('None')
|
||||||
false_obj = builtin.get_by_name('False')
|
false_obj = builtin.get_by_name('False')
|
||||||
true_obj = builtin.get_by_name('True')
|
true_obj = builtin.get_by_name('True')
|
||||||
|
|||||||
@@ -48,12 +48,13 @@ class GeneratorMixin(object):
|
|||||||
def names_dicts(self, search_global=False): # is always False
|
def names_dicts(self, search_global=False): # is always False
|
||||||
dct = {}
|
dct = {}
|
||||||
executes_generator = '__next__', 'send', 'next'
|
executes_generator = '__next__', 'send', 'next'
|
||||||
for name in compiled.generator_obj.get_defined_names():
|
for names in compiled.generator_obj.names_dict.values():
|
||||||
if name.value in executes_generator:
|
for name in names:
|
||||||
parent = GeneratorMethod(self, name.parent)
|
if name.value in executes_generator:
|
||||||
dct[name.value] = [helpers.FakeName(name.name, parent, is_definition=True)]
|
parent = GeneratorMethod(self, name.parent)
|
||||||
else:
|
dct[name.value] = [helpers.FakeName(name.name, parent, is_definition=True)]
|
||||||
dct[name.value] = [name]
|
else:
|
||||||
|
dct[name.value] = [name]
|
||||||
yield dct
|
yield dct
|
||||||
|
|
||||||
def get_index_types(self, evaluator, index_array):
|
def get_index_types(self, evaluator, index_array):
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ class Instance(use_metaclass(CachedMetaClass, Executed)):
|
|||||||
for inst in self._evaluator.execute(s):
|
for inst in self._evaluator.execute(s):
|
||||||
yield inst._self_names_dict(add_mro=False)
|
yield inst._self_names_dict(add_mro=False)
|
||||||
|
|
||||||
for names_dict in self.base.names_dicts(search_global=False):
|
for names_dict in self.base.names_dicts(search_global=False, is_instance=True):
|
||||||
yield LazyInstanceDict(self._evaluator, self, names_dict)
|
yield LazyInstanceDict(self._evaluator, self, names_dict)
|
||||||
|
|
||||||
def get_index_types(self, evaluator, index_array):
|
def get_index_types(self, evaluator, index_array):
|
||||||
@@ -475,12 +475,15 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)):
|
|||||||
def params(self):
|
def params(self):
|
||||||
return self.get_subscope_by_name('__init__').params
|
return self.get_subscope_by_name('__init__').params
|
||||||
|
|
||||||
def names_dicts(self, search_global):
|
def names_dicts(self, search_global, is_instance=False):
|
||||||
if search_global:
|
if search_global:
|
||||||
yield self.names_dict
|
yield self.names_dict
|
||||||
else:
|
else:
|
||||||
for scope in self.py__mro__(self._evaluator):
|
for scope in self.py__mro__(self._evaluator):
|
||||||
yield scope.names_dict
|
if isinstance(scope, compiled.CompiledObject):
|
||||||
|
yield scope.names_dicts(False, is_instance)[0]
|
||||||
|
else:
|
||||||
|
yield scope.names_dict
|
||||||
|
|
||||||
def is_class(self):
|
def is_class(self):
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user