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())
class FakeImport(pr.Import):
class FakeImport(pr.ImportName):
def __init__(self, name, parent, level=0):
super(FakeImport, self).__init__([])
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
responsibility of the decorators.
"""
type = 'funcdef'
def __init__(self, evaluator, base, *args, **kwargs):
super(FunctionExecution, self).__init__(evaluator, base, *args, **kwargs)
# for deep_ast_copy
@@ -646,7 +648,7 @@ class FunctionExecution(Executed):
return objects
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))
return getattr(self.base, name)

View File

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

View File

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