mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-18 11:35:57 +08:00
basically working descriptors!
This commit is contained in:
33
evaluate.py
33
evaluate.py
@@ -184,16 +184,12 @@ class Instance(Executable):
|
|||||||
|
|
||||||
def get_descriptor_return(self, obj):
|
def get_descriptor_return(self, obj):
|
||||||
""" Throws an error if there's no method. """
|
""" Throws an error if there's no method. """
|
||||||
print '\n\nis_desc'
|
|
||||||
method = self.get_subscope_by_name('__get__')
|
method = self.get_subscope_by_name('__get__')
|
||||||
print 'yessa'
|
|
||||||
# args in __set__ descriptors are obj, class.
|
# args in __set__ descriptors are obj, class.
|
||||||
args = parsing.Array([[obj], [obj.base]], None)
|
args = parsing.Array([[obj], [obj.base]], None)
|
||||||
method = InstanceElement(self, method)
|
method = InstanceElement(self, method)
|
||||||
print 'la'
|
|
||||||
res = Execution(method, args).get_return_types()
|
res = Execution(method, args).get_return_types()
|
||||||
|
|
||||||
print res
|
|
||||||
print '\n\n'
|
print '\n\n'
|
||||||
return res
|
return res
|
||||||
|
|
||||||
@@ -295,6 +291,7 @@ class Function(object):
|
|||||||
self.base_func = func
|
self.base_func = func
|
||||||
self.is_decorated = is_decorated
|
self.is_decorated = is_decorated
|
||||||
|
|
||||||
|
@property
|
||||||
@memoize_default()
|
@memoize_default()
|
||||||
def decorated_func(self):
|
def decorated_func(self):
|
||||||
"""
|
"""
|
||||||
@@ -336,11 +333,22 @@ class Function(object):
|
|||||||
f = Function(f)
|
f = Function(f)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
@property
|
||||||
|
def base_instance(self):
|
||||||
|
""" Return the instance, which is created by the decorator """
|
||||||
|
if isinstance(self.decorated_func, Instance):
|
||||||
|
return self.decorated_func
|
||||||
|
else:
|
||||||
|
raise AttributeError('There is no base instance')
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return getattr(self.decorated_func(), name)
|
return getattr(self.decorated_func, name)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<e%s of %s>" % (self.__class__.__name__, self.base_func)
|
dec = ''
|
||||||
|
if self.decorated_func != self.base_func:
|
||||||
|
dec = " is " + repr(self.decorated_func)
|
||||||
|
return "<e%s of %s%s>" % (self.__class__.__name__, self.base_func, dec)
|
||||||
|
|
||||||
|
|
||||||
class Execution(Executable):
|
class Execution(Executable):
|
||||||
@@ -380,7 +388,7 @@ class Execution(Executable):
|
|||||||
debug.dbg('__call__', call_method, self.base)
|
debug.dbg('__call__', call_method, self.base)
|
||||||
base = self.base
|
base = self.base
|
||||||
if isinstance(self.base, Function):
|
if isinstance(self.base, Function):
|
||||||
base = self.base.decorated_func()
|
base = self.base.decorated_func
|
||||||
call_method = InstanceElement(base, call_method)
|
call_method = InstanceElement(base, call_method)
|
||||||
exe = Execution(call_method, self.var_args)
|
exe = Execution(call_method, self.var_args)
|
||||||
stmts = exe.get_return_types()
|
stmts = exe.get_return_types()
|
||||||
@@ -863,10 +871,9 @@ def get_scopes_for_name(scope, name_str, position=None, search_global=False):
|
|||||||
else:
|
else:
|
||||||
inst = Instance(Class(par.parent.parent))
|
inst = Instance(Class(par.parent.parent))
|
||||||
result.append(inst)
|
result.append(inst)
|
||||||
elif isinstance(par, Instance) \
|
elif isinstance(par, InstanceElement) \
|
||||||
and hasattr(par, 'get_descriptor_return'):
|
and hasattr(par, 'get_descriptor_return'):
|
||||||
try:
|
try:
|
||||||
raise KeyError()
|
|
||||||
result += par.get_descriptor_return(scope)
|
result += par.get_descriptor_return(scope)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
result.append(par)
|
result.append(par)
|
||||||
@@ -904,7 +911,6 @@ def get_scopes_for_name(scope, name_str, position=None, search_global=False):
|
|||||||
else:
|
else:
|
||||||
names = scope.get_defined_names()
|
names = scope.get_defined_names()
|
||||||
scope_generator = iter([(scope, names)])
|
scope_generator = iter([(scope, names)])
|
||||||
#print ' ln', position
|
|
||||||
|
|
||||||
return remove_statements(filter_name(scope_generator))
|
return remove_statements(filter_name(scope_generator))
|
||||||
|
|
||||||
@@ -1001,7 +1007,7 @@ def follow_statement(stmt, scope=None, seek_name=None):
|
|||||||
for op, set_vars in stmt.assignment_details:
|
for op, set_vars in stmt.assignment_details:
|
||||||
new_result += assign_tuples(set_vars, result, seek_name)
|
new_result += assign_tuples(set_vars, result, seek_name)
|
||||||
result = new_result
|
result = new_result
|
||||||
return result
|
return set(result)
|
||||||
|
|
||||||
|
|
||||||
def follow_call_list(scope, call_list):
|
def follow_call_list(scope, call_list):
|
||||||
@@ -1038,7 +1044,7 @@ def follow_call(scope, call):
|
|||||||
|
|
||||||
position = (call.parent_stmt.line_nr, call.parent_stmt.indent)
|
position = (call.parent_stmt.line_nr, call.parent_stmt.indent)
|
||||||
current = next(path)
|
current = next(path)
|
||||||
print scope, call, call.parent_stmt, current
|
|
||||||
if isinstance(current, parsing.Array):
|
if isinstance(current, parsing.Array):
|
||||||
result = [Array(current)]
|
result = [Array(current)]
|
||||||
else:
|
else:
|
||||||
@@ -1111,10 +1117,9 @@ def follow_path(path, scope, position=None):
|
|||||||
else:
|
else:
|
||||||
# the function must not be decorated with something else
|
# the function must not be decorated with something else
|
||||||
if isinstance(scope, Function) and \
|
if isinstance(scope, Function) and \
|
||||||
isinstance(scope.decorated_func(), Function):
|
isinstance(scope.decorated_func, Function):
|
||||||
# TODO check default function methods and return them
|
# TODO check default function methods and return them
|
||||||
result = []
|
result = []
|
||||||
print 'la'
|
|
||||||
else:
|
else:
|
||||||
# TODO check magic class methods and return them also
|
# TODO check magic class methods and return them also
|
||||||
# this is the typical lookup while chaining things
|
# this is the typical lookup while chaining things
|
||||||
|
|||||||
@@ -187,10 +187,10 @@ class B():
|
|||||||
def r(self, value):
|
def r(self, value):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
#? []
|
|
||||||
B().r.
|
|
||||||
#? []
|
#? []
|
||||||
B().r()
|
B().r()
|
||||||
|
#? int()
|
||||||
|
B().r
|
||||||
|
|
||||||
class Decorator(object):
|
class Decorator(object):
|
||||||
def __init__(self, func):
|
def __init__(self, func):
|
||||||
|
|||||||
Reference in New Issue
Block a user