-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Parse triple quoted string annotations as if parenthesized #15387
base: main
Are you sure you want to change the base?
Conversation
b33cf05
to
efbd25e
Compare
|
88460a5
to
be3115f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!! The red-knot test and changes look fine to me, but that's a small part of this PR. Would rather have @dhruvmanila take a look at the parser/ruff side.
crates/red_knot_python_semantic/resources/mdtest/annotations/string.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. Would you mind adding a test where an expression is incorrectly parenthesized?
def f(
a: """
int |
str)
"""
)
or
def f(
a: """
int) |
str
"""
)
I'm curious how it recovers in those cases and if we need to do something more than just setting nesting = 1
crates/ruff_python_parser/src/lib.rs
Outdated
@@ -599,6 +641,8 @@ pub enum Mode { | |||
/// [System shell access]: https://ipython.readthedocs.io/en/stable/interactive/reference.html#system-shell-access | |||
/// [Automatic parentheses and quotes]: https://ipython.readthedocs.io/en/stable/interactive/reference.html#automatic-parentheses-and-quotes | |||
Ipython, | |||
|
|||
ParenthesizedExpression, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd move it next to Expression
and let's add a comment explaining what this mode is.
93125a2
to
b3cf440
Compare
@MichaReiser I added the test cases you suggested and the tests pass(see this commit) So I fixed the nesting check to check for nesting value of 1 when mode is |
crates/ruff_python_parser/src/lib.rs
Outdated
parsed.try_into_expression().unwrap().into_result() | ||
} | ||
|
||
pub fn parse_as_string_annotation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about parse_string_annotation
or parse_string_annotation_content
? Additionally, we should provide documentation similar to what you've done in parse_parenthesized_expression_range
as this is a public function and the crate is being used by multiple projects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, renamed to parse_string_annotation
and added documentation.
# single quotes are not implicitly parenthesized | ||
invalid: "\n int" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the same test case using triple-quoted string which will be useful as a documentation?
# but, using the same annotation inside a triple-quoted string is valid
valid: """\n int"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I made a mistake here, invalid: "\n int"
is in a python file. So this string will not actually have a newline character but \
and n
as characters. So also """"\n int"""
fails.
Is there a way to actually create a type annotation that contains newline inside a python file without using triple quotes?
https://pyright-play.net/?code=JYOwbghgNsAmBcACARKgOiRoAurkCh9RIYEVUiRcKg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this right now. Since I could not figure out how to do it correctly.
invalid2: """ | ||
int | | ||
str) | ||
""" | ||
|
||
invalid3: """ | ||
int) | | ||
str | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some test cases for unclosed parenthesis similar to how these? It would also be useful to provide test cases with multiple parentheses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. This looks great.
Co-authored-by: Dhruv Manilawala <[email protected]>
Co-authored-by: Dhruv Manilawala <[email protected]>
Co-authored-by: Micha Reiser <[email protected]>
cf342b5
to
a74d41e
Compare
Summary
Resolves #9467
Parse quoted annotations as if the string content is inside parenthesis.
With this logic
x
andy
in this example are equal:Also this rule only applies to triple quotes(link).
This PR is based on the comments on the issue.
I did one extra change, since we don't want any indentation tokens I am setting the
State::Other
as the initial state of the Lexer.Remaining work:
Test Plan
Added a test which previously failed because quoted annotation contained indentation.
Added an mdtest for red-knot.
Updated previous test.