-
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()`
- Loading branch information
Showing
22 changed files
with
634 additions
and
115 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
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
from pathlib import Path | ||
|
||
# match | ||
_ = Path(".") | ||
|
||
# no match | ||
_ = Path() | ||
print(".") | ||
Path("file.txt") | ||
Path(".", "folder") |
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__) |
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,68 @@ | ||
use rustpython_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> { | ||
// TODO: if pathlib not imported, then add to imports. Remove os import if not referenced | ||
if let ExprKind::Call { func, .. } = &expr.node { | ||
let mut fix = match diagnostic { | ||
DiagnosticKind::PathlibGetcwd(_) => Some(Fix::replacement( | ||
"Path.cwd".to_string(), | ||
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 to | ||
let arg = args.first().unwrap(); | ||
let contents = checker.locator.slice_source_code_range(&Range::new( | ||
arg.location, | ||
arg.end_location.unwrap(), | ||
)); | ||
let output = apply_fix(&fixme, &Locator::new(contents)); | ||
|
||
fix = Some(Fix::replacement( | ||
output, | ||
parent.location, | ||
parent.end_location.unwrap(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
fix | ||
} 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; |
51 changes: 51 additions & 0 deletions
51
src/rules/flake8_use_pathlib/rules/simplify_path_constructor.rs
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,51 @@ | ||
use ruff_macros::derive_message_formats; | ||
use rustpython_ast::{Constant, Expr, ExprKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::fix::Fix; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::AlwaysAutofixableViolation; | ||
|
||
define_violation!( | ||
pub struct SimplifyPathConstructor; | ||
); | ||
impl AlwaysAutofixableViolation for SimplifyPathConstructor { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Do not pass the current directory explicitly to `Path`") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Replace `Path(\".\")` with `Path()`") | ||
} | ||
} | ||
|
||
/// PTH200 | ||
pub fn simplify_path_constructor(checker: &mut Checker, expr: &Expr) { | ||
if checker.resolve_call_path(expr).map_or(false, |call_path| { | ||
call_path.as_slice() == ["pathlib", "Path"] | ||
}) { | ||
if let ExprKind::Call { args, keywords, .. } = &expr.node { | ||
if keywords.is_empty() && args.len() == 1 { | ||
let arg = &args.first().unwrap(); | ||
if let ExprKind::Constant { | ||
value: Constant::Str(value), | ||
.. | ||
} = &arg.node | ||
{ | ||
if value == "." { | ||
let mut diagnostic = | ||
Diagnostic::new(SimplifyPathConstructor, Range::from_located(expr)); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic | ||
.amend(Fix::deletion(arg.location, arg.end_location.unwrap())); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
}; | ||
}; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ expression: diagnostics | |
column: 4 | ||
end_location: | ||
row: 3 | ||
column: 17 | ||
column: 27 | ||
fix: ~ | ||
parent: ~ | ||
|
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ expression: diagnostics | |
column: 4 | ||
end_location: | ||
row: 3 | ||
column: 8 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
|
Oops, something went wrong.