1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
from dataclasses import dataclass
from mypy.nodes import CallExpr, RefExpr, StrExpr
from refurb.checks.common import normalize_os_path, stringify
from refurb.error import Error
@dataclass
class ErrorInfo(Error):
"""
The Path() constructor defaults to the current directory, so don't pass the
current directory explicitly.
Bad:
```
file = Path(".")
```
Good:
```
file = Path()
```
Note: Lots of different values can trigger this check, including `"."`,
`""`, `os.curdir`, and `os.path.curdir`.
"""
name = "simplify-path-constructor"
code = 153
categories = ("pathlib", "readability")
def check(node: CallExpr, errors: list[Error]) -> None:
match node:
case CallExpr(
args=[StrExpr(value="." | "" as value)],
callee=RefExpr(fullname="pathlib.Path") as ref,
):
func_name = stringify(ref)
errors.append(
ErrorInfo.from_node(node, f'Replace `{func_name}("{value}")` with `Path()`')
)
match node:
case CallExpr(
args=[RefExpr(fullname=arg) as arg_ref],
callee=RefExpr(fullname="pathlib.Path") as func_ref,
) if ((arg := normalize_os_path(arg)) in {"os.curdir", "os.path.curdir"}):
func_name = stringify(func_ref)
arg_name = stringify(arg_ref)
msg = f"Replace `{func_name}({arg_name})` with `Path()`"
errors.append(ErrorInfo.from_node(node, msg))
|