From 63c606151216c4368c4fd5ad20c29fcba3c48b3f Mon Sep 17 00:00:00 2001 From: dosisod <39638017+dosisod@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:32:32 -0800 Subject: [PATCH] Cleanup existing checks --- refurb/checks/common.py | 23 ++++++++++++++----- .../itertools/use_chain_from_iterable.py | 9 +++----- refurb/checks/readability/use_dict_union.py | 15 +++--------- refurb/checks/readability/use_sort.py | 6 +---- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/refurb/checks/common.py b/refurb/checks/common.py index 098801d..6f0f1f4 100644 --- a/refurb/checks/common.py +++ b/refurb/checks/common.py @@ -336,12 +336,23 @@ def _stringify(node: Node) -> str: return f"({inner})" - case CallExpr(): - name = _stringify(node.callee) + case CallExpr(arg_names=arg_names, arg_kinds=arg_kinds, args=args): + call_args: list[str] = [] - args = ", ".join(_stringify(arg) for arg in node.args) + for arg_name, kind, arg in zip(arg_names, arg_kinds, args): + if kind == ArgKind.ARG_NAMED: + call_args.append(f"{arg_name}={_stringify(arg)}") - return f"{name}({args})" + elif kind == ArgKind.ARG_STAR: + call_args.append(f"*{_stringify(arg)}") + + elif kind == ArgKind.ARG_STAR2: + call_args.append(f"**{_stringify(arg)}") + + else: + call_args.append(_stringify(arg)) + + return f"{_stringify(node.callee)}({', '.join(call_args)})" case IndexExpr(base=base, index=index): return f"{stringify(base)}[{stringify(index)}]" @@ -378,10 +389,10 @@ def _stringify(node: Node) -> str: body=Block(body=[ReturnStmt(expr=Expression() as expr)]), ) if (all(kind == ArgKind.ARG_POS for kind in arg_kinds) and all(arg_names)): if arg_names: - args = " " + args = " " # type: ignore args += ", ".join(arg_names) # type: ignore else: - args = "" + args = "" # type: ignore body = _stringify(expr) diff --git a/refurb/checks/itertools/use_chain_from_iterable.py b/refurb/checks/itertools/use_chain_from_iterable.py index aff47d7..903af98 100644 --- a/refurb/checks/itertools/use_chain_from_iterable.py +++ b/refurb/checks/itertools/use_chain_from_iterable.py @@ -6,7 +6,6 @@ GeneratorExpr, ListComprehension, ListExpr, - NameExpr, RefExpr, SetComprehension, ) @@ -110,7 +109,7 @@ def check( callee=RefExpr(fullname="builtins.sum"), args=[arg, ListExpr(items=[])], ): - old = f"sum({stringify(arg)}, [])" + old = stringify(node) new = f"chain.from_iterable({stringify(arg)})" case CallExpr( @@ -132,10 +131,8 @@ def check( args=[arg], arg_kinds=[ArgKind.ARG_STAR], ): - chain = "chain" if isinstance(callee, NameExpr) else "itertools.chain" - - old = f"{chain}(*{stringify(arg)})" - new = f"{chain}.from_iterable({stringify(arg)})" + old = stringify(node) + new = f"{stringify(callee)}.from_iterable({stringify(arg)})" case _: return diff --git a/refurb/checks/readability/use_dict_union.py b/refurb/checks/readability/use_dict_union.py index 619f8ad..febabf5 100644 --- a/refurb/checks/readability/use_dict_union.py +++ b/refurb/checks/readability/use_dict_union.py @@ -107,16 +107,12 @@ def check(node: DictExpr | CallExpr, errors: list[Error], settings: Settings) -> errors.append(ErrorInfo.from_node(node, msg)) case CallExpr(callee=RefExpr(fullname="builtins.dict")): - old = [] args: list[str] = [] kwargs: dict[str, str] = {} - # ignore dict(x) since that is covered by FURB123 + # ignore dict(x) and dict() since that is covered by FURB123 match node.arg_kinds: - case []: - return - - case [ArgKind.ARG_POS]: + case [] | [ArgKind.ARG_POS]: return # TODO: move dict(a=1, b=2) to FURB112 @@ -129,8 +125,6 @@ def check(node: DictExpr | CallExpr, errors: list[Error], settings: Settings) -> return if kind == ArgKind.ARG_STAR2: - old.append(f"**{stringify(arg)}") - stringified_arg = stringify(arg) if len(node.args) == 1: @@ -141,15 +135,12 @@ def check(node: DictExpr | CallExpr, errors: list[Error], settings: Settings) -> args.append(stringified_arg) elif name: - old.append(f"{name}={stringify(arg)}") kwargs[name] = stringify(arg) else: - old.append(stringify(arg)) args.append(stringify(arg)) - inner = ", ".join(old) - old_msg = f"dict({inner})" + old_msg = stringify(node) if kwargs: kwargs2 = ", ".join(f'"{name}": {expr}' for name, expr in kwargs.items()) diff --git a/refurb/checks/readability/use_sort.py b/refurb/checks/readability/use_sort.py index f0a7f52..4dfb7f0 100644 --- a/refurb/checks/readability/use_sort.py +++ b/refurb/checks/readability/use_sort.py @@ -52,21 +52,17 @@ def check(node: AssignmentStmt, errors: list[Error]) -> None: and str(ty).startswith("builtins.list[") and all(arg_kind == ArgKind.ARG_NAMED for arg_kind in arg_kinds) ): - old_args: list[str] = [] new_args: list[str] = [] name = stringify(assign_ref) - old_args.append(name) - if rest: for arg_name, expr in zip(arg_names, rest): arg = f"{arg_name}={stringify(expr)}" - old_args.append(arg) new_args.append(arg) - old = f"{name} = sorted({', '.join(old_args)})" + old = stringify(node) new = f"{name}.sort({', '.join(new_args)})" msg = f"Replace `{old}` with `{new}`"