Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions resources/test/fixtures/F821.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,36 @@ def update_tomato():
f'B'
f'{B}'
)


from typing import Annotated, Literal # noqa: E402


def arbitrary_callable() -> None:
...


class PEP593Test:
field: Annotated[
int,
"base64",
arbitrary_callable(),
123,
(1, 2, 3),
]
field_with_stringified_type: Annotated[
"PEP593Test",
123,
]
field_with_undefined_stringified_type: Annotated[
"PEP593Test123",
123,
]
field_with_nested_subscript: Annotated[
dict[Literal["foo"], str],
123,
]
field_with_undefined_nested_subscript: Annotated[
dict["foo", "bar"], # these should fail as undefined
123,
]
71 changes: 58 additions & 13 deletions src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,33 @@ fn match_name_or_attr(expr: &Expr, target: &str) -> bool {
}
}

fn is_annotated_subscript(expr: &Expr) -> bool {
#[derive(Clone, Copy)]
pub enum SubscriptKind {
AnnotatedSubscript,
PEP593AnnotatedSubscript,
}

fn match_annotated_subscript(expr: &Expr) -> Option<SubscriptKind> {
match &expr.node {
ExprKind::Attribute { attr, .. } => typing::is_annotated_subscript(attr),
ExprKind::Name { id, .. } => typing::is_annotated_subscript(id),
_ => false,
ExprKind::Attribute { attr, .. } => {
if typing::is_annotated_subscript(attr) {
Some(SubscriptKind::AnnotatedSubscript)
} else if typing::is_pep593_annotated_subscript(attr) {
Some(SubscriptKind::PEP593AnnotatedSubscript)
} else {
None
}
}
ExprKind::Name { id, .. } => {
if typing::is_annotated_subscript(id) {
Some(SubscriptKind::AnnotatedSubscript)
} else if typing::is_pep593_annotated_subscript(id) {
Some(SubscriptKind::PEP593AnnotatedSubscript)
} else {
None
}
Comment on lines +118 to +134
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can definitely be refactored to avoid duplication, I got lazy 😴

}
_ => None,
}
}

Expand Down Expand Up @@ -862,9 +884,11 @@ where
ExprKind::Constant {
value: Constant::Str(value),
..
} if self.in_annotation && !self.in_literal => {
self.deferred_string_annotations
.push((Range::from_located(expr), value));
} => {
if self.in_annotation && !self.in_literal {
self.deferred_string_annotations
.push((Range::from_located(expr), value));
}
}
ExprKind::Lambda { args, .. } => {
// Visit the arguments, but avoid the body, which will be deferred.
Expand Down Expand Up @@ -1015,12 +1039,33 @@ where
}
}
ExprKind::Subscript { value, slice, ctx } => {
if is_annotated_subscript(value) {
self.visit_expr(value);
self.visit_annotation(slice);
self.visit_expr_context(ctx);
} else {
visitor::walk_expr(self, expr);
match match_annotated_subscript(value) {
Some(subscript) => match subscript {
SubscriptKind::AnnotatedSubscript => {
self.visit_expr(value);
self.visit_annotation(slice);
self.visit_expr_context(ctx);
}
SubscriptKind::PEP593AnnotatedSubscript => {
// the first argument is a type (including forward references)
// the rest of the arguments are arbitrary python objects
self.visit_expr(value);
match &slice.node {
ExprKind::Tuple { elts, ctx } => {
let first = elts.first().unwrap();
self.visit_expr(first);
self.in_annotation = false;
for nxt in elts.iter().skip(1) {
self.visit_expr(nxt);
}
self.in_annotation = true;
self.visit_expr_context(ctx);
}
_ => panic!(), // arguments can only be slices
};
}
},
None => visitor::walk_expr(self, expr),
}
}
_ => visitor::walk_expr(self, expr),
Expand Down
5 changes: 5 additions & 0 deletions src/python/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static ANNOTATED_SUBSCRIPTS: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
"AbstractAsyncContextManager",
"AbstractContextManager",
"AbstractSet",
// "Annotated",
"AsyncContextManager",
"AsyncGenerator",
"AsyncIterable",
Expand Down Expand Up @@ -87,3 +88,7 @@ static ANNOTATED_SUBSCRIPTS: Lazy<BTreeSet<&'static str>> = Lazy::new(|| {
pub fn is_annotated_subscript(name: &str) -> bool {
ANNOTATED_SUBSCRIPTS.contains(name)
}

pub fn is_pep593_annotated_subscript(name: &str) -> bool {
name == "Annotated"
}
27 changes: 27 additions & 0 deletions src/snapshots/ruff__linter__tests__f821.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,31 @@ expression: checks
row: 89
column: 9
fix: ~
- kind:
UndefinedName: PEP593Test123
location:
row: 114
column: 10
end_location:
row: 114
column: 24
fix: ~
- kind:
UndefinedName: foo
location:
row: 122
column: 15
end_location:
row: 122
column: 19
fix: ~
- kind:
UndefinedName: bar
location:
row: 122
column: 22
end_location:
row: 122
column: 26
fix: ~