Skip to content

Commit

Permalink
Treat typing.Annotated subscripts as type definitions (astral-sh#10285
Browse files Browse the repository at this point in the history
)

## Summary

I think this code has existed since the start of `typing.Annotated`
support (astral-sh#333), and was then
overlooked over a series of refactors.

Closes astral-sh#10279.
  • Loading branch information
charliermarsh authored and nkxxll committed Mar 10, 2024
1 parent bc6aebf commit 8f6cb9d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Test: ensure that we treat strings in `typing.Annotation` as type definitions."""

from pathlib import Path
from re import RegexFlag
from typing import Annotated

p: Annotated["Path", int] = 1
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ impl<'a> Visitor<'a> for Checker<'a> {
{
let mut iter = elts.iter();
if let Some(expr) = iter.next() {
self.visit_expr(expr);
self.visit_type_definition(expr);
}
for expr in iter {
self.visit_non_type_definition(expr);
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod tests {
#[test_case(Rule::UnusedImport, Path::new("F401_20.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_21.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_22.py"))]
#[test_case(Rule::UnusedImport, Path::new("F401_23.py"))]
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.py"))]
#[test_case(Rule::ImportShadowedByLoopVar, Path::new("F402.ipynb"))]
#[test_case(Rule::UndefinedLocalWithImportStar, Path::new("F403.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: crates/ruff_linter/src/rules/pyflakes/mod.rs
---
F401_23.py:4:16: F401 [*] `re.RegexFlag` imported but unused
|
3 | from pathlib import Path
4 | from re import RegexFlag
| ^^^^^^^^^ F401
5 | from typing import Annotated
|
= help: Remove unused import: `re.RegexFlag`

Safe fix
1 1 | """Test: ensure that we treat strings in `typing.Annotation` as type definitions."""
2 2 |
3 3 | from pathlib import Path
4 |-from re import RegexFlag
5 4 | from typing import Annotated
6 5 |
7 6 | p: Annotated["Path", int] = 1

0 comments on commit 8f6cb9d

Please sign in to comment.