-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flake8-use-pathlib autofix and new rules
PTH200-204: - Simplify path constructor `Path(".")` to `Path()` - Replace `os.path.getsize` with `Path.stat()` Autofix for: - PTH200: `Path(".")` to `Path()` - PTH109: `os.path.getcwd(x)` to `Path(x).cwd()` Promote `get_member_import_name_alias` from `pylint` to `ast.helpers`
- Loading branch information
Showing
29 changed files
with
1,071 additions
and
269 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
1 change: 1 addition & 0 deletions
1
crates/ruff/resources/test/fixtures/flake8_use_pathlib/full_name.py
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import os | ||
import os.path | ||
import pathlib | ||
|
||
p = "/foo" | ||
|
||
|
34 changes: 34 additions & 0 deletions
34
crates/ruff/resources/test/fixtures/flake8_use_pathlib/guarded.py
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,34 @@ | ||
# ensure that no fixes are applied when`pathlib` is not imported | ||
# (needed until this item is resolved: https://github.com/charliermarsh/ruff/issues/835) | ||
import os | ||
import os.path | ||
|
||
p = "/foo" | ||
|
||
a = os.path.abspath(p) | ||
aa = os.chmod(p) | ||
aaa = os.mkdir(p) | ||
os.makedirs(p) | ||
os.rename(p) | ||
os.replace(p) | ||
os.rmdir(p) | ||
os.remove(p) | ||
os.unlink(p) | ||
os.getcwd(p) | ||
b = os.path.exists(p) | ||
bb = os.path.expanduser(p) | ||
bbb = os.path.isdir(p) | ||
bbbb = os.path.isfile(p) | ||
bbbbb = os.path.islink(p) | ||
os.readlink(p) | ||
os.stat(p) | ||
os.path.isabs(p) | ||
os.path.join(p) | ||
os.path.basename(p) | ||
os.path.dirname(p) | ||
os.path.samefile(p) | ||
os.path.splitext(p) | ||
with open(p) as fp: | ||
fp.read() | ||
open(p).close() | ||
os.getcwdb(p) |
1 change: 1 addition & 0 deletions
1
crates/ruff/resources/test/fixtures/flake8_use_pathlib/import_as.py
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import os as foo | ||
import os.path as foo_p | ||
import pathlib as pth | ||
|
||
p = "/foo" | ||
|
||
|
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
12 changes: 12 additions & 0 deletions
12
crates/ruff/resources/test/fixtures/flake8_use_pathlib/simplify_pathlib_constructor.py
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,12 @@ | ||
from pathlib import Path | ||
from pathlib import Path as pth | ||
|
||
# match | ||
_ = Path(".") | ||
_ = pth(".") | ||
|
||
# no match | ||
_ = Path() | ||
print(".") | ||
Path("file.txt") | ||
Path(".", "folder") |
19 changes: 19 additions & 0 deletions
19
crates/ruff/resources/test/fixtures/flake8_use_pathlib/stat.py
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 os | ||
from pathlib import Path | ||
|
||
os.path.getatime("filename") | ||
os.path.getatime(b"filename") | ||
os.path.getatime(Path("filename")) | ||
|
||
os.path.getmtime("filename") | ||
os.path.getmtime(b"filename") | ||
os.path.getmtime(Path("filename")) | ||
|
||
os.path.getctime("filename") | ||
os.path.getctime(b"filename") | ||
os.path.getctime(Path("filename")) | ||
|
||
os.path.getsize("filename") | ||
os.path.getsize(b"filename") | ||
os.path.getsize(Path("filename")) | ||
os.path.getsize(__file__) |
15 changes: 15 additions & 0 deletions
15
crates/ruff/resources/test/fixtures/flake8_use_pathlib/use_pathlib.py
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 |
---|---|---|
@@ -1,3 +1,18 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
(Path("") / "").open() | ||
|
||
_ = Path(os.getcwd()) | ||
|
||
_ = Path( | ||
os.\ | ||
getcwd() | ||
) | ||
|
||
_ = Path( | ||
os.getcwdb(), | ||
) | ||
|
||
# should not be unwrapped | ||
_ = Path(os.getcwd(), hello='world') |
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
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,70 @@ | ||
use rustpython_parser::ast::{Expr, ExprKind}; | ||
|
||
use crate::ast::helpers; | ||
use crate::ast::types::Range; | ||
use crate::autofix::apply_fix; | ||
use crate::checkers::ast::Checker; | ||
use crate::fix::Fix; | ||
use crate::registry::DiagnosticKind; | ||
use crate::source_code::Locator; | ||
|
||
pub fn pathlib_fix( | ||
checker: &mut Checker, | ||
diagnostic: &DiagnosticKind, | ||
expr: &Expr, | ||
parent: Option<&Expr>, | ||
) -> Option<Fix> { | ||
// Guard that Path is imported, `content` contains the name or aliaas | ||
if let Some(content) = helpers::get_member_import_name_alias(checker, "pathlib", "Path") { | ||
if let ExprKind::Call { func, .. } = &expr.node { | ||
let mut fix = match diagnostic { | ||
DiagnosticKind::PathlibGetcwd(_) => Some(Fix::replacement( | ||
format!("{content}.cwd"), | ||
func.location, | ||
func.end_location.unwrap(), | ||
)), | ||
_ => None, | ||
}; | ||
|
||
// Wrapped in a `Path()` call | ||
if let Some(fixme) = fix.clone() { | ||
if let Some(parent) = parent { | ||
if checker | ||
.resolve_call_path(parent) | ||
.map_or(false, |call_path| { | ||
call_path.as_slice() == ["pathlib", "Path"] | ||
}) | ||
{ | ||
if let ExprKind::Call { args, keywords, .. } = &parent.node { | ||
if args.len() == 1 && keywords.is_empty() { | ||
// Reset the line index | ||
let fixme = Fix::replacement( | ||
fixme.content.to_string(), | ||
helpers::to_relative(fixme.location, expr.location), | ||
helpers::to_relative(fixme.end_location, expr.location), | ||
); | ||
|
||
// Apply the fix | ||
let arg = args.first().unwrap(); | ||
let contents = checker.locator.slice_source_code_range( | ||
&Range::new(arg.location, arg.end_location.unwrap()), | ||
); | ||
|
||
fix = Some(Fix::replacement( | ||
apply_fix(&fixme, &Locator::new(contents)), | ||
parent.location, | ||
parent.end_location.unwrap(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fix | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} |
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
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,3 @@ | ||
pub use simplify_path_constructor::{simplify_path_constructor, SimplifyPathConstructor}; | ||
|
||
mod simplify_path_constructor; |
Oops, something went wrong.