mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
Implemented dynamic superclasses
This commit is contained in:
@@ -157,9 +157,9 @@ https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
|
||||
Parameters
|
||||
----------
|
||||
var1 : array_like
|
||||
Array_like means all those objects -- lists, nested lists, etc. --
|
||||
that can be converted to an array. We can also refer to
|
||||
variables like `var1`.
|
||||
Array_like means all those objects -- lists, nested lists,
|
||||
etc. -- that can be converted to an array. We can also
|
||||
refer to variables like `var1`.
|
||||
var2 : int
|
||||
The type above can either refer to an actual Python type
|
||||
(e.g. ``int``), or describe the type of the variable in more
|
||||
|
||||
@@ -174,7 +174,7 @@ class Parser(object):
|
||||
break
|
||||
return imports
|
||||
|
||||
def _parse_parentheses(self):
|
||||
def _parse_parentheses(self, is_class):
|
||||
"""
|
||||
Functions and Classes have params (which means for classes
|
||||
super-classes). They are parsed here and returned as Statements.
|
||||
@@ -195,8 +195,9 @@ class Parser(object):
|
||||
if annotation:
|
||||
param.add_annotation(annotation)
|
||||
|
||||
# params without vars are usually syntax errors.
|
||||
if param and (param.get_defined_names()):
|
||||
# function params without vars are usually syntax errors.
|
||||
# expressions are valid in superclass declarations.
|
||||
if param and (param.get_defined_names() or is_class):
|
||||
param.position_nr = pos
|
||||
names.append(param)
|
||||
pos += 1
|
||||
@@ -222,7 +223,7 @@ class Parser(object):
|
||||
tok = next(self._gen)
|
||||
if tok.string != '(':
|
||||
return None
|
||||
params = self._parse_parentheses()
|
||||
params = self._parse_parentheses(is_class=False)
|
||||
|
||||
colon = next(self._gen)
|
||||
annotation = None
|
||||
@@ -259,17 +260,17 @@ class Parser(object):
|
||||
cname = pr.Name(self.module, [(cname.string, cname.start_pos)],
|
||||
cname.start_pos, cname.end_pos)
|
||||
|
||||
super = []
|
||||
superclasses = []
|
||||
_next = next(self._gen)
|
||||
if _next.string == '(':
|
||||
super = self._parse_parentheses()
|
||||
superclasses = self._parse_parentheses(is_class=True)
|
||||
_next = next(self._gen)
|
||||
|
||||
if _next.string != ':':
|
||||
debug.warning("class syntax: %s@%s", cname, _next.start_pos[0])
|
||||
return None
|
||||
|
||||
return pr.Class(self.module, cname, super, first_pos)
|
||||
return pr.Class(self.module, cname, superclasses, first_pos)
|
||||
|
||||
def _parse_statement(self, pre_used_token=None, added_breaks=None,
|
||||
stmt_class=pr.Statement, names_are_set_vars=False):
|
||||
|
||||
@@ -917,8 +917,8 @@ class Statement(Simple, DocstringMixin):
|
||||
|
||||
def get_code(self, new_line=True):
|
||||
def assemble(command_list, assignment=None):
|
||||
pieces = [c.get_code() if isinstance(c, Simple) else c.string if
|
||||
isinstance(c, tokenize.Token) else unicode(c)
|
||||
pieces = [c.get_code() if isinstance(c, Simple) else c.string
|
||||
if isinstance(c, tokenize.Token) else unicode(c)
|
||||
for c in command_list]
|
||||
if assignment is None:
|
||||
return ''.join(pieces)
|
||||
|
||||
@@ -191,6 +191,22 @@ Base.upper
|
||||
#? ['upper']
|
||||
Base().upper
|
||||
|
||||
# -----------------
|
||||
# dynamic inheritance
|
||||
# -----------------
|
||||
|
||||
class Angry(object):
|
||||
def shout(self):
|
||||
return 'THIS IS MALARKEY!'
|
||||
|
||||
def classgetter():
|
||||
return Angry
|
||||
|
||||
class Dude(classgetter()):
|
||||
def react(self):
|
||||
#? ['shout']
|
||||
self.s
|
||||
|
||||
# -----------------
|
||||
# __call__
|
||||
# -----------------
|
||||
|
||||
Reference in New Issue
Block a user