Kaydet (Commit) 82399a26 authored tarafından Anthony Sottile's avatar Anthony Sottile Kaydeden (comit) GitHub

Merge pull request #15 from asottile/simplify_funcs

Simplify functions now that we are handling unhug of (
...@@ -16,7 +16,7 @@ from tokenize_rt import UNIMPORTANT_WS ...@@ -16,7 +16,7 @@ from tokenize_rt import UNIMPORTANT_WS
Offset = collections.namedtuple('Offset', ('line', 'utf8_byte_offset')) Offset = collections.namedtuple('Offset', ('line', 'utf8_byte_offset'))
Call = collections.namedtuple('Call', ('node', 'star_args', 'arg_offsets')) Call = collections.namedtuple('Call', ('node', 'star_args', 'arg_offsets'))
Func = collections.namedtuple('Func', ('node', 'star_args', 'arg_offsets')) Func = collections.namedtuple('Func', ('node', 'arg_offsets'))
Literal = collections.namedtuple('Literal', ('node', 'backtrack')) Literal = collections.namedtuple('Literal', ('node', 'backtrack'))
Literal.__new__.__defaults__ = (False,) Literal.__new__.__defaults__ = (False,)
Fix = collections.namedtuple('Fix', ('braces', 'multi_arg', 'initial_indent')) Fix = collections.namedtuple('Fix', ('braces', 'multi_arg', 'initial_indent'))
...@@ -138,34 +138,24 @@ class FindNodes(ast.NodeVisitor): ...@@ -138,34 +138,24 @@ class FindNodes(ast.NodeVisitor):
self.generic_visit(node) self.generic_visit(node)
def visit_FunctionDef(self, node): def visit_FunctionDef(self, node):
has_starargs = False has_starargs = (
args = list(node.args.args) node.args.vararg or node.args.kwarg or
# python 3 only
if node.args.vararg: getattr(node.args, 'kwonlyargs', None)
if isinstance(node.args.vararg, ast.AST): # pragma: no cover (py3) )
args.append(node.args.vararg)
has_starargs = True
if node.args.kwarg:
if isinstance(node.args.kwarg, ast.AST): # pragma: no cover (py3)
args.append(node.args.kwarg)
has_starargs = True
py3_kwonlyargs = getattr(node.args, 'kwonlyargs', None)
if py3_kwonlyargs: # pragma: no cover (py3)
args.extend(py3_kwonlyargs)
has_starargs = True
orig = node.lineno orig = node.lineno
is_multiline = False is_multiline = False
offsets = set() offsets = set()
for argnode in args: for argnode in node.args.args:
offset = _to_offset(argnode) offset = _to_offset(argnode)
if offset.line > orig: if offset.line > orig:
is_multiline = True is_multiline = True
offsets.add(offset) offsets.add(offset)
if is_multiline: if is_multiline and not has_starargs:
key = Offset(node.lineno, node.col_offset) key = Offset(node.lineno, node.col_offset)
self.funcs[key] = Func(node, has_starargs, offsets) self.funcs[key] = Func(node, offsets)
self.generic_visit(node) self.generic_visit(node)
...@@ -359,8 +349,6 @@ def _fix_src(contents_text, py35_plus): ...@@ -359,8 +349,6 @@ def _fix_src(contents_text, py35_plus):
fix_data = _find_call(call, i, tokens) fix_data = _find_call(call, i, tokens)
elif key in visitor.funcs: elif key in visitor.funcs:
func = visitor.funcs[key] func = visitor.funcs[key]
# any amount of starargs excludes adding a comma for defs
add_comma = not func.star_args
# functions can be treated as calls # functions can be treated as calls
fix_data = _find_call(func, i, tokens) fix_data = _find_call(func, i, tokens)
# Handle parenthesized things # Handle parenthesized things
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment