Skip to content

Commit

Permalink
Replace .clone() with .copy():
Browse files Browse the repository at this point in the history
I don't know how I let that one slip past, I must've been half asleep.
  • Loading branch information
dosisod committed Jan 8, 2024
1 parent d583934 commit a295cee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
27 changes: 25 additions & 2 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2238,15 +2238,15 @@ def process(file_name: str):

Categories: `readability`

You don't need to call `.clone()` on a dict/set when using it in a union
You don't need to call `.copy()` on a dict/set when using it in a union
since the original dict/set is not modified.

Bad:

```python
d = {"a": 1}

merged = d.clone() | {"b": 2}
merged = d.copy() | {"b": 2}
```

Good:
Expand All @@ -2255,4 +2255,27 @@ Good:
d = {"a": 1}

merged = d | {"b": 2}
```

## FURB186: `use-sort`

Categories: `performance` `readability`

Don't use `sorted()` to sort a list and reassign it to itself, use the
faster in-place `.sort()` method instead.

Bad:

```python
names = ["Bob", "Alice", "Charlie"]

names = sorted(names)
```

Good:

```python
names = ["Bob", "Alice", "Charlie"]

names.sort()
```
6 changes: 3 additions & 3 deletions refurb/checks/readability/no_copy_with_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
@dataclass
class ErrorInfo(Error):
"""
You don't need to call `.clone()` on a dict/set when using it in a union
You don't need to call `.copy()` 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}
merged = d.copy() | {"b": 2}
```
Good:
Expand Down Expand Up @@ -52,7 +52,7 @@ def check_expr(expr: Expression, errors: list[Error]) -> None:
),
args=[],
) if str(ty).startswith(UNIONABLE_TYPES):
msg = f"Replace `{stringify(ref)}.clone()` with `{stringify(ref)}`"
msg = f"Replace `{stringify(ref)}.copy()` with `{stringify(ref)}`"

errors.append(ErrorInfo.from_node(expr, msg))

Expand Down
12 changes: 6 additions & 6 deletions test/data/err_185.txt
Original file line number Diff line number Diff line change
@@ -1,6 +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`
test/data/err_185.py:5:5 [FURB185]: Replace `x.copy()` with `x`
test/data/err_185.py:6:10 [FURB185]: Replace `x.copy()` with `x`
test/data/err_185.py:8:5 [FURB185]: Replace `y.copy()` with `y`
test/data/err_185.py:9:13 [FURB185]: Replace `y.copy()` with `y`
test/data/err_185.py:11:5 [FURB185]: Replace `x.copy()` with `x`
test/data/err_185.py:11:21 [FURB185]: Replace `x.copy()` with `x`

0 comments on commit a295cee

Please sign in to comment.