mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Update PTH122 documentation to include Path.stem
and Path.parent
(#6884)
Closes https://github.com/astral-sh/ruff/issues/6846.
This commit is contained in:
parent
0e79074c31
commit
e0a40783ca
5 changed files with 17 additions and 9 deletions
|
@ -240,7 +240,7 @@ full_name.py:30:1: PTH121 `os.path.samefile()` should be replaced by `Path.samef
|
|||
32 | with open(p) as fp:
|
||||
|
|
||||
|
||||
full_name.py:31:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`
|
||||
full_name.py:31:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`
|
||||
|
|
||||
29 | os.path.dirname(p)
|
||||
30 | os.path.samefile(p)
|
||||
|
|
|
@ -239,7 +239,7 @@ import_as.py:30:1: PTH121 `os.path.samefile()` should be replaced by `Path.samef
|
|||
31 | foo_p.splitext(p)
|
||||
|
|
||||
|
||||
import_as.py:31:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`
|
||||
import_as.py:31:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`
|
||||
|
|
||||
29 | foo_p.dirname(p)
|
||||
30 | foo_p.samefile(p)
|
||||
|
|
|
@ -240,7 +240,7 @@ import_from.py:32:1: PTH121 `os.path.samefile()` should be replaced by `Path.sam
|
|||
34 | with open(p) as fp:
|
||||
|
|
||||
|
||||
import_from.py:33:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`
|
||||
import_from.py:33:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`
|
||||
|
|
||||
31 | dirname(p)
|
||||
32 | samefile(p)
|
||||
|
|
|
@ -239,7 +239,7 @@ import_from_as.py:37:1: PTH121 `os.path.samefile()` should be replaced by `Path.
|
|||
38 | xsplitext(p)
|
||||
|
|
||||
|
||||
import_from_as.py:38:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`
|
||||
import_from_as.py:38:1: PTH122 `os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`
|
||||
|
|
||||
36 | xdirname(p)
|
||||
37 | xsamefile(p)
|
||||
|
|
|
@ -974,8 +974,14 @@ impl Violation for OsPathSamefile {
|
|||
/// ## 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.suffix` can improve readability over the `os`
|
||||
/// module's counterparts (e.g., `os.path.splitext()`).
|
||||
/// methods such as `Path.suffix` and `Path.stem` can improve readability over
|
||||
/// the `os` module's counterparts (e.g., `os.path.splitext()`).
|
||||
///
|
||||
/// `os.path.splitext()` specifically returns a tuple of the file root and
|
||||
/// extension (e.g., given `splitext('/foo/bar.py')`, `os.path.splitext()`
|
||||
/// returns `("foo/bar", ".py")`. These outputs can be reconstructed through a
|
||||
/// combination of `Path.suffix` (`".py"`), `Path.stem` (`"bar"`), and
|
||||
/// `Path.parent` (`"foo"`).
|
||||
///
|
||||
/// Note that `os` functions may be preferable if performance is a concern,
|
||||
/// e.g., in hot loops.
|
||||
|
@ -984,14 +990,16 @@ impl Violation for OsPathSamefile {
|
|||
/// ```python
|
||||
/// import os
|
||||
///
|
||||
/// os.path.splitext("f1.py")
|
||||
/// (root, ext) = os.path.splitext("foo/bar.py")
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// from pathlib import Path
|
||||
///
|
||||
/// Path("f1.py").suffix
|
||||
/// path = Path("foo/bar.py")
|
||||
/// root = path.parent / path.stem
|
||||
/// ext = path.suffix
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
|
@ -1008,7 +1016,7 @@ pub struct OsPathSplitext;
|
|||
impl Violation for OsPathSplitext {
|
||||
#[derive_message_formats]
|
||||
fn message(&self) -> String {
|
||||
format!("`os.path.splitext()` should be replaced by `Path.suffix`")
|
||||
format!("`os.path.splitext()` should be replaced by `Path.suffix`, `Path.stem`, and `Path.parent`")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue