Skip to content

Commit

Permalink
Add documentation for the pathlib rules (#5815)
Browse files Browse the repository at this point in the history
Reviving #2348 step by step

Pt 1: docs

Tracking issue: #2646.
  • Loading branch information
sbrugman authored Jul 21, 2023
1 parent 5f2014b commit d62183b
Show file tree
Hide file tree
Showing 6 changed files with 873 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use ruff_macros::{derive_message_formats, violation};
///
/// ## Examples
/// ```python
/// import os
///
/// os.path.getsize(__file__)
/// ```
///
/// Use instead:
/// ```python
/// from pathlib import Path
///
/// Path(__file__).stat().st_size
/// ```
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use ruff_macros::{derive_message_formats, violation};
///
/// ## Examples
/// ```python
/// import os
///
/// os.path.getsize(__file__)
/// ```
///
/// Use instead:
/// ```python
/// from pathlib import Path
///
/// Path(__file__).stat().st_size
/// ```
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use ruff_macros::{derive_message_formats, violation};
///
/// ## Examples
/// ```python
/// import os
///
/// os.path.getsize(__file__)
/// ```
///
/// Use instead:
/// ```python
/// from pathlib import Path
///
/// Path(__file__).stat().st_size
/// ```
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ use ruff_macros::{derive_message_formats, violation};
///
/// ## Examples
/// ```python
/// import os
///
/// os.path.getsize(__file__)
/// ```
///
/// Use instead:
/// ```python
/// from pathlib import Path
///
/// Path(__file__).stat().st_size
/// ```
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,50 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
.semantic()
.resolve_call_path(expr)
.and_then(|call_path| match call_path.as_slice() {
// PTH100
["os", "path", "abspath"] => Some(OsPathAbspath.into()),
// PTH101
["os", "chmod"] => Some(OsChmod.into()),
["os", "mkdir"] => Some(OsMkdir.into()),
// PTH102
["os", "makedirs"] => Some(OsMakedirs.into()),
// PTH103
["os", "mkdir"] => Some(OsMkdir.into()),
// PTH104
["os", "rename"] => Some(OsRename.into()),
// PTH105
["os", "replace"] => Some(OsReplace.into()),
// PTH106
["os", "rmdir"] => Some(OsRmdir.into()),
// PTH107
["os", "remove"] => Some(OsRemove.into()),
// PTH108
["os", "unlink"] => Some(OsUnlink.into()),
// PTH109
["os", "getcwd"] => Some(OsGetcwd.into()),
["os", "getcwdb"] => Some(OsGetcwd.into()),
// PTH110
["os", "path", "exists"] => Some(OsPathExists.into()),
// PTH111
["os", "path", "expanduser"] => Some(OsPathExpanduser.into()),
// PTH112
["os", "path", "isdir"] => Some(OsPathIsdir.into()),
// PTH113
["os", "path", "isfile"] => Some(OsPathIsfile.into()),
// PTH114
["os", "path", "islink"] => Some(OsPathIslink.into()),
// PTH116
["os", "stat"] => Some(OsStat.into()),
// PTH117
["os", "path", "isabs"] => Some(OsPathIsabs.into()),
// PTH118
["os", "path", "join"] => Some(OsPathJoin.into()),
// PTH119
["os", "path", "basename"] => Some(OsPathBasename.into()),
// PTH120
["os", "path", "dirname"] => Some(OsPathDirname.into()),
// PTH121
["os", "path", "samefile"] => Some(OsPathSamefile.into()),
// PTH122
["os", "path", "splitext"] => Some(OsPathSplitext.into()),
// PTH202
["os", "path", "getsize"] => Some(OsPathGetsize.into()),
Expand All @@ -52,8 +74,11 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
["os", "path", "getmtime"] => Some(OsPathGetmtime.into()),
// PTH205
["os", "path", "getctime"] => Some(OsPathGetctime.into()),
["", "open"] => Some(BuiltinOpen.into()),
// PTH123
["" | "builtin", "open"] => Some(BuiltinOpen.into()),
// PTH124
["py", "path", "local"] => Some(PyPath.into()),
// PTH115
// Python 3.9+
["os", "readlink"] if checker.settings.target_version >= PythonVersion::Py39 => {
Some(OsReadlink.into())
Expand Down
Loading

0 comments on commit d62183b

Please sign in to comment.