Skip to content

Commit cfe1801

Browse files
committed
Rename rule
1 parent e231ad9 commit cfe1801

File tree

6 files changed

+49
-31
lines changed

6 files changed

+49
-31
lines changed

crates/ruff_linter/src/checkers/ast/analyze/expression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
199199
if checker.is_rule_enabled(Rule::AccessAnnotationsFromClassDict) {
200200
ruff::rules::access_annotations_from_class_dict_by_key(checker, subscript);
201201
}
202-
if checker.is_rule_enabled(Rule::NestedAnnotatedType) {
203-
ruff::rules::nested_annotated_type(checker, subscript);
202+
if checker.is_rule_enabled(Rule::UnionWithAnnotatedType) {
203+
ruff::rules::union_with_annotated_type(checker, subscript);
204204
}
205205
pandas_vet::rules::subscript(checker, value, expr);
206206
}

crates/ruff_linter/src/codes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
10581058
(Ruff, "063") => rules::ruff::rules::AccessAnnotationsFromClassDict,
10591059
(Ruff, "064") => rules::ruff::rules::NonOctalPermissions,
10601060
(Ruff, "065") => rules::ruff::rules::LoggingEagerConversion,
1061-
(Ruff, "066") => rules::ruff::rules::NestedAnnotatedType,
1061+
(Ruff, "066") => rules::ruff::rules::UnionWithAnnotatedType,
10621062

