Skip to content

Commit

Permalink
Check for os.curdir and os.path.curdir in FURB153
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Oct 15, 2023
1 parent 19f80c8 commit 8d77d44
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -1417,7 +1417,7 @@ def area(r: float) -> float:
Categories: `pathlib` `readability`

The Path() constructor defaults to the current directory, so don't pass the
current directory (".") explicitly.
current directory explicitly.

Bad:

Expand All @@ -1431,6 +1431,9 @@ Good:
file = Path()
```

Note: Lots of different values can trigger this check, including `"."`,
`""`, `os.curdir`, and `os.path.curdir`.

## FURB154: `simplify-global-and-nonlocal`

Categories: `builtin` `readability`
Expand Down
21 changes: 19 additions & 2 deletions refurb/checks/pathlib/simplify_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

from mypy.nodes import CallExpr, RefExpr, StrExpr

from refurb.checks.common import stringify
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.
current directory explicitly.
Bad:
Expand All @@ -23,6 +23,9 @@ class ErrorInfo(Error):
```
file = Path()
```
Note: Lots of different values can trigger this check, including `"."`,
`""`, `os.curdir`, and `os.path.curdir`.
"""

name = "simplify-path-constructor"
Expand All @@ -43,3 +46,17 @@ def check(node: CallExpr, errors: list[Error]) -> None:
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))
7 changes: 7 additions & 0 deletions test/data/err_153.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import os
import pathlib
from pathlib import Path
from os import path, curdir

# these should match

_ = Path(".")
_ = Path("")
_ = pathlib.Path(".")
_ = Path(os.curdir)
_ = Path(curdir)
_ = Path(os.path.curdir)
_ = Path(path.curdir)
_ = pathlib.Path(curdir)


# these should not
Expand Down
11 changes: 8 additions & 3 deletions test/data/err_153.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
test/data/err_153.py:6:5 [FURB153]: Replace `Path(".")` with `Path()`
test/data/err_153.py:7:5 [FURB153]: Replace `Path("")` with `Path()`
test/data/err_153.py:8:5 [FURB153]: Replace `pathlib.Path(".")` with `Path()`
test/data/err_153.py:8:5 [FURB153]: Replace `Path(".")` with `Path()`
test/data/err_153.py:9:5 [FURB153]: Replace `Path("")` with `Path()`
test/data/err_153.py:10:5 [FURB153]: Replace `pathlib.Path(".")` with `Path()`
test/data/err_153.py:11:5 [FURB153]: Replace `Path(os.curdir)` with `Path()`
test/data/err_153.py:12:5 [FURB153]: Replace `Path(curdir)` with `Path()`
test/data/err_153.py:13:5 [FURB153]: Replace `Path(os.path.curdir)` with `Path()`
test/data/err_153.py:14:5 [FURB153]: Replace `Path(path.curdir)` with `Path()`
test/data/err_153.py:15:5 [FURB153]: Replace `pathlib.Path(curdir)` with `Path()`

0 comments on commit 8d77d44

Please sign in to comment.