Replace a few isinstance calls with the type.

This commit is contained in:
Dave Halter
2017-04-01 00:26:22 +02:00
parent a6829ca546
commit 1fd10d978d

View File

@@ -164,7 +164,7 @@ class Evaluator(object):
name, position=stmt.start_pos, search_global=True)
for_stmt = tree.search_ancestor(stmt, 'for_stmt')
if isinstance(for_stmt, tree.ForStmt) and types \
if for_stmt is not None and for_stmt.type == 'for_stmt' and types \
and for_stmt.defines_one_name():
# Iterate through result and add the values, that's possible
# only in for loops without clutter, because they are
@@ -271,18 +271,19 @@ class Evaluator(object):
def _eval_element_not_cached(self, context, element):
debug.dbg('eval_element %s@%s', element, element.start_pos)
types = set()
if isinstance(element, (tree.Name, tree.Literal)) or element.type == 'atom':
typ = element.type
if isinstance(element, (tree.Name, tree.Literal)) or typ == 'atom':
types = self.eval_atom(context, element)
elif isinstance(element, tree.Keyword):
elif typ == 'keyword':
# For False/True/None
if element.value in ('False', 'True', 'None'):
types.add(compiled.builtin_from_name(self, element.value))
# else: print e.g. could be evaluated like this in Python 2.7
elif isinstance(element, tree.Lambda):
elif typ == 'lambda':
types = set([er.FunctionContext(self, context, element)])
elif element.type == 'expr_stmt':
elif typ == 'expr_stmt':
types = self.eval_statement(context, element)
elif element.type in ('power', 'atom_expr'):
elif typ in ('power', 'atom_expr'):
first_child = element.children[0]
if not (first_child.type == 'keyword' and first_child.value == 'await'):
types = self.eval_atom(context, first_child)
@@ -292,22 +293,22 @@ class Evaluator(object):
types = set(precedence.calculate(self, context, types, trailer, right))
break
types = self.eval_trailer(context, types, trailer)
elif element.type in ('testlist_star_expr', 'testlist',):
elif typ in ('testlist_star_expr', 'testlist',):
# The implicit tuple in statements.
types = set([iterable.SequenceLiteralContext(self, context, element)])
elif element.type in ('not_test', 'factor'):
elif typ in ('not_test', 'factor'):
types = self.eval_element(context, element.children[-1])
for operator in element.children[:-1]:
types = set(precedence.factor_calculate(self, types, operator))
elif element.type == 'test':
elif typ == 'test':
# `x if foo else y` case.
types = (self.eval_element(context, element.children[0]) |
self.eval_element(context, element.children[-1]))
elif element.type == 'operator':
elif typ == 'operator':
# Must be an ellipsis, other operators are not evaluated.
assert element.value == '...'
types = set([compiled.create(self, Ellipsis)])
elif element.type == 'dotted_name':
elif typ == 'dotted_name':
types = self.eval_atom(context, element.children[0])
for next_name in element.children[2::2]:
# TODO add search_global=True?
@@ -316,9 +317,9 @@ class Evaluator(object):
for typ in types
)
types = types
elif element.type == 'eval_input':
elif typ == 'eval_input':
types = self._eval_element_not_cached(context, element.children[0])
elif element.type == 'annassign':
elif typ == 'annassign':
print(element.children[1])
types = pep0484._evaluate_for_annotation(context, element.children[1])
print('xxx')
@@ -333,10 +334,10 @@ class Evaluator(object):
generate the node (because it has just one child). In that case an atom
might be a name or a literal as well.
"""
if isinstance(atom, tree.Name):
if atom.type == 'name':
# This is the first global lookup.
stmt = atom.get_definition()
if isinstance(stmt, tree.CompFor):
if stmt.type == 'comp_for':
stmt = tree.search_ancestor(stmt, ('expr_stmt', 'lambda', 'funcdef', 'classdef'))
if stmt is None or stmt.type != 'expr_stmt':
# We only need to adjust the start_pos for statements, because
@@ -490,7 +491,7 @@ class Evaluator(object):
if param_name.string_name == name.value:
param_names.append(param_name)
return param_names
elif isinstance(par, tree.ExprStmt) and name in par.get_defined_names():
elif par.type == 'expr_stmt' and name in par.get_defined_names():
# Only take the parent, because if it's more complicated than just
# a name it's something you can "goto" again.
return [TreeNameDefinition(context, name)]