mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
create an is_callable method for all representation objects, to determine if something is callable or not
This commit is contained in:
@@ -532,7 +532,6 @@ class Script(object):
|
|||||||
|
|
||||||
:rtype: list of :class:`classes.CallSignature`
|
:rtype: list of :class:`classes.CallSignature`
|
||||||
"""
|
"""
|
||||||
|
|
||||||
user_stmt = self._parser.user_stmt_with_whitespace()
|
user_stmt = self._parser.user_stmt_with_whitespace()
|
||||||
call, index = helpers.func_call_and_param_index(user_stmt, self._pos)
|
call, index = helpers.func_call_and_param_index(user_stmt, self._pos)
|
||||||
if call is None:
|
if call is None:
|
||||||
@@ -543,9 +542,10 @@ class Script(object):
|
|||||||
origins = cache.cache_call_signatures(_callable, user_stmt)
|
origins = cache.cache_call_signatures(_callable, user_stmt)
|
||||||
debug.speed('func_call followed')
|
debug.speed('func_call followed')
|
||||||
|
|
||||||
return [classes.CallSignature(self._evaluator, o, call, index) for o in origins
|
print([classes.CallSignature(self._evaluator, o, call, index)
|
||||||
if o.isinstance(er.Function, er.Instance, er.Class)
|
for o in origins])
|
||||||
or isinstance(o, compiled.CompiledObject) and o.type() != 'module']
|
return [classes.CallSignature(self._evaluator, o, call, index)
|
||||||
|
for o in origins if o.is_callable()]
|
||||||
|
|
||||||
|
|
||||||
class Interpreter(Script):
|
class Interpreter(Script):
|
||||||
|
|||||||
@@ -138,6 +138,10 @@ class CompiledObject(Base):
|
|||||||
def get_imports(self):
|
def get_imports(self):
|
||||||
return [] # Builtins don't have imports
|
return [] # Builtins don't have imports
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
"""Check if the object has a ``__call__`` method."""
|
||||||
|
return hasattr(self.obj, '__call__')
|
||||||
|
|
||||||
|
|
||||||
class CompiledName(object):
|
class CompiledName(object):
|
||||||
def __init__(self, obj, name):
|
def __init__(self, obj, name):
|
||||||
|
|||||||
@@ -160,6 +160,13 @@ class Instance(use_metaclass(CachedMetaClass, Executable)):
|
|||||||
names.append(InstanceElement(self._evaluator, self, var, True))
|
names.append(InstanceElement(self._evaluator, self, var, True))
|
||||||
yield self, names
|
yield self, names
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
try:
|
||||||
|
self.get_subscope_by_name('__call__')
|
||||||
|
return True
|
||||||
|
except KeyError:
|
||||||
|
return False
|
||||||
|
|
||||||
def get_index_types(self, index=None):
|
def get_index_types(self, index=None):
|
||||||
args = [] if index is None else [index]
|
args = [] if index is None else [index]
|
||||||
try:
|
try:
|
||||||
@@ -232,6 +239,9 @@ class InstanceElement(use_metaclass(CachedMetaClass, pr.Base)):
|
|||||||
def isinstance(self, *cls):
|
def isinstance(self, *cls):
|
||||||
return isinstance(self.var, cls)
|
return isinstance(self.var, cls)
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
return self.var.is_callable()
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s of %s>" % (type(self).__name__, self.var)
|
return "<%s of %s>" % (type(self).__name__, self.var)
|
||||||
|
|
||||||
@@ -298,6 +308,9 @@ class Class(use_metaclass(CachedMetaClass, pr.IsScope)):
|
|||||||
return sub
|
return sub
|
||||||
raise KeyError("Couldn't find subscope.")
|
raise KeyError("Couldn't find subscope.")
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
return True
|
||||||
|
|
||||||
@common.safe_property
|
@common.safe_property
|
||||||
def name(self):
|
def name(self):
|
||||||
return self.base.name
|
return self.base.name
|
||||||
@@ -379,6 +392,9 @@ class Function(use_metaclass(CachedMetaClass, pr.IsScope)):
|
|||||||
def get_magic_function_scope(self):
|
def get_magic_function_scope(self):
|
||||||
return compiled.magic_function_class
|
return compiled.magic_function_class
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
return True
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return getattr(self.base_func, name)
|
return getattr(self.base_func, name)
|
||||||
|
|
||||||
|
|||||||
@@ -118,6 +118,13 @@ class Base(object):
|
|||||||
scope = scope.parent
|
scope = scope.parent
|
||||||
return scope
|
return scope
|
||||||
|
|
||||||
|
def is_callable(self):
|
||||||
|
"""
|
||||||
|
By default parser objects are not callable, we make them callable by
|
||||||
|
the ``evaluate.representation`` objects.
|
||||||
|
"""
|
||||||
|
return False
|
||||||
|
|
||||||
def space(self, from_pos, to_pos):
|
def space(self, from_pos, to_pos):
|
||||||
"""Return the space between two tokens"""
|
"""Return the space between two tokens"""
|
||||||
linecount = to_pos[0] - from_pos[0]
|
linecount = to_pos[0] - from_pos[0]
|
||||||
|
|||||||
@@ -206,12 +206,11 @@ def test_signature_is_definition():
|
|||||||
|
|
||||||
def test_no_signature():
|
def test_no_signature():
|
||||||
# str doesn't have a __call__ method
|
# str doesn't have a __call__ method
|
||||||
assert Script('str()(').call_signatures == []
|
assert Script('str()(').call_signatures() == []
|
||||||
|
|
||||||
s = dedent("""\
|
s = dedent("""\
|
||||||
class X():
|
class X():
|
||||||
pass
|
pass
|
||||||
X()(
|
X()(""")
|
||||||
""")
|
assert Script(s).call_signatures() == []
|
||||||
assert Script(s).call_signatures == []
|
assert len(Script(s, column=2).call_signatures()) == 1
|
||||||
assert len(Script(s, column=2).call_signatures) == 1
|
|
||||||
|
|||||||
Reference in New Issue
Block a user