mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-06 14:04:26 +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`
|
||||
"""
|
||||
|
||||
user_stmt = self._parser.user_stmt_with_whitespace()
|
||||
call, index = helpers.func_call_and_param_index(user_stmt, self._pos)
|
||||
if call is None:
|
||||
@@ -543,9 +542,10 @@ class Script(object):
|
||||
origins = cache.cache_call_signatures(_callable, user_stmt)
|
||||
debug.speed('func_call followed')
|
||||
|
||||
return [classes.CallSignature(self._evaluator, o, call, index) for o in origins
|
||||
if o.isinstance(er.Function, er.Instance, er.Class)
|
||||
or isinstance(o, compiled.CompiledObject) and o.type() != 'module']
|
||||
print([classes.CallSignature(self._evaluator, o, call, index)
|
||||
for o in origins])
|
||||
return [classes.CallSignature(self._evaluator, o, call, index)
|
||||
for o in origins if o.is_callable()]
|
||||
|
||||
|
||||
class Interpreter(Script):
|
||||
|
||||
@@ -138,6 +138,10 @@ class CompiledObject(Base):
|
||||
def get_imports(self):
|
||||
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):
|
||||
def __init__(self, obj, name):
|
||||
|
||||
@@ -160,6 +160,13 @@ class Instance(use_metaclass(CachedMetaClass, Executable)):
|
||||
names.append(InstanceElement(self._evaluator, self, var, True))
|
||||
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):
|
||||
args = [] if index is None else [index]
|
||||
try:
|
||||
@@ -232,6 +239,9 @@ class InstanceElement(use_metaclass(CachedMetaClass, pr.Base)):
|
||||
def isinstance(self, *cls):
|
||||
return isinstance(self.var, cls)
|
||||
|
||||
def is_callable(self):
|
||||
return self.var.is_callable()
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s of %s>" % (type(self).__name__, self.var)
|
||||
|
||||
@@ -298,6 +308,9 @@ class Class(use_metaclass(CachedMetaClass, pr.IsScope)):
|
||||
return sub
|
||||
raise KeyError("Couldn't find subscope.")
|
||||
|
||||
def is_callable(self):
|
||||
return True
|
||||
|
||||
@common.safe_property
|
||||
def name(self):
|
||||
return self.base.name
|
||||
@@ -379,6 +392,9 @@ class Function(use_metaclass(CachedMetaClass, pr.IsScope)):
|
||||
def get_magic_function_scope(self):
|
||||
return compiled.magic_function_class
|
||||
|
||||
def is_callable(self):
|
||||
return True
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.base_func, name)
|
||||
|
||||
|
||||
@@ -118,6 +118,13 @@ class Base(object):
|
||||
scope = scope.parent
|
||||
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):
|
||||
"""Return the space between two tokens"""
|
||||
linecount = to_pos[0] - from_pos[0]
|
||||
|
||||
@@ -206,12 +206,11 @@ def test_signature_is_definition():
|
||||
|
||||
def test_no_signature():
|
||||
# str doesn't have a __call__ method
|
||||
assert Script('str()(').call_signatures == []
|
||||
assert Script('str()(').call_signatures() == []
|
||||
|
||||
s = dedent("""\
|
||||
class X():
|
||||
pass
|
||||
X()(
|
||||
""")
|
||||
assert Script(s).call_signatures == []
|
||||
assert len(Script(s, column=2).call_signatures) == 1
|
||||
X()(""")
|
||||
assert Script(s).call_signatures() == []
|
||||
assert len(Script(s, column=2).call_signatures()) == 1
|
||||
|
||||
Reference in New Issue
Block a user