Skip to content

Commit b83c024

Browse files
authored
[flake8-pyi] Extend PYI033 to Python files in preview (#26129)
## Summary This PR revives #4166. There's no reason to use type comments in modern Python code. I'd argue they're effectively soft-deprecated at this point, and they're entirely unsupported by modern type checkers such as ty and pyrefly. In the three years since the last PR proposing this change, Python 2 has become much less widely used in the ecosystem, and many other flake8-pyi rules have been extended so that they now run on `.py` files as well. If you enable strict mode in pyright, it enables pyright's `reportTypeCommentUsage` rule, which bans the use of type comments. I don't want to have to reimplement that rule in ty for pyright parity, when we already have a very good implementation over here in Ruff ;) #1619 (comment) pointed out that there are still uses for type comments for things like `for` loops, e.g. ```py from models import Wheel for wheel in car.wheels.all(): # type: Wheel ... ``` But even here, while it's not quite as pretty, mypy users can easily use modern type annotations instead: ```py from models import Wheel wheel: Wheel for wheel in car.wheels.all(): ... ``` Closes #4460
1 parent e8a5e38 commit b83c024

7 files changed

Lines changed: 181 additions & 29 deletions

File tree

‎crates/ruff_linter/resources/test/fixtures/flake8_pyi/PYI033.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
from collections.abc import Sequence
44
from typing import TypeAlias
55

6-
A: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
7-
B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
8-
C: TypeAlias = None #type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
9-
D: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
10-
E: TypeAlias = None# type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
11-
F: TypeAlias = None#type:int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
6+
A: TypeAlias = None # type: int # error
7+
B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # error
8+
C: TypeAlias = None #type: int # error
9+
D: TypeAlias = None # type: int # error
10+
E: TypeAlias = None# type: int # error
11+
F: TypeAlias = None#type:int # error
1212

1313
def func(
14-
arg1, # type: dict[str, int] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
15-
arg2 # type: Sequence[bytes] # And here's some more info about this arg # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
14+
arg1, # type: dict[str, int] # error
15+
arg2 # type: Sequence[bytes] # And here's some more info about this arg # error
1616
): ...
1717

1818
class Foo:
19-
Attr: TypeAlias = None # type: set[str] # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
19+
Attr: TypeAlias = None # type: set[str] # error
2020

2121
G: TypeAlias = None # type: ignore
2222
H: TypeAlias = None # type: ignore[attr-defined]

‎crates/ruff_linter/src/checkers/tokens.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ pub(crate) fn check_tokens(
157157
pyupgrade::rules::extraneous_parentheses(context, tokens, locator);
158158
}
159159

160-
if source_type.is_stub() && context.is_rule_enabled(Rule::TypeCommentInStub) {
161-
flake8_pyi::rules::type_comment_in_stub(context, locator, comment_ranges);
160+
if context.is_rule_enabled(Rule::TypeCommentInStub) {
161+
flake8_pyi::rules::type_comment_in_stub(context, locator, comment_ranges, source_type);
162162
}
163163

164164
if context.any_rule_enabled(&[

‎crates/ruff_linter/src/preview.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ pub(crate) const fn is_bad_version_info_in_non_stub_enabled(settings: &LinterSet
5353
settings.preview.is_enabled()
5454
}
5555

56+
// https://github.com/astral-sh/ruff/issues/4460
57+
pub(crate) const fn is_type_comment_in_non_stub_enabled(settings: &LinterSettings) -> bool {
58+
settings.preview.is_enabled()
59+
}
60+
5661
/// <https://github.com/astral-sh/ruff/pull/19303>
5762
pub(crate) const fn is_fix_f_string_logging_enabled(settings: &LinterSettings) -> bool {
5863
settings.preview.is_enabled()

‎crates/ruff_linter/src/rules/flake8_pyi/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ mod tests {
146146
#[test_case(Rule::RedundantNumericUnion, Path::new("PYI041_2.py"))]
147147
#[test_case(Rule::RedundantNumericUnion, Path::new("PYI041_3.py"))]
148148
#[test_case(Rule::RedundantNumericUnion, Path::new("PYI041_4.py"))]
149+
#[test_case(Rule::TypeCommentInStub, Path::new("PYI033.py"))]
149150
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
150151
let snapshot = format!(
151152
"preview_{}_{}",

‎crates/ruff_linter/src/rules/flake8_pyi/rules/type_comment_in_stub.rs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,27 @@ use std::sync::LazyLock;
33
use regex::Regex;
44

55
use ruff_macros::{ViolationMetadata, derive_message_formats};
6+
use ruff_python_ast::PySourceType;
67
use ruff_python_trivia::CommentRanges;
78

89
use crate::Locator;
910
use crate::Violation;
1011
use crate::checkers::ast::LintContext;
12+
use crate::preview::is_type_comment_in_non_stub_enabled;
1113

1214
/// ## What it does
13-
/// Checks for the use of type comments (e.g., `x = 1 # type: int`) in stub
14-
/// files.
15+
/// Checks for the use of type comments (e.g., `x = 1 # type: int`).
16+
///
17+
/// By default, this check only runs on stub files. If [`preview`] mode is enabled,
18+
/// the check also runs on `.py` files.
1519
///
1620
/// ## Why is this bad?
17-
/// Stub (`.pyi`) files should use type annotations directly, rather
18-
/// than type comments, even if they're intended to support Python 2, since
19-
/// stub files are not executed at runtime. The one exception is `# type: ignore`.
21+
/// Type comments are a soft-deprecated form of type annotation. They are unsupported
22+
/// by some modern type checkers, including ty and Pyrefly, and are only necessary when
23+
/// supporting end-of-life Python versions such as 3.5 or older. They are never necessary
24+
/// in stub files, which are not executed at runtime.
25+
///
26+
/// This rule does not apply to `# type: ignore` suppression comments.
2027
///
2128
/// ## Example
2229
/// ```pyi
@@ -34,7 +41,7 @@ pub(crate) struct TypeCommentInStub;
3441
impl Violation for TypeCommentInStub {
3542
#[derive_message_formats]
3643
fn message(&self) -> String {
37-
"Don't use type comments in stub file".to_string()
44+
"Don't use type comments".to_string()
3845
}
3946
}
4047

@@ -43,12 +50,17 @@ pub(crate) fn type_comment_in_stub(
4350
context: &LintContext,
4451
locator: &Locator,
4552
comment_ranges: &CommentRanges,
53+
source_type: PySourceType,
4654
) {
55+
if !source_type.is_stub() && !is_type_comment_in_non_stub_enabled(context.settings()) {
56+
return;
57+
}
58+
4759
for range in comment_ranges {
4860
let comment = locator.slice(range);
4961

5062
if TYPE_COMMENT_REGEX.is_match(comment) && !TYPE_IGNORE_REGEX.is_match(comment) {
51-
context.report_diagnostic_if_enabled(TypeCommentInStub, range);
63+
context.report_diagnostic(TypeCommentInStub, range);
5264
}
5365
}
5466
}

‎crates/ruff_linter/src/rules/flake8_pyi/snapshots/ruff_linter__rules__flake8_pyi__tests__PYI033_PYI033.pyi.snap‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
33
---
4-
PYI033 Don't use type comments in stub file
4+
PYI033 Don't use type comments
55
--> PYI033.pyi:6:22
66
|
77
4 | from typing import TypeAlias
@@ -12,7 +12,7 @@ PYI033 Don't use type comments in stub file
1212
8 | C: TypeAlias = None #type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
1313
|
1414

15-
PYI033 Don't use type comments in stub file
15+
PYI033 Don't use type comments
1616
--> PYI033.pyi:7:22
1717
|
1818
6 | …one # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
@@ -22,7 +22,7 @@ PYI033 Don't use type comments in stub file
2222
9 | …one # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
2323
|
2424

25-
PYI033 Don't use type comments in stub file
25+
PYI033 Don't use type comments
2626
--> PYI033.pyi:8:22
2727
|
2828
6 | A: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
@@ -33,7 +33,7 @@ PYI033 Don't use type comments in stub file
3333
10 | E: TypeAlias = None# type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
3434
|
3535

36-
PYI033 Don't use type comments in stub file
36+
PYI033 Don't use type comments
3737
--> PYI033.pyi:9:22
3838
|
3939
7 | …as = None # type: str # And here's an extra comment about why it's that type # Y033 Do not use type comments in stubs (e.g. use "x…
@@ -44,7 +44,7 @@ PYI033 Don't use type comments in stub file
4444
11 | …as = None#type:int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
4545
|
4646

47-
PYI033 Don't use type comments in stub file
47+
PYI033 Don't use type comments
4848
--> PYI033.pyi:10:20
4949
|
5050
8 | C: TypeAlias = None #type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
@@ -54,7 +54,7 @@ PYI033 Don't use type comments in stub file
5454
11 | F: TypeAlias = None#type:int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
5555
|
5656

57-
PYI033 Don't use type comments in stub file
57+
PYI033 Don't use type comments
5858
--> PYI033.pyi:11:20
5959
|
6060
9 | D: TypeAlias = None # type: int # Y033 Do not use type comments in stubs (e.g. use "x: int" instead of "x = ... # type: int")
@@ -65,7 +65,7 @@ PYI033 Don't use type comments in stub file
6565
13 | def func(
6666
|
6767

68-
PYI033 Don't use type comments in stub file
68+
PYI033 Don't use type comments
6969
--> PYI033.pyi:14:12
7070
|
7171
13 | def func(
@@ -75,7 +75,7 @@ PYI033 Don't use type comments in stub file
7575
16 | ): ...
7676
|
7777

78-
PYI033 Don't use type comments in stub file
78+
PYI033 Don't use type comments
7979
--> PYI033.pyi:15:11
8080
|
8181
13 | …unc(
@@ -85,7 +85,7 @@ PYI033 Don't use type comments in stub file
8585
16 | ….
8686
|
8787

88-
PYI033 Don't use type comments in stub file
88+
PYI033 Don't use type comments
8989
--> PYI033.pyi:19:29
9090
|
9191
18 | …
@@ -95,7 +95,7 @@ PYI033 Don't use type comments in stub file
9595
21 | …ne # type: ignore
9696
|
9797

98-
PYI033 Don't use type comments in stub file
98+
PYI033 Don't use type comments
9999
--> PYI033.pyi:29:22
100100
|
101101
28 | # Whole line commented out # type: int
@@ -105,7 +105,7 @@ PYI033 Don't use type comments in stub file
105105
31 | class Bar:
106106
|
107107

108-
PYI033 Don't use type comments in stub file
108+
PYI033 Don't use type comments
109109
--> PYI033.pyi:32:26
110110
|
111111
31 | class Bar:
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
source: crates/ruff_linter/src/rules/flake8_pyi/mod.rs
3+
---
4+
--- Linter settings ---
5+
-linter.preview = disabled
6+
+linter.preview = enabled
7+
8+
--- Summary ---
9+
Removed: 0
10+
Added: 11
11+
12+
--- Added ---
13+
PYI033 Don't use type comments
14+
--> PYI033.py:6:22
15+
|
16+
4 | from typing import TypeAlias
17+
5 |
18+
6 | A: TypeAlias = None # type: int # error
19+
| ^^^^^^^^^^^^^^^^^^^^
20+
7 | B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # error
21+
8 | C: TypeAlias = None #type: int # error
22+
|
23+
24+
25+
PYI033 Don't use type comments
26+
--> PYI033.py:7:22
27+
|
28+
6 | A: TypeAlias = None # type: int # error
29+
7 | B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # error
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
8 | C: TypeAlias = None #type: int # error
32+
9 | D: TypeAlias = None # type: int # error
33+
|
34+
35+
36+
PYI033 Don't use type comments
37+
--> PYI033.py:8:22
38+
|
39+
6 | A: TypeAlias = None # type: int # error
40+
7 | B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # error
41+
8 | C: TypeAlias = None #type: int # error
42+
| ^^^^^^^^^^^^^^^^^^^
43+
9 | D: TypeAlias = None # type: int # error
44+
10 | E: TypeAlias = None# type: int # error
45+
|
46+
47+
48+
PYI033 Don't use type comments
49+
--> PYI033.py:9:22
50+
|
51+
7 | B: TypeAlias = None # type: str # And here's an extra comment about why it's that type # error
52+
8 | C: TypeAlias = None #type: int # error
53+
9 | D: TypeAlias = None # type: int # error
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
55+
10 | E: TypeAlias = None# type: int # error
56+
11 | F: TypeAlias = None#type:int # error
57+
|
58+
59+
60+
PYI033 Don't use type comments
61+
--> PYI033.py:10:20
62+
|
63+
8 | C: TypeAlias = None #type: int # error
64+
9 | D: TypeAlias = None # type: int # error
65+
10 | E: TypeAlias = None# type: int # error
66+
| ^^^^^^^^^^^^^^^^^^^^^^^
67+
11 | F: TypeAlias = None#type:int # error
68+
|
69+
70+
71+
PYI033 Don't use type comments
72+
--> PYI033.py:11:20
73+
|
74+
9 | D: TypeAlias = None # type: int # error
75+
10 | E: TypeAlias = None# type: int # error
76+
11 | F: TypeAlias = None#type:int # error
77+
| ^^^^^^^^^^^^^^^^^^
78+
12 |
79+
13 | def func(
80+
|
81+
82+
83+
PYI033 Don't use type comments
84+
--> PYI033.py:14:12
85+
|
86+
13 | def func(
87+
14 | arg1, # type: dict[str, int] # error
88+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
89+
15 | arg2 # type: Sequence[bytes] # And here's some more info about this arg # error
90+
16 | ): ...
91+
|
92+
93+
94+
PYI033 Don't use type comments
95+
--> PYI033.py:15:11
96+
|
97+
13 | def func(
98+
14 | arg1, # type: dict[str, int] # error
99+
15 | arg2 # type: Sequence[bytes] # And here's some more info about this arg # error
100+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101+
16 | ): ...
102+
|
103+
104+
105+
PYI033 Don't use type comments
106+
--> PYI033.py:19:29
107+
|
108+
18 | class Foo:
109+
19 | Attr: TypeAlias = None # type: set[str] # error
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
111+
20 |
112+
21 | G: TypeAlias = None # type: ignore
113+
|
114+
115+
116+
PYI033 Don't use type comments
117+
--> PYI033.py:29:22
118+
|
119+
28 | # Whole line commented out # type: int
120+
29 | M: TypeAlias = None # type: can't parse me!
121+
| ^^^^^^^^^^^^^^^^^^^^^^^
122+
30 |
123+
31 | class Bar:
124+
|
125+
126+
127+
PYI033 Don't use type comments
128+
--> PYI033.py:32:26
129+
|
130+
31 | class Bar:
131+
32 | N: TypeAlias = None # type: can't parse me either!
132+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
33 | # This whole line is commented out and indented # type: str
134+
|

0 commit comments

Comments
 (0)