Skip to content

Commit 985a17d

Browse files
dylwil3ntBre
authored andcommitted
[pygrep_hooks] Stabilize usingAsyncMock methods in invalid-mock-access (PGH005) (#20272)
Introduced in #18547. Removed gating, updated tests. Not documented so documentation is the same.
1 parent 2bc72e0 commit 985a17d

File tree

5 files changed

+129
-258
lines changed

5 files changed

+129
-258
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,6 @@ pub(crate) const fn is_optional_as_none_in_union_enabled(settings: &LinterSettin
200200
settings.preview.is_enabled()
201201
}
202202

203-
// https://github.com/astral-sh/ruff/pull/18547
204-
pub(crate) const fn is_invalid_async_mock_access_check_enabled(settings: &LinterSettings) -> bool {
205-
settings.preview.is_enabled()
206-
}
207-
208203
// https://github.com/astral-sh/ruff/pull/18867
209204
pub(crate) const fn is_raise_exception_byte_string_enabled(settings: &LinterSettings) -> bool {
210205
settings.preview.is_enabled()

crates/ruff_linter/src/rules/pygrep_hooks/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod tests {
1010

1111
use crate::registry::Rule;
1212

13-
use crate::settings::types::PreviewMode;
1413
use crate::test::test_path;
1514
use crate::{assert_diagnostics, settings};
1615

@@ -30,22 +29,4 @@ mod tests {
3029
assert_diagnostics!(snapshot, diagnostics);
3130
Ok(())
3231
}
33-
34-
#[test_case(Rule::InvalidMockAccess, Path::new("PGH005_0.py"))]
35-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
36-
let snapshot = format!(
37-
"preview__{}_{}",
38-
rule_code.noqa_code(),
39-
path.to_string_lossy()
40-
);
41-
let diagnostics = test_path(
42-
Path::new("pygrep_hooks").join(path).as_path(),
43-
&settings::LinterSettings {
44-
preview: PreviewMode::Enabled,
45-
..settings::LinterSettings::for_rule(rule_code)
46-
},
47-
)?;
48-
assert_diagnostics!(snapshot, diagnostics);
49-
Ok(())
50-
}
5132
}

crates/ruff_linter/src/rules/pygrep_hooks/rules/invalid_mock_access.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use ruff_text_size::Ranged;
55

66
use crate::Violation;
77
use crate::checkers::ast::Checker;
8-
use crate::preview::is_invalid_async_mock_access_check_enabled;
98

109
#[derive(Debug, PartialEq, Eq)]
1110
enum Reason {
@@ -62,18 +61,16 @@ pub(crate) fn uncalled_mock_method(checker: &Checker, expr: &Expr) {
6261
| "assert_has_calls"
6362
| "assert_not_called"
6463
);
65-
let is_uncalled_async_mock_method =
66-
is_invalid_async_mock_access_check_enabled(checker.settings())
67-
&& matches!(
68-
attr.as_str(),
69-
"assert_awaited"
70-
| "assert_awaited_once"
71-
| "assert_awaited_with"
72-
| "assert_awaited_once_with"
73-
| "assert_any_await"
74-
| "assert_has_awaits"
75-
| "assert_not_awaited"
76-
);
64+
let is_uncalled_async_mock_method = matches!(
65+
attr.as_str(),
66+
"assert_awaited"
67+
| "assert_awaited_once"
68+
| "assert_awaited_with"
69+
| "assert_awaited_once_with"
70+
| "assert_any_await"
71+
| "assert_has_awaits"
72+
| "assert_not_awaited"
73+
);
7774
if is_uncalled_mock_method || is_uncalled_async_mock_method {
7875
checker.report_diagnostic(
7976
InvalidMockAccess {
@@ -104,18 +101,16 @@ pub(crate) fn non_existent_mock_method(checker: &Checker, test: &Expr) {
104101
| "has_calls"
105102
| "not_called"
106103
);
107-
let is_missing_async_mock_method =
108-
is_invalid_async_mock_access_check_enabled(checker.settings())
109-
&& matches!(
110-
attr.as_str(),
111-
"awaited"
112-
| "awaited_once"
113-
| "awaited_with"
114-
| "awaited_once_with"
115-
| "any_await"
116-
| "has_awaits"
117-
| "not_awaited"
118-
);
104+
let is_missing_async_mock_method = matches!(
105+
attr.as_str(),
106+
"awaited"
107+
| "awaited_once"
108+
| "awaited_with"
109+
| "awaited_once_with"
110+
| "any_await"
111+
| "has_awaits"
112+
| "not_awaited"
113+
);
119114
if is_missing_mock_method || is_missing_async_mock_method {
120115
checker.report_diagnostic(
121116
InvalidMockAccess {

crates/ruff_linter/src/rules/pygrep_hooks/snapshots/ruff_linter__rules__pygrep_hooks__tests__PGH005_PGH005_0.py.snap

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,112 @@ PGH005 Mock method should be called: `assert_called_once_with`
9898
13 |
9999
14 | # OK
100100
|
101+
102+
PGH005 Non-existent mock method: `not_awaited`
103+
--> PGH005_0.py:26:8
104+
|
105+
24 | # =================
106+
25 | # Errors
107+
26 | assert my_mock.not_awaited()
108+
| ^^^^^^^^^^^^^^^^^^^^^
109+
27 | assert my_mock.awaited_once_with()
110+
28 | assert my_mock.not_awaited
111+
|
112+
113+
PGH005 Non-existent mock method: `awaited_once_with`
114+
--> PGH005_0.py:27:8
115+
|
116+
25 | # Errors
117+
26 | assert my_mock.not_awaited()
118+
27 | assert my_mock.awaited_once_with()
119+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
120+
28 | assert my_mock.not_awaited
121+
29 | assert my_mock.awaited_once_with
122+
|
123+
124+
PGH005 Non-existent mock method: `not_awaited`
125+
--> PGH005_0.py:28:8
126+
|
127+
26 | assert my_mock.not_awaited()
128+
27 | assert my_mock.awaited_once_with()
129+
28 | assert my_mock.not_awaited
130+
| ^^^^^^^^^^^^^^^^^^^
131+
29 | assert my_mock.awaited_once_with
132+
30 | my_mock.assert_not_awaited
133+
|
134+
135+
PGH005 Non-existent mock method: `awaited_once_with`
136+
--> PGH005_0.py:29:8
137+
|
138+
27 | assert my_mock.awaited_once_with()
139+
28 | assert my_mock.not_awaited
140+
29 | assert my_mock.awaited_once_with
141+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
142+
30 | my_mock.assert_not_awaited
143+
31 | my_mock.assert_awaited
144+
|
145+
146+
PGH005 Mock method should be called: `assert_not_awaited`
147+
--> PGH005_0.py:30:1
148+
|
149+
28 | assert my_mock.not_awaited
150+
29 | assert my_mock.awaited_once_with
151+
30 | my_mock.assert_not_awaited
152+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
153+
31 | my_mock.assert_awaited
154+
32 | my_mock.assert_awaited_once_with
155+
|
156+
157+
PGH005 Mock method should be called: `assert_awaited`
158+
--> PGH005_0.py:31:1
159+
|
160+
29 | assert my_mock.awaited_once_with
161+
30 | my_mock.assert_not_awaited
162+
31 | my_mock.assert_awaited
163+
| ^^^^^^^^^^^^^^^^^^^^^^
164+
32 | my_mock.assert_awaited_once_with
165+
33 | my_mock.assert_awaited_once_with
166+
|
167+
168+
PGH005 Mock method should be called: `assert_awaited_once_with`
169+
--> PGH005_0.py:32:1
170+
|
171+
30 | my_mock.assert_not_awaited
172+
31 | my_mock.assert_awaited
173+
32 | my_mock.assert_awaited_once_with
174+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
175+
33 | my_mock.assert_awaited_once_with
176+
34 | MyMock.assert_awaited_once_with
177+
|
178+
179+
PGH005 Mock method should be called: `assert_awaited_once_with`
180+
--> PGH005_0.py:33:1
181+
|
182+
31 | my_mock.assert_awaited
183+
32 | my_mock.assert_awaited_once_with
184+
33 | my_mock.assert_awaited_once_with
185+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
186+
34 | MyMock.assert_awaited_once_with
187+
35 | assert my_mock.awaited
188+
|
189+
190+
PGH005 Mock method should be called: `assert_awaited_once_with`
191+
--> PGH005_0.py:34:1
192+
|
193+
32 | my_mock.assert_awaited_once_with
194+
33 | my_mock.assert_awaited_once_with
195+
34 | MyMock.assert_awaited_once_with
196+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
197+
35 | assert my_mock.awaited
198+
|
199+
200+
PGH005 Non-existent mock method: `awaited`
201+
--> PGH005_0.py:35:8
202+
|
203+
33 | my_mock.assert_awaited_once_with
204+
34 | MyMock.assert_awaited_once_with
205+
35 | assert my_mock.awaited
206+
| ^^^^^^^^^^^^^^^
207+
36 |
208+
37 | # OK
209+
|

0 commit comments

Comments
 (0)