Skip to content

support pep593 annotations#333

Merged
charliermarsh merged 7 commits intoastral-sh:mainfrom
adriangb:pep539-support
Oct 6, 2022
Merged

support pep593 annotations#333
charliermarsh merged 7 commits intoastral-sh:mainfrom
adriangb:pep539-support

Conversation

@adriangb
Copy link
Copy Markdown
Contributor

@adriangb adriangb commented Oct 6, 2022

Currently PEP593 annotations throw an error because they are being processed as regular type annotations where instead the first argument should be processed as a regular annotations and the rest should be processed as arbitrary code (and can have string constants, which is what causes the current error).

src/check_ast.rs Outdated
Comment on lines +695 to +700
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
Copy Markdown
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
Copy Markdown
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.

@charliermarsh
Copy link
Copy Markdown
Member

Thanks for this! Excited to review.

@charliermarsh charliermarsh merged commit df438ba into astral-sh:main Oct 6, 2022
@charliermarsh
Copy link
Copy Markdown
Member

Great PR! Thanks for putting it together! I made a couple minor tweaks but nothing major.

@adriangb adriangb deleted the pep539-support branch October 6, 2022 16:04
Comment on lines +119 to +135
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
}
Copy link
Copy Markdown
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 😴

charliermarsh added a commit that referenced this pull request Mar 8, 2024
## 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.
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
)

## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants