8000 Use dicts instead of long elif chains for fastparse operator conversion by ddfisher · Pull Request #1248 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Use dicts instead of long elif chains for fastparse operator conversion #1248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 40 additions & 48 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,58 +98,50 @@ def visit_NoneType(self, n):
def visit_list(self, l):
return [self.visit(e) for e in l]

op_map = {
typed_ast.Add: '+',
typed_ast.Sub: '-',
typed_ast.Mult: '*',
typed_ast.MatMult: '@',
typed_ast.Div: '/',
typed_ast.Mod: '%',
typed_ast.Pow: '**',
typed_ast.LShift: '<<',
typed_ast.RShift: '>>',
typed_ast.BitOr: '|',
typed_ast.BitXor: '^',
typed_ast.BitAnd: '&',
typed_ast.FloorDiv: '//'
}

def from_operator(self, op):
if isinstance(op, typed_ast.Add):
return '+'
elif isinstance(op, typed_ast.Sub):
return '-'
elif isinstance(op, typed_ast.Mult):
return '*'
elif isinstance(op, typed_ast.MatMult):
raise RuntimeError('No conversion for MatMult operator')
elif isinstance(op, typed_ast.Div):
return '/'
elif isinstance(op, typed_ast.Mod):
return '%'
elif isinstance(op, typed_ast.Pow):
return '**'
elif isinstance(op, typed_ast.LShift):
return '<<'
elif isinstance(op, typed_ast.RShift):
return '>>'
elif isinstance(op, typed_ast.BitOr):
return '|'
elif isinstance(op, typed_ast.BitXor):
return '^'
elif isinstance(op, typed_ast.BitAnd):
return '&'
elif isinstance(op, typed_ast.FloorDiv):
return '//'
raise RuntimeError('Unknown operator ' + str(type(op)))
op_name = ASTConverter.op_map.get(type(op))
if op_name is None:
raise RuntimeError('Unknown operator ' + str(type(op)))
elif op_name == '@':
raise RuntimeError('mypy does not support the MatMult operator')
else:
return op_name

comp_op_map = {
typed_ast.Gt: '>',
typed_ast.Lt: '<',
typed_ast.Eq: '==',
typed_ast.GtE: '>=',
typed_ast.LtE: '<=',
typed_ast.NotEq: '!=',
typed_ast.Is: 'is',
typed_ast.IsNot: 'is not',
typed_ast.In: 'in',
typed_ast.NotIn: 'not in'
}

def from_comp_operator(self, op):
if isinstance(op, typed_ast.Gt):
return '>'
elif isinstance(op, typed_ast.Lt):
return '<'
elif isinstance(op, typed_ast.Eq):
return '=='
elif isinstance(op, typed_ast.GtE):
return '>='
elif isinstance(op, typed_ast.LtE):
return '<='
elif isinstance(op, typed_ast.NotEq):
return '!='
elif isinstance(op, typed_ast.Is):
return 'is'
elif isinstance(op, typed_ast.IsNot):
return 'is not'
elif isinstance(op, typed_ast.In):
return 'in'
elif isinstance(op, typed_ast.NotIn):
return 'not in'
else:
op_name = ASTConverter.comp_op_map.get(type(op))
if op_name is None:
raise RuntimeError('Unknown comparison operator ' + str(type(op)))
else:
return op_name

def as_block(self, stmts, lineno):
b = None
Expand Down
0