-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 call expressions (without call chaining) #5341
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from unittest.mock import MagicMock | ||
|
||
|
||
def f(*args, **kwargs): | ||
pass | ||
|
||
this_is_a_very_long_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd = 1 | ||
session = MagicMock() | ||
models = MagicMock() | ||
|
||
f() | ||
|
||
f(1) | ||
|
||
f(x=2) | ||
|
||
f(1, x=2) | ||
|
||
f( | ||
this_is_a_very_long_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd | ||
) | ||
f( | ||
this_is_a_very_long_keyword_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd=1 | ||
) | ||
|
||
f( | ||
1, | ||
mixed_very_long_arguments=1, | ||
) | ||
|
||
f( | ||
this_is_a_very_long_argument_asökdhflakjslaslhfdlaffahflahsöfdhasägporejfäalkdsjäfalisjäfdlkasjd, | ||
these_arguments_have_values_that_need_to_break_because_they_are_too_long1=(100000 - 100000000000), | ||
these_arguments_have_values_that_need_to_break_because_they_are_too_long2="akshfdlakjsdfad" + "asdfasdfa", | ||
these_arguments_have_values_that_need_to_break_because_they_are_too_long3=session, | ||
) | ||
|
||
f( | ||
# dangling comment | ||
) | ||
|
||
|
||
f( | ||
only=1, short=1, arguments=1 | ||
) | ||
|
||
f( | ||
hey_this_is_a_long_call, it_has_funny_attributes_that_breaks_into_three_lines=1 | ||
) | ||
|
||
f( | ||
hey_this_is_a_very_long_call=1, it_has_funny_attributes_asdf_asdf=1, too_long_for_the_line=1, really=True | ||
) | ||
|
||
# TODO(konstin): Call chains/fluent interface (https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#call-chains) | ||
result = ( | ||
session.query(models.Customer.id) | ||
.filter( | ||
models.Customer.account_id == 10000, | ||
models.Customer.email == "user@example.org", | ||
) | ||
.order_by(models.Customer.id.asc()) | ||
.all() | ||
) | ||
# TODO(konstin): Black has this special case for comment placement where everything stays in one line | ||
f( | ||
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" | ||
) | ||
|
||
f( | ||
session, | ||
b=1, | ||
** # oddly placed end-of-line comment | ||
dict() | ||
) | ||
f( | ||
session, | ||
b=1, | ||
** | ||
# oddly placed own line comment | ||
dict() | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,11 @@ impl FormatNodeRule<Keyword> for FormatKeyword { | |
arg, | ||
value, | ||
} = item; | ||
if let Some(argument) = arg { | ||
write!(f, [argument.format(), text("=")])?; | ||
if let Some(arg) = arg { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I prefer to move the logic that is common for all branches out of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i want to keep those two specifically since they are one entry in a list each and because each branch has its own comment handling |
||
write!(f, [arg.format(), text("="), value.format()]) | ||
} else { | ||
// Comments after the stars are reassigned as trailing value comments | ||
write!(f, [text("**"), value.format()]) | ||
} | ||
|
||
value.format().fmt(f) | ||
} | ||
} |
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.
Nice!