-
Notifications
You must be signed in to change notification settings - Fork 883
/
replaceable_by_pathlib.rs
163 lines (160 loc) · 6.9 KB
/
replaceable_by_pathlib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_python_ast::{Constant, Expr, ExprCall, ExprConstant};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::AsRule;
use crate::rules::flake8_use_pathlib::rules::{
Glob, OsPathGetatime, OsPathGetctime, OsPathGetmtime, OsPathGetsize,
};
use crate::rules::flake8_use_pathlib::violations::{
BuiltinOpen, OsChmod, OsGetcwd, OsMakedirs, OsMkdir, OsPathAbspath, OsPathBasename,
OsPathDirname, OsPathExists, OsPathExpanduser, OsPathIsabs, OsPathIsdir, OsPathIsfile,
OsPathIslink, OsPathJoin, OsPathSamefile, OsPathSplitext, OsReadlink, OsRemove, OsRename,
OsReplace, OsRmdir, OsStat, OsUnlink, PyPath,
};
use crate::settings::types::PythonVersion;
pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, call: &ExprCall) {
if let Some(diagnostic_kind) =
checker
.semantic()
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.as_slice() {
// PTH100
["os", "path", "abspath"] => Some(OsPathAbspath.into()),
// PTH101
["os", "chmod"] => Some(OsChmod.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 {
module: "path".to_string(),
}
.into(),
),
["os", "sep", "join"] => Some(
OsPathJoin {
module: "sep".to_string(),
}
.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()),
// PTH203
["os", "path", "getatime"] => Some(OsPathGetatime.into()),
// PTH204
["os", "path", "getmtime"] => Some(OsPathGetmtime.into()),
// PTH205
["os", "path", "getctime"] => Some(OsPathGetctime.into()),
// PTH123
["" | "builtin", "open"] => {
// `closefd` and `openener` are not supported by pathlib, so check if they are
// are set to non-default values.
// https://github.com/astral-sh/ruff/issues/7620
// Signature as of Python 3.11 (https://docs.python.org/3/library/functions.html#open):
// ```text
// 0 1 2 3 4 5
// open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
// 6 7
// closefd=True, opener=None)
// ^^^^ ^^^^
// ```
// For `pathlib` (https://docs.python.org/3/library/pathlib.html#pathlib.Path.open):
// ```text
// Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)
// ```
if call
.arguments
.find_argument("closefd", 6)
.is_some_and(|expr| {
!matches!(
expr,
Expr::Constant(ExprConstant {
value: Constant::Bool(true),
..
})
)
})
|| call
.arguments
.find_argument("opener", 7)
.is_some_and(|expr| {
!matches!(
expr,
Expr::Constant(ExprConstant {
value: Constant::None,
..
})
)
})
{
return None;
}
Some(BuiltinOpen.into())
}
// PTH124
["py", "path", "local"] => Some(PyPath.into()),
// PTH207
["glob", "glob"] => Some(
Glob {
function: "glob".to_string(),
}
.into(),
),
["glob", "iglob"] => Some(
Glob {
function: "iglob".to_string(),
}
.into(),
),
// PTH115
// Python 3.9+
["os", "readlink"] if checker.settings.target_version >= PythonVersion::Py39 => {
Some(OsReadlink.into())
}
_ => None,
})
{
let diagnostic = Diagnostic::new::<DiagnosticKind>(diagnostic_kind, call.func.range());
if checker.enabled(diagnostic.kind.rule()) {
checker.diagnostics.push(diagnostic);
}
}
}