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