-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
] Implement glob
(PTH207
)
- Loading branch information
Showing
9 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH207.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 @@ | ||
import os | ||
import glob | ||
from glob import glob as search | ||
|
||
|
||
extensions_dir = "./extensions" | ||
|
||
# PTH207 | ||
glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
search("*.png") |
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
41 changes: 41 additions & 0 deletions
41
crates/ruff/src/rules/flake8_use_pathlib/rules/glob_rule.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,41 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for the use of `glob`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os` and `glob`. | ||
/// | ||
/// When possible, using `Path` object methods such as `Path.stat()` can | ||
/// improve readability over their low-level counterparts (e.g., | ||
/// `glob.glob()`). | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import glob | ||
/// import os | ||
/// | ||
/// glob.glob(os.path.join(path, "requirements*.txt")) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from pathlib import Path | ||
/// | ||
/// Path(path).glob("requirements*.txt") | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.glob`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob) | ||
/// - [Python documentation: `glob.glob`](https://docs.python.org/3/library/glob.html#glob.glob) | ||
/// ``` | ||
#[violation] | ||
pub struct Glob; | ||
|
||
impl Violation for Glob { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Replace `glob` with `Path.glob`") | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...lake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH207_PTH207.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,20 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_use_pathlib/mod.rs | ||
--- | ||
PTH207.py:9:1: PTH207 Replace `glob` with `Path.glob` | ||
| | ||
8 | # PTH207 | ||
9 | glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
| ^^^^^^^^^ PTH207 | ||
10 | search("*.png") | ||
| | ||
|
||
PTH207.py:10:1: PTH207 Replace `glob` with `Path.glob` | ||
| | ||
8 | # PTH207 | ||
9 | glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
10 | search("*.png") | ||
| ^^^^^^ PTH207 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.