Skip to content

Commit

Permalink
Use original source for _simple_arg
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed May 2, 2020
1 parent 7602e2b commit 842c971
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions pyupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,16 +2190,11 @@ def _replace(i: int, mapping: Dict[str, str], node: NameOrAttr) -> None:
return tokens_to_src(tokens)


def _simple_arg(arg: ast.expr) -> bool:
return (
isinstance(arg, ast.Name) or
(isinstance(arg, ast.Attribute) and _simple_arg(arg.value)) or
(
isinstance(arg, ast.Call) and
_simple_arg(arg.func) and
not arg.args and not arg.keywords
)
)
def _simple_arg(arg: ast.expr, lines: List[str]) -> bool:
if arg.lineno != arg.end_lineno:
return False
s = _unparse(arg, lines)
return '\\' not in s and "'" not in s and '"' not in s


def _starargs(call: ast.Call) -> bool:
Expand Down Expand Up @@ -2256,8 +2251,10 @@ def _parse(self, node: ast.Call) -> Optional[Tuple[DotFormatPart, ...]]:
isinstance(node.func, ast.Attribute) and
isinstance(node.func.value, ast.Str) and
node.func.attr == 'format' and
all(_simple_arg(arg) for arg in node.args) and
all(_simple_arg(k.value) for k in node.keywords) and
all(_simple_arg(arg, self.lines) for arg in node.args) and
all(
_simple_arg(k.value, self.lines) for k in node.keywords
) and
not _starargs(node)
):
return None
Expand Down

0 comments on commit 842c971

Please sign in to comment.