-
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
Format target: annotation = value?
expressions
#5661
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
95afea9
to
5689102
Compare
@@ -6,20 +6,20 @@ use ruff_text_size::TextSize; | |||
use rustpython_parser::ast::Ranged; | |||
|
|||
/// Adds parentheses and indents `content` if it doesn't fit on a line. | |||
pub(crate) fn optional_parentheses<'ast, T>(content: &T) -> OptionalParentheses<'_, 'ast> | |||
pub(crate) fn parenthesize_if_expands<'ast, T>(content: &T) -> ParenthesizeIfExpands<'_, 'ast> |
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 think this name is better. The rest of the code refers to optional parentheses that we sometimes omit because they are unnecessary. These are parentheses that we always add if the content expands.
@@ -106,30 +105,9 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr { | |||
// Add optional parentheses. Ignore if the item renders parentheses itself. | |||
Parentheses::Optional => { | |||
if can_omit_optional_parentheses(item, f.context()) { | |||
let saved_level = f.context().node_level(); |
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 moved this, unchanged, to parentheses.rs
return_annotation | ||
.format() | ||
.with_options(Parenthesize::IfBreaks) | ||
optional_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.
Function defs are weird. They add parentheses around subscript expressions.
let options = PyFormatOptions::default(); | ||
let options_path = input_path.with_extension("options.json"); | ||
|
||
let options: PyFormatOptions = if let Ok(options_file) = fs::File::open(options_path) { |
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.
This adds support for options for black tests.
|
||
-class F(A, C): ... | ||
|
||
-def spam() -> None: ... | ||
+class F(A, C): | ||
+ ... | ||
+ | ||
+ | ||
+def spam() -> None: |
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.
Hmm, we'll need into single line bodies at some point. Potentially something that our suite refactor would simplify but we shouldn't be blocked by it.l
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.
🥳
5689102
to
018b5d2
Compare
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
32813bd
to
9cc7c21
Compare
018b5d2
to
a9c9dd5
Compare
9cc7c21
to
f3444d0
Compare
a9c9dd5
to
3fbd025
Compare
3fbd025
to
30e7756
Compare
Summary
This PR implements formatting of
target: annotation (= value)?
expressions.It resulted in me going down a rabbit hole because I then noticed that tuples inside of subscript should not be parenthesized. So I fixed that too.
Test Plan
I added new tests. I fixed the
skip_magic_comma
test to run it with magic comma disabled (This test got me really confused because one test wanted to have trailing comma and this test wanted not to have trailing commas).