-
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.
Discovered that the usage of `glob.glob` is [widespread](https://grep.app/search?current=7&q=glob.glob%28&filter%5Blang%5D%5B0%5D=Python) when working on the previous lints for `flake8-use-pathlib`.
- Loading branch information
Showing
9 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
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,11 @@ | ||
import os | ||
import glob | ||
from glob import glob as search | ||
|
||
|
||
extensions_dir = "./extensions" | ||
|
||
# PTH207 | ||
glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
list(glob.iglob(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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for the use of `glob` and `iglob`. | ||
/// | ||
/// ## 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.glob()` can | ||
/// improve readability over their low-level counterparts (e.g., | ||
/// `glob.glob()`). | ||
/// | ||
/// Note that `glob.glob` and `Path.glob` are not exact equivalents: | ||
/// | ||
/// | | `glob` | `Path.glob` | | ||
/// |-------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------| | ||
/// | Hidden files | Excludes hidden files by default. From Python 3.11 onwards, the `include_hidden` keyword can used to include hidden directories. | Includes hidden files by default. | | ||
/// | Iterator | `iglob` returns an iterator. Under the hood, `glob` simply converts the iterator to a list. | `Path.glob` returns an iterator. | | ||
/// | Working directory | `glob` takes a `root_dir` keyword to set the current working directory. | `Path.rglob` can be used to return the relative path. | | ||
/// | Globstar (`**`) | `glob` requires the `recursive` flag to be set to `True` for the `**` pattern to match any files and zero or more directories, subdirectories, and symbolic links. | The `**` pattern in `Path.glob` means "this directory and all subdirectories, recursively". In other words, it enables recursive globbing. | | ||
/// | ||
/// ## 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: `Path.rglob`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob) | ||
/// - [Python documentation: `glob.glob`](https://docs.python.org/3/library/glob.html#glob.glob) | ||
/// - [Python documentation: `glob.iglob`](https://docs.python.org/3/library/glob.html#glob.iglob) | ||
#[violation] | ||
pub struct Glob { | ||
pub function: String, | ||
} | ||
|
||
impl Violation for Glob { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let Glob { function } = self; | ||
format!("Replace `{function}` with `Path.glob` or `Path.rglob`") | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
...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,30 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_use_pathlib/mod.rs | ||
--- | ||
PTH207.py:9:1: PTH207 Replace `glob` with `Path.glob` or `Path.rglob` | ||
| | ||
8 | # PTH207 | ||
9 | glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
| ^^^^^^^^^ PTH207 | ||
10 | list(glob.iglob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp"))) | ||
11 | search("*.png") | ||
| | ||
|
||
PTH207.py:10:6: PTH207 Replace `iglob` with `Path.glob` or `Path.rglob` | ||
| | ||
8 | # PTH207 | ||
9 | glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
10 | list(glob.iglob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp"))) | ||
| ^^^^^^^^^^ PTH207 | ||
11 | search("*.png") | ||
| | ||
|
||
PTH207.py:11:1: PTH207 Replace `glob` with `Path.glob` or `Path.rglob` | ||
| | ||
9 | glob.glob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp")) | ||
10 | list(glob.iglob(os.path.join(extensions_dir, "ops", "autograd", "*.cpp"))) | ||
11 | search("*.png") | ||
| ^^^^^^ PTH207 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.