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 59 60 61 62 63 64 65 66
|
from dataclasses import dataclass
from mypy.nodes import CallExpr, NameExpr, StrExpr
from refurb.checks.pathlib.util import is_pathlike
from refurb.error import Error
@dataclass
class ErrorInfo(Error):
"""
When you want to open a Path object, don't pass it to `open()`, just call
`.open()` on the Path object itself:
Bad:
```
path = Path("filename")
with open(path) as f:
pass
```
Good:
```
path = Path("filename")
with path.open() as f:
pass
```
"""
name = "use-pathlib-open"
code = 117
categories = ("pathlib",)
def check(node: CallExpr, errors: list[Error]) -> None:
match node:
case CallExpr(
callee=NameExpr(fullname="builtins.open") as open_node,
args=[
CallExpr(
callee=NameExpr(fullname="builtins.str"),
args=[arg],
)
| arg,
*rest,
],
) if is_pathlike(arg):
mode = args = ""
match rest:
case [StrExpr(value=value), *_]:
mode = f'"{value}"'
args = f", {mode}"
expr = "x" if arg == node.args[0] else "str(x)"
errors.append(
ErrorInfo.from_node(
open_node,
f"Replace `open({expr}{args})` with `x.open({mode})`",
)
)
|