1
0
forked from VimPlug/jedi

Replace some isinstance checks in the parser tree with .type checks.

This commit is contained in:
Dave Halter
2015-02-09 12:27:29 +01:00
parent a9a3387cb0
commit a095f8d9e0
4 changed files with 17 additions and 13 deletions

View File

@@ -156,7 +156,7 @@ def get_module_names(module, all_scopes):
return chain.from_iterable(dct.values()) return chain.from_iterable(dct.values())
class FakeImport(pr.Import): class FakeImport(pr.ImportName):
def __init__(self, name, parent, level=0): def __init__(self, name, parent, level=0):
super(FakeImport, self).__init__([]) super(FakeImport, self).__init__([])
self.parent = parent self.parent = parent

View File

@@ -565,6 +565,8 @@ class FunctionExecution(Executed):
multiple calls to functions and recursion has to be avoided. But this is multiple calls to functions and recursion has to be avoided. But this is
responsibility of the decorators. responsibility of the decorators.
""" """
type = 'funcdef'
def __init__(self, evaluator, base, *args, **kwargs): def __init__(self, evaluator, base, *args, **kwargs):
super(FunctionExecution, self).__init__(evaluator, base, *args, **kwargs) super(FunctionExecution, self).__init__(evaluator, base, *args, **kwargs)
# for deep_ast_copy # for deep_ast_copy
@@ -646,7 +648,7 @@ class FunctionExecution(Executed):
return objects return objects
def __getattr__(self, name): def __getattr__(self, name):
if name not in ['start_pos', 'end_pos', 'imports', '_sub_module', 'type']: if name not in ['start_pos', 'end_pos', 'imports', 'name', 'type']:
raise AttributeError('Tried to access %s: %s. Why?' % (name, self)) raise AttributeError('Tried to access %s: %s. Why?' % (name, self))
return getattr(self.base, name) return getattr(self.base, name)

View File

@@ -212,5 +212,5 @@ class PgenParser(object):
# symbol, children = node # symbol, children = node
self.stack[-1][2][1].append(newnode) self.stack[-1][2][1].append(newnode)
except IndexError: except IndexError:
# stack is empty, set the rootnode. # Stack is empty, set the rootnode.
self.rootnode = newnode self.rootnode = newnode

View File

@@ -77,7 +77,7 @@ class DocstringMixin(object):
except AttributeError: except AttributeError:
pass # Probably a pass Keyword (Leaf). pass # Probably a pass Keyword (Leaf).
else: else:
if isinstance(first, String): if first.type == 'string':
# TODO We have to check next leaves until there are no new # TODO We have to check next leaves until there are no new
# leaves anymore that might be part of the docstring. A # leaves anymore that might be part of the docstring. A
# docstring can also look like this: ``'foo' 'bar' # docstring can also look like this: ``'foo' 'bar'
@@ -281,16 +281,17 @@ class Name(Leaf):
def is_definition(self): def is_definition(self):
stmt = self.get_definition() stmt = self.get_definition()
if isinstance(stmt, (Function, Class, Module)): if stmt.type in ('funcdef', 'classdef', 'file_input'):
return self == stmt.name return self == stmt.name
elif isinstance(stmt, ForStmt): elif stmt.type == 'for_stmt':
return self.start_pos < stmt.children[2].start_pos return self.start_pos < stmt.children[2].start_pos
elif isinstance(stmt, Param): elif stmt.type == 'param':
return self == stmt.get_name() return self == stmt.get_name()
elif isinstance(stmt, TryStmt): elif stmt.type == 'try_stmt':
return self.prev_sibling() == 'as' return self.prev_sibling() == 'as'
else: else:
return isinstance(stmt, (ExprStmt, Import, CompFor, WithStmt)) \ return stmt.type in ('expr_stmt', 'import_name', 'import_from',
'comp_for', 'with_stmt') \
and self in stmt.get_defined_names() and self in stmt.get_defined_names()
def assignment_indexes(self): def assignment_indexes(self):
@@ -402,6 +403,7 @@ class BaseNode(Base):
the parser tree inherits from this class. the parser tree inherits from this class.
""" """
__slots__ = ('children', 'parent') __slots__ = ('children', 'parent')
type = None
def __init__(self, children): def __init__(self, children):
""" """
@@ -452,7 +454,7 @@ class BaseNode(Base):
def get_statement_for_position(self, pos): def get_statement_for_position(self, pos):
for c in self.children: for c in self.children:
if c.start_pos <= pos <= c.end_pos: if c.start_pos <= pos <= c.end_pos:
if isinstance(c, (ExprStmt, Import)): if c.type in ('expr_stmt', 'import_from', 'import_name'):
return c return c
else: else:
try: try:
@@ -633,7 +635,7 @@ class Module(Scope):
# statement/import with a tokenizer (to check for syntax changes like # statement/import with a tokenizer (to check for syntax changes like
# the future print statement). # the future print statement).
for imp in self.imports: for imp in self.imports:
if isinstance(imp, ImportFrom) and imp.level == 0: if imp.type == 'import_from' and imp.level == 0:
for path in imp.paths(): for path in imp.paths():
if [str(name) for name in path] == ['__future__', 'absolute_import']: if [str(name) for name in path] == ['__future__', 'absolute_import']:
return True return True
@@ -950,7 +952,7 @@ class ImportFrom(Import):
else: else:
as_names = [last] as_names = [last]
for as_name in as_names: for as_name in as_names:
if isinstance(as_name, Name): if as_name.type == 'name':
yield as_name, None yield as_name, None
else: else:
yield as_name.children[::2] # yields x, y -> ``x as y`` yield as_name.children[::2] # yields x, y -> ``x as y``
@@ -1011,7 +1013,7 @@ class ImportName(Import):
as_name = as_name.children[0] as_name = as_name.children[0]
else: else:
alias = None alias = None
if isinstance(as_name, Name): if as_name.type == 'name':
yield [as_name], alias yield [as_name], alias
else: else:
# dotted_names # dotted_names