Skip to content

Commit 2e211c5

Browse files
Change default for PT001 and PT023 (#12838)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 9fd8aaa commit 2e211c5

15 files changed

+351
-390
lines changed

crates/ruff/tests/snapshots/show_settings__display_default_settings.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ linter.flake8_import_conventions.aliases = {
264264
}
265265
linter.flake8_import_conventions.banned_aliases = {}
266266
linter.flake8_import_conventions.banned_from = []
267-
linter.flake8_pytest_style.fixture_parentheses = true
267+
linter.flake8_pytest_style.fixture_parentheses = false
268268
linter.flake8_pytest_style.parametrize_names_type = tuple
269269
linter.flake8_pytest_style.parametrize_values_type = list
270270
linter.flake8_pytest_style.parametrize_values_row_type = tuple
@@ -278,7 +278,7 @@ linter.flake8_pytest_style.raises_require_match_for = [
278278
socket.error,
279279
]
280280
linter.flake8_pytest_style.raises_extend_require_match_for = []
281-
linter.flake8_pytest_style.mark_parentheses = true
281+
linter.flake8_pytest_style.mark_parentheses = false
282282
linter.flake8_quotes.inline_quotes = double
283283
linter.flake8_quotes.multiline_quotes = double
284284
linter.flake8_quotes.docstring_quotes = double

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ mod tests {
2828
Rule::PytestFixtureIncorrectParenthesesStyle,
2929
Path::new("PT001.py"),
3030
Settings {
31-
fixture_parentheses: false,
31+
fixture_parentheses: true,
3232
..Settings::default()
3333
},
34-
"PT001_no_parentheses"
34+
"PT001_parentheses"
3535
)]
3636
#[test_case(
3737
Rule::PytestFixturePositionalArgs,
@@ -252,10 +252,10 @@ mod tests {
252252
Rule::PytestIncorrectMarkParenthesesStyle,
253253
Path::new("PT023.py"),
254254
Settings {
255-
mark_parentheses: false,
255+
mark_parentheses: true,
256256
..Settings::default()
257257
},
258-
"PT023_no_parentheses"
258+
"PT023_parentheses"
259259
)]
260260
#[test_case(
261261
Rule::PytestUnnecessaryAsyncioMarkOnFixture,

