-
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.
Implements #5905 (comment) --------- Co-authored-by: konsti <konstin@mailbox.org>
- Loading branch information
Showing
8 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH206.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,20 @@ | ||
import os | ||
from os import sep | ||
|
||
|
||
file_name = "foo/bar" | ||
|
||
# PTH206 | ||
"foo/bar/".split(os.sep) | ||
"foo/bar/".split(sep=os.sep) | ||
"foo/bar/".split(os.sep)[-1] | ||
"foo/bar/".split(os.sep)[-2] | ||
"foo/bar/".split(os.sep)[-2:] | ||
"fizz/buzz".split(sep) | ||
"fizz/buzz".split(sep)[-1] | ||
os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
file_name.split(os.sep) | ||
(os.path.abspath(file_name)).split(os.sep) | ||
|
||
# OK | ||
"foo/bar/".split("/") |
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
98 changes: 98 additions & 0 deletions
98
crates/ruff/src/rules/flake8_use_pathlib/rules/os_sep_split.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,98 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::find_keyword; | ||
use rustpython_parser::ast::{Expr, ExprAttribute, Keyword, Ranged}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `.split(os.sep)` | ||
/// | ||
/// ## Why is this bad? | ||
/// The `pathlib` module in the standard library should be used for path | ||
/// manipulation. It provides a high-level API with the functionality | ||
/// needed for common operations on `Path` objects. | ||
/// | ||
/// ## Example | ||
/// If not all parts of the path are needed, then the `name` and `parent` | ||
/// attributes of the `Path` object should be used. Otherwise, the `parts` | ||
/// attribute can be used as shown in the last example. | ||
/// ```python | ||
/// import os | ||
/// | ||
/// "path/to/file_name.txt".split(os.sep)[-1] | ||
/// | ||
/// "path/to/file_name.txt".split(os.sep)[-2] | ||
/// | ||
/// # Iterating over the path parts | ||
/// if any(part in blocklist for part in "my/file/path".split(os.sep)): | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from pathlib import Path | ||
/// | ||
/// Path("path/to/file_name.txt").name | ||
/// | ||
/// Path("path/to/file_name.txt").parent.name | ||
/// | ||
/// # Iterating over the path parts | ||
/// if any(part in blocklist for part in Path("my/file/path").parts): | ||
/// ... | ||
/// ``` | ||
#[violation] | ||
pub struct OsSepSplit; | ||
|
||
impl Violation for OsSepSplit { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Replace `.split(os.sep)` with `Path.parts`") | ||
} | ||
} | ||
|
||
/// PTH206 | ||
pub(crate) fn os_sep_split( | ||
checker: &mut Checker, | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
) { | ||
let Expr::Attribute(ExprAttribute { attr, .. }) = func else { | ||
return; | ||
}; | ||
|
||
if attr.as_str() != "split" { | ||
return; | ||
}; | ||
|
||
let sep = if !args.is_empty() { | ||
// `.split(os.sep)` | ||
let [arg] = args else { | ||
return; | ||
}; | ||
arg | ||
} else if !keywords.is_empty() { | ||
// `.split(sep=os.sep)` | ||
let Some(keyword) = find_keyword(keywords, "sep") else { | ||
return; | ||
}; | ||
&keyword.value | ||
} else { | ||
return; | ||
}; | ||
|
||
if !checker | ||
.semantic() | ||
.resolve_call_path(sep) | ||
.map_or(false, |call_path| { | ||
matches!(call_path.as_slice(), ["os", "sep"]) | ||
}) | ||
{ | ||
return; | ||
} | ||
|
||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(OsSepSplit, attr.range())); | ||
} |
102 changes: 102 additions & 0 deletions
102
...lake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH206_PTH206.py.snap
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,102 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_use_pathlib/mod.rs | ||
--- | ||
PTH206.py:8:12: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
7 | # PTH206 | ||
8 | "foo/bar/".split(os.sep) | ||
| ^^^^^ PTH206 | ||
9 | "foo/bar/".split(sep=os.sep) | ||
10 | "foo/bar/".split(os.sep)[-1] | ||
| | ||
|
||
PTH206.py:9:12: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
7 | # PTH206 | ||
8 | "foo/bar/".split(os.sep) | ||
9 | "foo/bar/".split(sep=os.sep) | ||
| ^^^^^ PTH206 | ||
10 | "foo/bar/".split(os.sep)[-1] | ||
11 | "foo/bar/".split(os.sep)[-2] | ||
| | ||
|
||
PTH206.py:10:12: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
8 | "foo/bar/".split(os.sep) | ||
9 | "foo/bar/".split(sep=os.sep) | ||
10 | "foo/bar/".split(os.sep)[-1] | ||
| ^^^^^ PTH206 | ||
11 | "foo/bar/".split(os.sep)[-2] | ||
12 | "foo/bar/".split(os.sep)[-2:] | ||
| | ||
|
||
PTH206.py:11:12: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
9 | "foo/bar/".split(sep=os.sep) | ||
10 | "foo/bar/".split(os.sep)[-1] | ||
11 | "foo/bar/".split(os.sep)[-2] | ||
| ^^^^^ PTH206 | ||
12 | "foo/bar/".split(os.sep)[-2:] | ||
13 | "fizz/buzz".split(sep) | ||
| | ||
|
||
PTH206.py:12:12: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
10 | "foo/bar/".split(os.sep)[-1] | ||
11 | "foo/bar/".split(os.sep)[-2] | ||
12 | "foo/bar/".split(os.sep)[-2:] | ||
| ^^^^^ PTH206 | ||
13 | "fizz/buzz".split(sep) | ||
14 | "fizz/buzz".split(sep)[-1] | ||
| | ||
|
||
PTH206.py:13:13: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
11 | "foo/bar/".split(os.sep)[-2] | ||
12 | "foo/bar/".split(os.sep)[-2:] | ||
13 | "fizz/buzz".split(sep) | ||
| ^^^^^ PTH206 | ||
14 | "fizz/buzz".split(sep)[-1] | ||
15 | os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
| | ||
|
||
PTH206.py:14:13: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
12 | "foo/bar/".split(os.sep)[-2:] | ||
13 | "fizz/buzz".split(sep) | ||
14 | "fizz/buzz".split(sep)[-1] | ||
| ^^^^^ PTH206 | ||
15 | os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
16 | file_name.split(os.sep) | ||
| | ||
|
||
PTH206.py:15:47: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
13 | "fizz/buzz".split(sep) | ||
14 | "fizz/buzz".split(sep)[-1] | ||
15 | os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
| ^^^^^ PTH206 | ||
16 | file_name.split(os.sep) | ||
17 | (os.path.abspath(file_name)).split(os.sep) | ||
| | ||
|
||
PTH206.py:16:11: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
14 | "fizz/buzz".split(sep)[-1] | ||
15 | os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
16 | file_name.split(os.sep) | ||
| ^^^^^ PTH206 | ||
17 | (os.path.abspath(file_name)).split(os.sep) | ||
| | ||
|
||
PTH206.py:17:30: PTH206 Replace `.split(os.sep)` with `Path.parts` | ||
| | ||
15 | os.path.splitext("path/to/hello_world.py")[0].split(os.sep)[-1] | ||
16 | file_name.split(os.sep) | ||
17 | (os.path.abspath(file_name)).split(os.sep) | ||
| ^^^^^ PTH206 | ||
18 | | ||
19 | # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.