-
-
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
4 changed files
with
89 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,47 @@ | ||
from dataclasses import dataclass | ||
|
||
from mypy.nodes import CallExpr, MemberExpr, RefExpr, StrExpr | ||
|
||
from refurb.error import Error | ||
|
||
|
||
@dataclass | ||
class ErrorInfo(Error): | ||
""" | ||
If you want to get the current working directory don't call `resolve()` on | ||
an empty `Path()` object, use `Path.cwd()` instead. | ||
Bad: | ||
``` | ||
cwd = Path().resolve() | ||
``` | ||
Good: | ||
``` | ||
cwd = Path.cwd() | ||
``` | ||
""" | ||
|
||
name = "no-implicit-cwd" | ||
code = 177 | ||
categories = ("pathlib",) | ||
|
||
|
||
def check(node: CallExpr, errors: list[Error]) -> None: | ||
match node: | ||
case CallExpr( | ||
callee=MemberExpr( | ||
expr=CallExpr( | ||
callee=RefExpr(fullname="pathlib.Path"), | ||
args=[] | [StrExpr(value="" | ".")] as args, | ||
), | ||
name="resolve", | ||
), | ||
args=[], | ||
): | ||
arg = f'"{args[0].value}"' if args else "" # type: ignore | ||
msg = f"Replace `Path({arg}).resolve()` with `Path.cwd()`" | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pathlib | ||
from pathlib import Path | ||
|
||
# these should match | ||
|
||
_ = Path().resolve() | ||
_ = Path("").resolve() # noqa: FURB153 | ||
_ = Path(".").resolve() # noqa: FURB153 | ||
_ = pathlib.Path().resolve() | ||
|
||
|
||
# these should not | ||
|
||
_ = Path("some_file").resolve() | ||
_ = Path().resolve(True) | ||
_ = Path().resolve(False) # noqa: FURB120 | ||
|
||
p = Path() | ||
_ = p.resolve() |
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,4 @@ | ||
test/data/err_177.py:6:5 [FURB177]: Replace `Path().resolve()` with `Path.cwd()` | ||
test/data/err_177.py:7:5 [FURB177]: Replace `Path("").resolve()` with `Path.cwd()` | ||
test/data/err_177.py:8:5 [FURB177]: Replace `Path(".").resolve()` with `Path.cwd()` | ||
test/data/err_177.py:9:5 [FURB177]: Replace `Path().resolve()` with `Path.cwd()` |