-
-
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.
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from dataclasses import dataclass | ||
|
||
from mypy.nodes import CallExpr, ListExpr, MemberExpr, NameExpr, StrExpr | ||
|
||
from refurb.checks.common import stringify | ||
from refurb.error import Error | ||
from refurb.checks.string.use_fstring_fmt import CONVERSIONS as FURB_119_FUNCS | ||
|
||
|
||
@dataclass | ||
class ErrorInfo(Error): | ||
""" | ||
If you want to stringify a single value without concatenating anything, use | ||
the `str()` function instead. | ||
Bad: | ||
``` | ||
nums = [123, 456] | ||
num = f"{num[0]}") | ||
``` | ||
Good: | ||
``` | ||
nums = [123, 456] | ||
num = str(num[0]) | ||
``` | ||
""" | ||
|
||
name = "use-str-func" | ||
code = 183 | ||
categories = ("readability",) | ||
|
||
|
||
ignore = set[int]() | ||
|
||
|
||
def check(node: CallExpr, errors: list[Error]) -> None: | ||
if id(node) in ignore: | ||
return | ||
|
||
match node: | ||
case CallExpr( | ||
callee=MemberExpr( | ||
expr=StrExpr(value=""), | ||
name="join", | ||
), | ||
args=[ListExpr(items=items)], | ||
): | ||
ignore.update(id(item) for item in items) | ||
|
||
case CallExpr( | ||
callee=MemberExpr( | ||
expr=StrExpr(value="{:{}}"), | ||
name="format", | ||
), | ||
args=[arg, StrExpr(value="")], | ||
): | ||
match arg: | ||
case CallExpr(callee=NameExpr(fullname=fn)) if fn in FURB_119_FUNCS: | ||
return | ||
|
||
x = stringify(arg) | ||
|
||
msg = f'Replace `f"{{{x}}}"` with `str({x})`' | ||
|
||
errors.append(ErrorInfo.from_node(node, msg)) |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
# these will not | ||
|
||
f"{123}" | ||
f"{123}" # noqa: FURB183 | ||
|
||
f"{0b1010:b}" | ||
|
||
|
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,12 @@ | ||
x = " " | ||
|
||
# these should match | ||
|
||
f"{x}" | ||
f"{123}" | ||
|
||
|
||
# these should not | ||
|
||
f"hello{x}world" | ||
f"{x} {x}" |
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,2 @@ | ||
test/data/err_183.py:5:1 [FURB183]: Replace `f"{x}"` with `str(x)` | ||
test/data/err_183.py:6:1 [FURB183]: Replace `f"{123}"` with `str(123)` |