-
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 os-path-getsize
and `os-path-get(a…
…|m|c)-time` (`PTH202-205`) (#5835) Reviving #2348 step by step Pt 3. implement detection for: - `os.path.getsize` - `os.path.getmtime` - `os.path.getctime` - `os.path.getatime`
- Loading branch information
Showing
19 changed files
with
498 additions
and
2 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH202.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,14 @@ | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getsize | ||
|
||
|
||
os.path.getsize("filename") | ||
os.path.getsize(b"filename") | ||
os.path.getsize(Path("filename")) | ||
os.path.getsize(__file__) | ||
|
||
getsize("filename") | ||
getsize(b"filename") | ||
getsize(Path("filename")) | ||
getsize(__file__) |
12 changes: 12 additions & 0 deletions
12
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH203.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 @@ | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getatime | ||
|
||
os.path.getatime("filename") | ||
os.path.getatime(b"filename") | ||
os.path.getatime(Path("filename")) | ||
|
||
|
||
getatime("filename") | ||
getatime(b"filename") | ||
getatime(Path("filename")) |
13 changes: 13 additions & 0 deletions
13
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH204.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,13 @@ | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getmtime | ||
|
||
|
||
os.path.getmtime("filename") | ||
os.path.getmtime(b"filename") | ||
os.path.getmtime(Path("filename")) | ||
|
||
|
||
getmtime("filename") | ||
getmtime(b"filename") | ||
getmtime(Path("filename")) |
12 changes: 12 additions & 0 deletions
12
crates/ruff/resources/test/fixtures/flake8_use_pathlib/PTH205.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 @@ | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getctime | ||
|
||
|
||
os.path.getctime("filename") | ||
os.path.getctime(b"filename") | ||
os.path.getctime(Path("filename")) | ||
|
||
getctime("filename") | ||
getctime(b"filename") | ||
getctime(Path("filename")) |
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 |
---|---|---|
@@ -1,5 +1,13 @@ | ||
pub(crate) use os_path_getatime::*; | ||
pub(crate) use os_path_getctime::*; | ||
pub(crate) use os_path_getmtime::*; | ||
pub(crate) use os_path_getsize::*; | ||
pub(crate) use path_constructor_current_directory::*; | ||
pub(crate) use replaceable_by_pathlib::*; | ||
|
||
mod os_path_getatime; | ||
mod os_path_getctime; | ||
mod os_path_getmtime; | ||
mod os_path_getsize; | ||
mod path_constructor_current_directory; | ||
mod replaceable_by_pathlib; |
43 changes: 43 additions & 0 deletions
43
crates/ruff/src/rules/flake8_use_pathlib/rules/os_path_getatime.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,43 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `os.path.getatime`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os`. | ||
/// | ||
/// When possible, using `Path` object methods such as `Path.stat()` can | ||
/// improve readability over the `os` module's counterparts (e.g., | ||
/// `os.path.getsize()`). | ||
/// | ||
/// Note that `os` functions may be preferable if performance is a concern, | ||
/// e.g., in hot loops. | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// os.path.getsize(__file__) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// Path(__file__).stat().st_size | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat) | ||
/// - [Python documentation: `os.path.getsize`](https://docs.python.org/3/library/os.path.html#os.path.getsize) | ||
/// - [PEP 428](https://peps.python.org/pep-0428/) | ||
/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) | ||
/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) | ||
/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) | ||
#[violation] | ||
pub struct OsPathGetatime; | ||
|
||
impl Violation for OsPathGetatime { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`os.path.getatime` should be replaced by `Path.stat().st_atime`") | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
crates/ruff/src/rules/flake8_use_pathlib/rules/os_path_getctime.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,43 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `os.path.getatime`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os`. | ||
/// | ||
/// When possible, using `Path` object methods such as `Path.stat()` can | ||
/// improve readability over the `os` module's counterparts (e.g., | ||
/// `os.path.getsize()`). | ||
/// | ||
/// Note that `os` functions may be preferable if performance is a concern, | ||
/// e.g., in hot loops. | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// os.path.getsize(__file__) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// Path(__file__).stat().st_size | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat) | ||
/// - [Python documentation: `os.path.getsize`](https://docs.python.org/3/library/os.path.html#os.path.getsize) | ||
/// - [PEP 428](https://peps.python.org/pep-0428/) | ||
/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) | ||
/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) | ||
/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) | ||
#[violation] | ||
pub struct OsPathGetctime; | ||
|
||
impl Violation for OsPathGetctime { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`os.path.getctime` should be replaced by `Path.stat().st_ctime`") | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
crates/ruff/src/rules/flake8_use_pathlib/rules/os_path_getmtime.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,43 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `os.path.getatime`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os`. | ||
/// | ||
/// When possible, using `Path` object methods such as `Path.stat()` can | ||
/// improve readability over the `os` module's counterparts (e.g., | ||
/// `os.path.getsize()`). | ||
/// | ||
/// Note that `os` functions may be preferable if performance is a concern, | ||
/// e.g., in hot loops. | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// os.path.getsize(__file__) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// Path(__file__).stat().st_size | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat) | ||
/// - [Python documentation: `os.path.getsize`](https://docs.python.org/3/library/os.path.html#os.path.getsize) | ||
/// - [PEP 428](https://peps.python.org/pep-0428/) | ||
/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) | ||
/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) | ||
/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) | ||
#[violation] | ||
pub struct OsPathGetmtime; | ||
|
||
impl Violation for OsPathGetmtime { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`os.path.getmtime` should be replaced by `Path.stat().st_mtime`") | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
crates/ruff/src/rules/flake8_use_pathlib/rules/os_path_getsize.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,43 @@ | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `os.path.getsize`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os`. | ||
/// | ||
/// When possible, using `Path` object methods such as `Path.stat()` can | ||
/// improve readability over the `os` module's counterparts (e.g., | ||
/// `os.path.getsize()`). | ||
/// | ||
/// Note that `os` functions may be preferable if performance is a concern, | ||
/// e.g., in hot loops. | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// os.path.getsize(__file__) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// Path(__file__).stat().st_size | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat) | ||
/// - [Python documentation: `os.path.getsize`](https://docs.python.org/3/library/os.path.html#os.path.getsize) | ||
/// - [PEP 428](https://peps.python.org/pep-0428/) | ||
/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) | ||
/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) | ||
/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) | ||
#[violation] | ||
pub struct OsPathGetsize; | ||
|
||
impl Violation for OsPathGetsize { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`os.path.getsize` should be replaced by `Path.stat().st_size`") | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
...lake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH202_PTH202.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,76 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_use_pathlib/mod.rs | ||
--- | ||
PTH202.py:6:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
6 | os.path.getsize("filename") | ||
| ^^^^^^^^^^^^^^^ PTH202 | ||
7 | os.path.getsize(b"filename") | ||
8 | os.path.getsize(Path("filename")) | ||
| | ||
|
||
PTH202.py:7:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
6 | os.path.getsize("filename") | ||
7 | os.path.getsize(b"filename") | ||
| ^^^^^^^^^^^^^^^ PTH202 | ||
8 | os.path.getsize(Path("filename")) | ||
9 | os.path.getsize(__file__) | ||
| | ||
|
||
PTH202.py:8:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
6 | os.path.getsize("filename") | ||
7 | os.path.getsize(b"filename") | ||
8 | os.path.getsize(Path("filename")) | ||
| ^^^^^^^^^^^^^^^ PTH202 | ||
9 | os.path.getsize(__file__) | ||
| | ||
|
||
PTH202.py:9:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
7 | os.path.getsize(b"filename") | ||
8 | os.path.getsize(Path("filename")) | ||
9 | os.path.getsize(__file__) | ||
| ^^^^^^^^^^^^^^^ PTH202 | ||
10 | | ||
11 | getsize("filename") | ||
| | ||
|
||
PTH202.py:11:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
9 | os.path.getsize(__file__) | ||
10 | | ||
11 | getsize("filename") | ||
| ^^^^^^^ PTH202 | ||
12 | getsize(b"filename") | ||
13 | getsize(Path("filename")) | ||
| | ||
|
||
PTH202.py:12:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
11 | getsize("filename") | ||
12 | getsize(b"filename") | ||
| ^^^^^^^ PTH202 | ||
13 | getsize(Path("filename")) | ||
14 | getsize(__file__) | ||
| | ||
|
||
PTH202.py:13:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
11 | getsize("filename") | ||
12 | getsize(b"filename") | ||
13 | getsize(Path("filename")) | ||
| ^^^^^^^ PTH202 | ||
14 | getsize(__file__) | ||
| | ||
|
||
PTH202.py:14:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | ||
| | ||
12 | getsize(b"filename") | ||
13 | getsize(Path("filename")) | ||
14 | getsize(__file__) | ||
| ^^^^^^^ PTH202 | ||
| | ||
|
||
|
Oops, something went wrong.