crates/ruff_linter/src/rules/flake8_pytest_style/rules/fixture.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ use super::helpers::{
3232
/// optional.
3333
///
3434
/// Either removing those unnecessary parentheses _or_ requiring them for all
35-
/// fixtures is fine, but it's best to be consistent.
36-
///
37-
/// In [preview], this rule defaults to removing unnecessary parentheses, to match
38-
/// the behavior of official pytest projects.
35+
/// fixtures is fine, but it's best to be consistent. The rule defaults to
36+
/// removing unnecessary parentheses, to match the documentation of the
37+
/// official pytest projects.
3938
///
4039
/// ## Example
4140
///
@@ -62,8 +61,6 @@ use super::helpers::{
6261
///
6362
/// ## References
6463
/// - [`pytest` documentation: API Reference: Fixtures](https://docs.pytest.org/en/latest/reference/reference.html#fixtures-api)
65-
///
66-
/// [preview]: https://docs.astral.sh/ruff/preview/
6764
#[violation]
6865
pub struct PytestFixtureIncorrectParenthesesStyle {
6966
expected: Parentheses,
@@ -938,9 +935,7 @@ pub(crate) fn fixture(
938935
check_fixture_decorator(checker, name, decorator);
939936
}
940937

941-
if checker.enabled(Rule::PytestDeprecatedYieldFixture)
942-
&& checker.settings.flake8_pytest_style.fixture_parentheses
943-
{
938+
if checker.enabled(Rule::PytestDeprecatedYieldFixture) {
944939
check_fixture_decorator_name(checker, decorator);
945940
}
946941

crates/ruff_linter/src/rules/flake8_pytest_style/rules/marks.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use super::helpers::get_mark_decorators;
1414
/// without parentheses, depending on the [`lint.flake8-pytest-style.mark-parentheses`]
1515
/// setting.
1616
///
17-
/// In [preview], this rule defaults to removing unnecessary parentheses, to match
18-
/// the behavior of official pytest projects.
17+
/// The rule defaults to removing unnecessary parentheses,
18+
/// to match the documentation of the official pytest projects.
1919
///
2020
/// ## Why is this bad?
2121
/// If a `@pytest.mark.<marker>()` doesn't take any arguments, the parentheses are
@@ -49,8 +49,6 @@ use super::helpers::get_mark_decorators;
4949
///
5050
/// ## References
5151
/// - [`pytest` documentation: Marks](https://docs.pytest.org/en/latest/reference/reference.html#marks)
52-
///
53-
/// [preview]: https://docs.astral.sh/ruff/preview/
5452
#[violation]
5553
pub struct PytestIncorrectMarkParenthesesStyle {
5654
mark_name: String,

crates/ruff_linter/src/rules/flake8_pytest_style/settings.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fmt::Formatter;
66
use crate::display_settings;
77
use ruff_macros::CacheKey;
88

9-
use crate::settings::types::{IdentifierPattern, PreviewMode};
9+
use crate::settings::types::IdentifierPattern;
1010

1111
use super::types;
1212

@@ -38,27 +38,13 @@ pub struct Settings {
3838
impl Default for Settings {
3939
fn default() -> Self {
4040
Self {
41-
fixture_parentheses: true,
41+
fixture_parentheses: false,
4242
parametrize_names_type: types::ParametrizeNameType::default(),
4343
parametrize_values_type: types::ParametrizeValuesType::default(),
4444
parametrize_values_row_type: types::ParametrizeValuesRowType::default(),
4545
raises_require_match_for: default_broad_exceptions(),
4646
raises_extend_require_match_for: vec![],
47-
mark_parentheses: true,
48-
}
49-
}
50-
}
51-
52-
impl Settings {
53-
pub fn resolve_default(preview: PreviewMode) -> Self {
54-
if preview.is_enabled() {
55-
Self {
56-
fixture_parentheses: false,
57-
mark_parentheses: false,
58-
..Default::default()
59-
}
60-
} else {
61-
Self::default()
47+
mark_parentheses: false,
6248
}
6349
}
6450
}
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,127 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
33
---
4-
PT001.py:9:1: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture`
4+
PT001.py:14:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
55
|
6-
9 | @pytest.fixture
7-
| ^^^^^^^^^^^^^^^ PT001
8-
10 | def no_parentheses():
9-
11 | return 42
6+
14 | @pytest.fixture()
7+
| ^^^^^^^^^^^^^^^^^ PT001
8+
15 | def parentheses_no_params():
9+
16 | return 42
1010
|
11-
= help: Add parentheses
11+
= help: Remove parentheses
1212

1313
Safe fix
14-
6 6 | # `import pytest`
15-
7 7 |
16-
8 8 |
17-
9 |-@pytest.fixture
18-
9 |+@pytest.fixture()
19-
10 10 | def no_parentheses():
2014
11 11 | return 42
2115
12 12 |
16+
13 13 |
17+
14 |-@pytest.fixture()
18+
14 |+@pytest.fixture
19+
15 15 | def parentheses_no_params():
20+
16 16 | return 42
21+
17 17 |
2222

23-
PT001.py:34:1: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture`
23+
PT001.py:24:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
2424
|
25-
34 | @fixture
26-
| ^^^^^^^^ PT001
27-
35 | def imported_from_no_parentheses():
28-
36 | return 42
25+
24 | / @pytest.fixture(
26+
25 | |
27+
26 | | )
28+
| |_^ PT001
29+
27 | def parentheses_no_params_multiline():
30+
28 | return 42
2931
|
30-
= help: Add parentheses
32+
= help: Remove parentheses
33+
34+
Safe fix
35+
21 21 | return 42
36+
22 22 |
37+
23 23 |
38+
24 |-@pytest.fixture(
39+
25 |-
40+
26 |-)
41+
24 |+@pytest.fixture
42+
27 25 | def parentheses_no_params_multiline():
43+
28 26 | return 42
44+
29 27 |
45+
46+
PT001.py:39:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
47+
|
48+
39 | @fixture()
49+
| ^^^^^^^^^^ PT001
50+
40 | def imported_from_parentheses_no_params():
51+
41 | return 42
52+
|
53+
= help: Remove parentheses
3154

3255
Safe fix
33-
31 31 | # `from pytest import fixture`
34-
32 32 |
35-
33 33 |
36-
34 |-@fixture
37-
34 |+@fixture()
38-
35 35 | def imported_from_no_parentheses():
3956
36 36 | return 42
4057
37 37 |
58+
38 38 |
59+
39 |-@fixture()
60+
39 |+@fixture
61+
40 40 | def imported_from_parentheses_no_params():
62+
41 41 | return 42
63+
42 42 |
4164

42-
PT001.py:59:1: PT001 [*] Use `@pytest.fixture()` over `@pytest.fixture`
65+
PT001.py:49:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
4366
|
44-
59 | @aliased
45-
| ^^^^^^^^ PT001
46-
60 | def aliased_no_parentheses():
47-
61 | return 42
67+
49 | / @fixture(
68+
50 | |
69+
51 | | )
70+
| |_^ PT001
71+
52 | def imported_from_parentheses_no_params_multiline():
72+
53 | return 42
4873
|
49-
= help: Add parentheses
74+
= help: Remove parentheses
75+
76+
Safe fix
77+
46 46 | return 42
78+
47 47 |
79+
48 48 |
80+
49 |-@fixture(
81+
50 |-
82+
51 |-)
83+
49 |+@fixture
84+
52 50 | def imported_from_parentheses_no_params_multiline():
85+
53 51 | return 42
86+
54 52 |
87+
88+
PT001.py:64:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
89+
|
90+
64 | @aliased()
91+
| ^^^^^^^^^^ PT001
92+
65 | def aliased_parentheses_no_params():
93+
66 | return 42
94+
|
95+
= help: Remove parentheses
5096

5197
Safe fix
52-
56 56 | # `from pytest import fixture as aliased`
53-
57 57 |
54-
58 58 |
55-
59 |-@aliased
56-
59 |+@aliased()
57-
60 60 | def aliased_no_parentheses():
5898
61 61 | return 42
5999
62 62 |
100+
63 63 |
101+
64 |-@aliased()
102+
64 |+@aliased
103+
65 65 | def aliased_parentheses_no_params():
104+
66 66 | return 42
105+
67 67 |
60106

107+
PT001.py:74:1: PT001 [*] Use `@pytest.fixture` over `@pytest.fixture()`
108+
|
109+
74 | / @aliased(
110+
75 | |
111+
76 | | )
112+
| |_^ PT001
113+
77 | def aliased_parentheses_no_params_multiline():
114+
78 | return 42
115+
|
116+
= help: Remove parentheses
61117

118+
Safe fix
119+
71 71 | return 42
120+
72 72 |
121+
73 73 |
122+
74 |-@aliased(
123+
75 |-
124+
76 |-)
125+
74 |+@aliased
126+
77 75 | def aliased_parentheses_no_params_multiline():
127+
78 76 | return 42

0 commit comments

Comments
 (0)