mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
use memoize_default instead of strange underscore_decorators in api classes
This commit is contained in:
@@ -8,8 +8,8 @@ import warnings
|
|||||||
from jedi._compatibility import next, unicode
|
from jedi._compatibility import next, unicode
|
||||||
from jedi import settings
|
from jedi import settings
|
||||||
from jedi import common
|
from jedi import common
|
||||||
from jedi import cache
|
|
||||||
from jedi.parser import representation as pr
|
from jedi.parser import representation as pr
|
||||||
|
from jedi.evaluate.cache import memoize_default
|
||||||
from jedi.evaluate import representation as er
|
from jedi.evaluate import representation as er
|
||||||
from jedi.evaluate import iterable
|
from jedi.evaluate import iterable
|
||||||
from jedi.evaluate import imports
|
from jedi.evaluate import imports
|
||||||
@@ -300,8 +300,25 @@ class BaseDefinition(object):
|
|||||||
|
|
||||||
return stripped.is_callable()
|
return stripped.is_callable()
|
||||||
|
|
||||||
|
@memoize_default()
|
||||||
|
def _follow_statements_imports(self):
|
||||||
|
if self._definition.isinstance(pr.Statement):
|
||||||
|
defs = self._evaluator.eval_statement(self._definition)
|
||||||
|
elif self._definition.isinstance(pr.Import):
|
||||||
|
if self._definition.alias is None:
|
||||||
|
i = imports.ImportPath(self._evaluator, self._definition, True)
|
||||||
|
defs = imports.Importer(i.import_path + [unicode(self._name)],
|
||||||
|
i._importer.module).follow(self._evaluator)
|
||||||
|
else:
|
||||||
|
defs = imports.strip_imports(self._evaluator, [self._definition])
|
||||||
|
else:
|
||||||
|
return [self]
|
||||||
|
|
||||||
|
defs = [BaseDefinition(self._evaluator, d, d.start_pos) for d in defs]
|
||||||
|
return defs
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@cache.underscore_memoization
|
@memoize_default()
|
||||||
def params(self):
|
def params(self):
|
||||||
"""
|
"""
|
||||||
Raises an ``AttributeError``if the definition is not callable.
|
Raises an ``AttributeError``if the definition is not callable.
|
||||||
@@ -465,7 +482,7 @@ class Completion(BaseDefinition):
|
|||||||
return followed[0].type
|
return followed[0].type
|
||||||
return super(Completion, self).type
|
return super(Completion, self).type
|
||||||
|
|
||||||
@cache.underscore_memoization
|
@memoize_default()
|
||||||
def follow_definition(self):
|
def follow_definition(self):
|
||||||
"""
|
"""
|
||||||
Return the original definitions. I strongly recommend not using it for
|
Return the original definitions. I strongly recommend not using it for
|
||||||
@@ -488,7 +505,6 @@ class Completion(BaseDefinition):
|
|||||||
return [self]
|
return [self]
|
||||||
|
|
||||||
defs = [BaseDefinition(self._evaluator, d, d.start_pos) for d in defs]
|
defs = [BaseDefinition(self._evaluator, d, d.start_pos) for d in defs]
|
||||||
cache.clear_caches()
|
|
||||||
return defs
|
return defs
|
||||||
|
|
||||||
|
|
||||||
@@ -607,7 +623,7 @@ class Definition(BaseDefinition):
|
|||||||
position = '' if self.in_builtin_module else '@%s' % (self.line)
|
position = '' if self.in_builtin_module else '@%s' % (self.line)
|
||||||
return "%s:%s%s" % (self.module_name, self.description, position)
|
return "%s:%s%s" % (self.module_name, self.description, position)
|
||||||
|
|
||||||
@cache.underscore_memoization
|
@memoize_default()
|
||||||
def defined_names(self):
|
def defined_names(self):
|
||||||
"""
|
"""
|
||||||
List sub-definitions (e.g., methods in class).
|
List sub-definitions (e.g., methods in class).
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def memoize_default(default, evaluator_is_first_arg=False, second_arg_is_evaluator=False):
|
def memoize_default(default=None, evaluator_is_first_arg=False, second_arg_is_evaluator=False):
|
||||||
""" This is a typical memoization decorator, BUT there is one difference:
|
""" This is a typical memoization decorator, BUT there is one difference:
|
||||||
To prevent recursion it sets defaults.
|
To prevent recursion it sets defaults.
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ class Instance(use_metaclass(CachedMetaClass, Executable)):
|
|||||||
# (No var_args) used.
|
# (No var_args) used.
|
||||||
self.is_generated = False
|
self.is_generated = False
|
||||||
|
|
||||||
@memoize_default(None)
|
@memoize_default()
|
||||||
def _get_method_execution(self, func):
|
def _get_method_execution(self, func):
|
||||||
func = InstanceElement(self._evaluator, self, func, True)
|
func = InstanceElement(self._evaluator, self, func, True)
|
||||||
return FunctionExecution(self._evaluator, func, self.var_args)
|
return FunctionExecution(self._evaluator, func, self.var_args)
|
||||||
@@ -203,7 +203,7 @@ class InstanceElement(use_metaclass(CachedMetaClass, pr.Base)):
|
|||||||
self.is_class_var = is_class_var
|
self.is_class_var = is_class_var
|
||||||
|
|
||||||
@common.safe_property
|
@common.safe_property
|
||||||
@memoize_default(None)
|
@memoize_default()
|
||||||
def parent(self):
|
def parent(self):
|
||||||
par = self.var.parent
|
par = self.var.parent
|
||||||
if isinstance(par, Class) and par == self.instance.base \
|
if isinstance(par, Class) and par == self.instance.base \
|
||||||
@@ -336,7 +336,7 @@ class Function(use_metaclass(CachedMetaClass, pr.IsScope)):
|
|||||||
self.base_func = func
|
self.base_func = func
|
||||||
self.is_decorated = is_decorated
|
self.is_decorated = is_decorated
|
||||||
|
|
||||||
@memoize_default(None)
|
@memoize_default()
|
||||||
def _decorated_func(self):
|
def _decorated_func(self):
|
||||||
"""
|
"""
|
||||||
Returns the function, that is to be executed in the end.
|
Returns the function, that is to be executed in the end.
|
||||||
@@ -476,7 +476,7 @@ class FunctionExecution(Executable):
|
|||||||
raise AttributeError('Tried to access %s: %s. Why?' % (name, self))
|
raise AttributeError('Tried to access %s: %s. Why?' % (name, self))
|
||||||
return getattr(self.base, name)
|
return getattr(self.base, name)
|
||||||
|
|
||||||
@memoize_default(None)
|
@memoize_default()
|
||||||
def _scope_copy(self, scope):
|
def _scope_copy(self, scope):
|
||||||
""" Copies a scope (e.g. if) in an execution """
|
""" Copies a scope (e.g. if) in an execution """
|
||||||
# TODO method uses different scopes than the subscopes property.
|
# TODO method uses different scopes than the subscopes property.
|
||||||
|
|||||||
@@ -126,16 +126,16 @@ def test_completion_documentation():
|
|||||||
|
|
||||||
|
|
||||||
def test_signature_params():
|
def test_signature_params():
|
||||||
|
def check(defs):
|
||||||
|
params = defs[0].params
|
||||||
|
assert len(params) == 1
|
||||||
|
assert params[0].name == 'bar'
|
||||||
|
|
||||||
s = dedent('''
|
s = dedent('''
|
||||||
def foo(bar):
|
def foo(bar):
|
||||||
pass
|
pass
|
||||||
foo''')
|
foo''')
|
||||||
|
|
||||||
defs = Script(s).goto_definitions()
|
check(Script(s).goto_definitions())
|
||||||
params = defs[0].params
|
|
||||||
assert len(params) == 1
|
|
||||||
assert params[0].name == 'bar'
|
|
||||||
|
|
||||||
defs = Script(s).goto_assignments()
|
check(Script(s).goto_assignments())
|
||||||
with pytest.raises(AttributeError):
|
|
||||||
params = defs[0].params
|
|
||||||
|
|||||||
Reference in New Issue
Block a user