Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pep593 annotations #333

Merged
merged 7 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions resources/test/fixtures/F821.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ def update_tomato():
f'B'
f'{B}'
)


from typing import Annotated


def arbitrary_callable() -> None:
...

class PEP593Test:
field: Annotated[int, "base64", arbitrary_callable()]
9 changes: 8 additions & 1 deletion src/check_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct Checker<'a> {
in_f_string: Option<Range>,
in_annotation: bool,
in_literal: bool,
in_pep593_annotated: bool,
seen_non_import: bool,
seen_docstring: bool,
futures_allowed: bool,
Expand Down Expand Up @@ -91,6 +92,7 @@ impl<'a> Checker<'a> {
in_f_string: None,
in_annotation: false,
in_literal: false,
in_pep593_annotated: false,
seen_non_import: false,
seen_docstring: false,
futures_allowed: true,
Expand Down Expand Up @@ -675,6 +677,7 @@ where
let prev_in_f_string = self.in_f_string;
let prev_in_literal = self.in_literal;
let prev_in_annotation = self.in_annotation;
let prev_in_pep593_annotated = self.in_pep593_annotated;

if self.in_annotation && self.annotations_future_enabled {
self.deferred_annotations.push((
Expand All @@ -692,6 +695,9 @@ where
if match_name_or_attr(value, "Literal") {
self.in_literal = true;
}
if match_name_or_attr(value, "Annotated") {
self.in_pep593_annotated = true;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As a side note: it's totally valid to do from typing import Literal as Foo, I think this is just checking the name of the variable right?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's right. These checks are a bit limited in that they don't follow reassignment for these identifiers.

}
ExprKind::Tuple { elts, ctx } | ExprKind::List { elts, ctx } => {
if matches!(ctx, ExprContext::Store) {
Expand Down Expand Up @@ -862,7 +868,7 @@ where
ExprKind::Constant {
value: Constant::Str(value),
..
} if self.in_annotation && !self.in_literal => {
} if self.in_annotation && !(self.in_literal || self.in_pep593_annotated) => {
self.deferred_string_annotations
.push((Range::from_located(expr), value));
}
Expand Down Expand Up @@ -1039,6 +1045,7 @@ where

self.in_annotation = prev_in_annotation;
self.in_literal = prev_in_literal;
self.in_pep593_annotated = prev_in_pep593_annotated;
self.in_f_string = prev_in_f_string;
}

Expand Down