-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was a small nitpick I found myself doing when I was merging two dicts together. Can't say how common this pattern is, though I did get a few hits on grep.app.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from dataclasses import dataclass | ||
|
||
from mypy.nodes import CallExpr, Expression, MemberExpr, OpExpr, RefExpr, Var | ||
|
||
from refurb.checks.common import stringify | ||
from refurb.error import Error | ||
|
||
|
||
@dataclass | ||
class ErrorInfo(Error): | ||
""" | ||
You don't need to call `.clone()` on a dict/set when using it in a union | ||
since the original dict/set is not modified. | ||
Bad: | ||
``` | ||
d = {"a": 1} | ||
merged = d.clone() | {"b": 2} | ||
``` | ||
Good: | ||
``` | ||
d = {"a": 1} | ||
merged = d | {"b": 2} | ||
``` | ||
""" | ||
|
||
name = "no-copy-with-merge" | ||
categories = ("readability",) | ||
code = 185 | ||
|
||
|
||
UNIONABLE_TYPES = ("builtins.dict[", "builtins.set[") | ||
|
||
|
||
ignored_nodes = set[int]() | ||
|
||
|
||
def check_expr(expr: Expression, errors: list[Error]) -> None: | ||
if id(expr) in ignored_nodes: | ||
return | ||
|
||
match expr: | ||
case CallExpr( | ||
callee=MemberExpr( | ||
expr=RefExpr(node=Var(type=ty)) as ref, | ||
name="copy", | ||
), | ||
args=[], | ||
) if str(ty).startswith(UNIONABLE_TYPES): | ||
msg = f"Replace `{stringify(ref)}.clone()` with `{stringify(ref)}`" | ||
|
||
errors.append(ErrorInfo.from_node(expr, msg)) | ||
|
||
case OpExpr(left=lhs, op="|", right=rhs): | ||
check_expr(lhs, errors) | ||
check_expr(rhs, errors) | ||
|
||
ignored_nodes.add(id(expr)) | ||
|
||
|
||
def check(node: OpExpr, errors: list[Error]) -> None: | ||
check_expr(node, errors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
x = {} | ||
y = set() | ||
|
||
# these should match | ||
_ = x.copy() | {} | ||
_ = {} | x.copy() | ||
|
||
_ = y.copy() | set() | ||
_ = set() | y.copy() | ||
|
||
_ = x.copy() | {} | x.copy() | ||
|
||
|
||
|
||
class C: | ||
def copy(self) -> dict: | ||
return {} | ||
|
||
c = C() | ||
|
||
|
||
# these should not | ||
_ = c.copy() | {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
test/data/err_185.py:5:5 [FURB185]: Replace `x.clone()` with `x` | ||
test/data/err_185.py:6:10 [FURB185]: Replace `x.clone()` with `x` | ||
test/data/err_185.py:8:5 [FURB185]: Replace `y.clone()` with `y` | ||
test/data/err_185.py:9:13 [FURB185]: Replace `y.clone()` with `y` | ||
test/data/err_185.py:11:5 [FURB185]: Replace `x.clone()` with `x` | ||
test/data/err_185.py:11:21 [FURB185]: Replace `x.clone()` with `x` |