10631063
(Ruff, "100") => rules::ruff::rules::UnusedNOQA,
10641064
(Ruff, "101") => rules::ruff::rules::RedirectedNOQA,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ mod tests {
113113
#[test_case(Rule::LegacyFormPytestRaises, Path::new("RUF061_deprecated_call.py"))]
114114
#[test_case(Rule::NonOctalPermissions, Path::new("RUF064.py"))]
115115
#[test_case(Rule::LoggingEagerConversion, Path::new("RUF065.py"))]
116-
#[test_case(Rule::NestedAnnotatedType, Path::new("RUF066.py"))]
116+
#[test_case(Rule::UnionWithAnnotatedType, Path::new("RUF066.py"))]
117117
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_0.py"))]
118118
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_1.py"))]
119119
#[test_case(Rule::InvalidRuleCode, Path::new("RUF102.py"))]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) use mutable_class_default::*;
3030
pub(crate) use mutable_dataclass_default::*;
3131
pub(crate) use mutable_fromkeys_value::*;
3232
pub(crate) use needless_else::*;
33-
pub(crate) use nested_annotated_type::*;
33+
pub(crate) use union_with_annotated_type::*;
3434
pub(crate) use never_union::*;
3535
pub(crate) use non_octal_permissions::*;
3636
pub(crate) use none_not_at_end_of_union::*;
@@ -95,7 +95,7 @@ mod mutable_class_default;
9595
mod mutable_dataclass_default;
9696
mod mutable_fromkeys_value;
9797
mod needless_else;
98-
mod nested_annotated_type;
98+
mod union_with_annotated_type;
9999
mod never_union;
100100
mod non_octal_permissions;
101101
mod none_not_at_end_of_union;

crates/ruff_linter/src/rules/ruff/rules/nested_annotated_type.rs renamed to crates/ruff_linter/src/rules/ruff/rules/union_with_annotated_type.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::Violation;
66
use crate::checkers::ast::Checker;
77

88
/// ## What it does
9-
/// Checks for `Annotated[]` types nested within other subscript types or union types.
9+
/// Checks for `Annotated[]` types within a `Union` or `Optional` type.
1010
///
1111
/// ## Why is this bad?
1212
/// Consumers of `Annotated` types often only check the top-level type for annotations,
@@ -27,28 +27,46 @@ use crate::checkers::ast::Checker;
2727
/// # {'a': typing.Annotated[str | None, 'test data']}
2828
/// ```
2929
///
30+
/// ## Example
31+
/// ```python
32+
/// from typing import Annotated, Optional
33+
/// from fastapi import FastAPI, Query
34+
///
35+
/// app = FastAPI()
36+
///
37+
/// @app.get('/route')
38+
/// def route(param: Annotated[str, Query()] | None = None):
39+
/// ...
40+
/// ```
41+
/// This fails to parse `param` as a query parameter. Use instead:
42+
/// ```python
43+
/// @app.get('/route')
44+
/// def route(param: Annotated[str | None, Query()] = None):
45+
/// ...
46+
/// ```
47+
///
3048
#[derive(ViolationMetadata)]
31-
#[violation_metadata(preview_since = "0.14.3")]
32-
pub(crate) struct NestedAnnotatedType {
49+
#[violation_metadata(preview_since = "0.14.4")]
50+
pub(crate) struct UnionWithAnnotatedType {
3351
parent_type: ParentType,
3452
}
3553

36-
impl Violation for NestedAnnotatedType {
54+
impl Violation for UnionWithAnnotatedType {
3755
#[derive_message_formats]
3856
fn message(&self) -> String {
3957
match self.parent_type {
4058
ParentType::Subscript => {
41-
"`Annotated[]` type must not be nested within another type".to_string()
59+
"`Annotated[]` type must not be part of a Union or Optional type".to_string()
4260
}
4361
ParentType::BinOp => {
44-
"`Annotated[]` type must not be nested within a PEP604 type union (|)".to_string()
62+
"`Annotated[]` type must not be part of a PEP604 type union (|)".to_string()
4563
}
4664
}
4765
}
4866
}
4967

5068
/// RUF066
51-
pub(crate) fn nested_annotated_type(checker: &Checker, subscript: &ExprSubscript) {
69+
pub(crate) fn union_with_annotated_type(checker: &Checker, subscript: &ExprSubscript) {
5270
let semantic = checker.semantic();
5371

5472
if !semantic.match_typing_expr(&subscript.value, "Annotated") {
@@ -66,7 +84,7 @@ pub(crate) fn nested_annotated_type(checker: &Checker, subscript: &ExprSubscript
6684
.last();
6785

6886
if let Some((parent, parent_type)) = result {
69-
checker.report_diagnostic(NestedAnnotatedType { parent_type }, parent.range());
87+
checker.report_diagnostic(UnionWithAnnotatedType { parent_type }, parent.range());
7088
}
7189
}
7290

crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF066_RUF066.py.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/ruff/mod.rs
33
---
4-
RUF066 `Annotated[]` type must not be nested within another type
4+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
55
--> RUF066.py:4:4
66
|
77
3 | # Errors
@@ -11,7 +11,7 @@ RUF066 `Annotated[]` type must not be nested within another type
1111
6 | c: Optional[Annotated[float, "A float"]] = 2.71
1212
|
1313

14-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
14+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
1515
--> RUF066.py:5:4
1616
|
1717
3 | # Errors
@@ -22,7 +22,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
2222
7 | d: Union[Annotated[int, "An integer"], Annotated[str, "A string"]]
2323
|
2424

25-
RUF066 `Annotated[]` type must not be nested within another type
25+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
2626
--> RUF066.py:6:4
2727
|
2828
4 | a: Union[Annotated[int, "An integer"], None] = 5
@@ -32,7 +32,7 @@ RUF066 `Annotated[]` type must not be nested within another type
3232
7 | d: Union[Annotated[int, "An integer"], Annotated[str, "A string"]]
3333
|
3434

35-
RUF066 `Annotated[]` type must not be nested within another type
35+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
3636
--> RUF066.py:7:4
3737
|
3838
5 | b: Annotated[Union[int, str], "An integer or string"] | None = "World"
@@ -43,7 +43,7 @@ RUF066 `Annotated[]` type must not be nested within another type
4343
9 | def f1(x: Union[Annotated[int, "An integer"], None]) -> None:
4444
|
4545

46-
RUF066 `Annotated[]` type must not be nested within another type
46+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
4747
--> RUF066.py:7:4
4848
|
4949
5 | b: Annotated[Union[int, str], "An integer or string"] | None = "World"
@@ -54,7 +54,7 @@ RUF066 `Annotated[]` type must not be nested within another type
5454
9 | def f1(x: Union[Annotated[int, "An integer"], None]) -> None:
5555
|
5656

57-
RUF066 `Annotated[]` type must not be nested within another type
57+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
5858
--> RUF066.py:9:11
5959
|
6060
7 | d: Union[Annotated[int, "An integer"], Annotated[str, "A string"]]
@@ -64,7 +64,7 @@ RUF066 `Annotated[]` type must not be nested within another type
6464
10 | pass
6565
|
6666

67-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
67+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
6868
--> RUF066.py:12:11
6969
|
7070
10 | pass
@@ -74,7 +74,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
7474
13 | pass
7575
|
7676

77-
RUF066 `Annotated[]` type must not be nested within another type
77+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
7878
--> RUF066.py:15:11
7979
|
8080
13 | pass
@@ -84,7 +84,7 @@ RUF066 `Annotated[]` type must not be nested within another type
8484
16 | pass
8585
|
8686

87-
RUF066 `Annotated[]` type must not be nested within another type
87+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
8888
--> RUF066.py:18:4
8989
|
9090
16 | pass
@@ -95,7 +95,7 @@ RUF066 `Annotated[]` type must not be nested within another type
9595
20 | l: TypeAlias = Annotated[int, "An integer"] | None
9696
|
9797

98-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
98+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
9999
--> RUF066.py:20:16
100100
|
101101
18 | e: Annotated[Annotated[int, "An integer"], "Another annotation"]
@@ -106,7 +106,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
106106
22 | n: TypeAlias = Optional[Annotated[float, "A float"]]
107107
|
108108

109-
RUF066 `Annotated[]` type must not be nested within another type
109+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
110110
--> RUF066.py:21:16
111111
|
112112
20 | l: TypeAlias = Annotated[int, "An integer"] | None
@@ -116,7 +116,7 @@ RUF066 `Annotated[]` type must not be nested within another type
116116
23 | o: TypeAlias = "Annotated[str, 'A string'] | None"
117117
|
118118

119-
RUF066 `Annotated[]` type must not be nested within another type
119+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
120120
--> RUF066.py:22:16
121121
|
122122
20 | l: TypeAlias = Annotated[int, "An integer"] | None
@@ -126,7 +126,7 @@ RUF066 `Annotated[]` type must not be nested within another type
126126
23 | o: TypeAlias = "Annotated[str, 'A string'] | None"
127127
|
128128

129-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
129+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
130130
--> RUF066.py:23:17
131131
|
132132
21 | m: TypeAlias = Union[Annotated[int, "An integer"], str]
@@ -137,7 +137,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
137137
25 | p: None | Annotated[int, "An integer"]
138138
|
139139

140-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
140+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
141141
--> RUF066.py:25:4
142142
|
143143
23 | o: TypeAlias = "Annotated[str, 'A string'] | None"
@@ -148,7 +148,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
148148
27 | q = Annotated[int | str, "An integer or string"] | None
149149
|
150150

151-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
151+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
152152
--> RUF066.py:27:5
153153
|
154154
25 | p: None | Annotated[int, "An integer"]
@@ -158,7 +158,7 @@ RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
158158
28 | r = Optional[Annotated[float | None, "A float or None"]]
159159
|
160160

161-
RUF066 `Annotated[]` type must not be nested within another type
161+
RUF066 `Annotated[]` type must not be part of a Union or Optional type
162162
--> RUF066.py:28:5
163163
|
164164
27 | q = Annotated[int | str, "An integer or string"] | None
@@ -168,7 +168,7 @@ RUF066 `Annotated[]` type must not be nested within another type
168168
30 | @some_decorator()
169169
|
170170

171-
RUF066 `Annotated[]` type must not be nested within a PEP604 type union (|)
171+
RUF066 `Annotated[]` type must not be part of a PEP604 type union (|)
172172
--> RUF066.py:31:13
173173
|
174174
30 | @some_decorator()

0 commit comments

Comments
 (0)