Skip to content

Commit 57be3fc

Browse files
Treat typing.Annotated subscripts as type definitions (#10285)
## Summary I think this code has existed since the start of `typing.Annotated` support (#333), and was then overlooked over a series of refactors. Closes #10279.
1 parent 7a675cd commit 57be3fc

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Test: ensure that we treat strings in `typing.Annotation` as type definitions."""
2+
3+
from pathlib import Path
4+
from re import RegexFlag
5+
from typing import Annotated
6+
7+
p: Annotated["Path", int] = 1

crates/ruff_linter/src/checkers/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
13481348
{
13491349
let mut iter = elts.iter();
13501350
if let Some(expr) = iter.next() {
1351-
self.visit_expr(expr);
1351+
self.visit_type_definition(expr);
13521352
}
13531353
for expr in iter {
13541354
self.visit_non_type_definition(expr);

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

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mod tests {
5555
#[test_case(Rule::UnusedImport, Path::new("F401_20.py"))]
5656
#[test_case(Rule::UnusedImport, Path::new("F401_21.py"))]
5757
#[test_case(Rule::UnusedImport, Path::new("F401_22.py"))]
58+
#[test_case(Rule::UnusedImport, Path::new("F401_23.py"))]
5859
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"))]
5960
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.ipynb"))]
6061
#[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
3+
---
4+
F401_23.py:4:16: F401 [*] `re.RegexFlag` imported but unused
5+
|
6+
3 | from pathlib import Path
7+
4 | from re import RegexFlag
8+
| ^^^^^^^^^ F401
9+
5 | from typing import Annotated
10+
|
11+
= help: Remove unused import: `re.RegexFlag`
12+
13+
Safe fix
14+
1 1 | """Test: ensure that we treat strings in `typing.Annotation` as type definitions."""
15+
2 2 |
16+
3 3 | from pathlib import Path
17+
4 |-from re import RegexFlag
18+
5 4 | from typing import Annotated
19+
6 5 |
20+
7 6 | p: Annotated["Path", int] = 1

0 commit comments

Comments
 (0)