Skip to content

Commit

Permalink
Add better type deduction to use-pathlib-is-funcs and `use-pathlib-…
Browse files Browse the repository at this point in the history
…stat` checks
  • Loading branch information
dosisod committed Feb 14, 2024
1 parent aecd60a commit b2df45e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
25 changes: 9 additions & 16 deletions refurb/checks/pathlib/getsize.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from mypy.nodes import BytesExpr, CallExpr, NameExpr, RefExpr, StrExpr, Var
from mypy.nodes import CallExpr, RefExpr

from refurb.checks.common import is_same_type, normalize_os_path
from refurb.checks.common import get_mypy_type, is_same_type, normalize_os_path
from refurb.checks.pathlib.util import is_pathlike
from refurb.error import Error

Expand Down Expand Up @@ -52,21 +52,14 @@ def check(node: CallExpr, errors: list[Error]) -> None:
return

if is_pathlike(arg):
replace = f"x.{new_name}"
new = f"x.{new_name}"

else:
match arg:
case BytesExpr() | StrExpr():
pass

case NameExpr(node=Var(type=ty)) if is_same_type(ty, str, bytes):
pass
elif is_same_type(get_mypy_type(arg), str, bytes):
new = f"Path(x).{new_name}"

case _:
return
else:
return

replace = f"Path(x).{new_name}"
msg = f"Replace `{normalized_name}(x)` with `{new}`"

errors.append(
ErrorInfo.from_node(node, f"Replace `{normalized_name}(x)` with `{replace}`")
)
errors.append(ErrorInfo.from_node(node, msg))
25 changes: 9 additions & 16 deletions refurb/checks/pathlib/is_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from dataclasses import dataclass

from mypy.nodes import BytesExpr, CallExpr, NameExpr, RefExpr, StrExpr, Var
from mypy.nodes import CallExpr, RefExpr

from refurb.checks.common import is_same_type, normalize_os_path
from refurb.checks.common import get_mypy_type, is_same_type, normalize_os_path
from refurb.checks.pathlib.util import is_pathlike
from refurb.error import Error

Expand Down Expand Up @@ -51,21 +51,14 @@ def check(node: CallExpr, errors: list[Error]) -> None:
return

if is_pathlike(arg):
replace = f"x.{new_name}()"
new = f"x.{new_name}()"

else:
match arg:
case BytesExpr() | StrExpr():
pass

case NameExpr(node=Var(type=ty)) if is_same_type(ty, str, bytes):
pass
elif is_same_type(get_mypy_type(arg), str, bytes):
new = f"Path(x).{new_name}()"

case _:
return
else:
return

replace = f"Path(x).{new_name}()"
msg = f"Replace `{normalized_name}(x)` with `{new}`"

errors.append(
ErrorInfo.from_node(node, f"Replace `{normalized_name}(x)` with `{replace}`")
)
errors.append(ErrorInfo.from_node(node, msg))
6 changes: 6 additions & 0 deletions test/data/err_146.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
isfile("filename")
islink("filename")

class C:
p: str

isfile(C().p)


# these should not

os.path.ismount("somefile")
Expand Down
1 change: 1 addition & 0 deletions test/data/err_146.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ test/data/err_146.py:36:1 [FURB146]: Replace `os.path.isabs(x)` with `Path(x).is
test/data/err_146.py:37:1 [FURB146]: Replace `os.path.isdir(x)` with `Path(x).is_dir()`
test/data/err_146.py:38:1 [FURB146]: Replace `os.path.isfile(x)` with `Path(x).is_file()`
test/data/err_146.py:39:1 [FURB146]: Replace `os.path.islink(x)` with `Path(x).is_symlink()`
test/data/err_146.py:44:1 [FURB146]: Replace `os.path.isfile(x)` with `Path(x).is_file()`
6 changes: 6 additions & 0 deletions test/data/err_155.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,11 @@
os.path.getsize(__file__)
getsize("filename")

class C:
s: str

getsize(C().s)


# this should not match
os.path.getsize(1)
1 change: 1 addition & 0 deletions test/data/err_155.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ test/data/err_155.py:23:1 [FURB155]: Replace `os.path.getsize(x)` with `Path(x).
test/data/err_155.py:24:1 [FURB155]: Replace `os.path.getsize(x)` with `x.stat().st_size`
test/data/err_155.py:25:1 [FURB155]: Replace `os.path.getsize(x)` with `Path(x).stat().st_size`
test/data/err_155.py:26:1 [FURB155]: Replace `os.path.getsize(x)` with `Path(x).stat().st_size`
test/data/err_155.py:31:1 [FURB155]: Replace `os.path.getsize(x)` with `Path(x).stat().st_size`

0 comments on commit b2df45e

Please sign in to comment.