You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR changes our multiline_string preview style implementation (added in #9243) to only hug strings in call arguments when there's no newline between the ( and the string's quotes.
This is to address the feedback raised in Black's repository (issue)
The stable formatting always indents strings:
dedent(
""" value """
)
The preview style as it is implemented today (not this PR) tries to hug multiline strings, and only falls back to indenting the string if the first line of the string exceeds the configured line width:
dedent(""" value""")
The benefit of the new style is that it reduces vertical spacing (The two strings in the examples aren't equivalent because of the whitespace before the closing quotes but the whitespace before the closing quotes with indent is only used to align the quotes.
The main concern with the new preview style is that it messes up the multiline string formatting for existing Black users because it can dealign the quotes and fixing the quote alignment isn't possible without knowing if the argument is whitespace sensitive or not.
For example, it turns...
dedent(
""" value """
)
into...
dedent(""" value """)
which looks worse.
However, the preview style improves formatting for new black users or new code written when you have:
deftest():
a=dedent(""" Some code that needs dedenting """)
because it no longer changes it to
deftest():
a=dedent(
""" Some code that needs dedenting """
)
This PR refiens the multiline_string preview style with a heuristic of when to hug the multiline string and when not.
The idea is to honor the author's decision by checking if the opening parentheses and the string both start on the same line. If so, hug the multline string, otherwise don't.
There are two advantages to this:
Existing Blacked code won't change because the formatter detects the newline between the opening parentheses and the quotes and continues to indent the multiline string
Existing Blacked or Ruffed code using preview style won't change because the formatter detects that there's no newline between the opening parentheses and the string start and, because of it, won't indent the multiline string.
This heuristic is the same as Prettier uses for single-argument, multiline string call expressions.
Downsides
The downside of this heuristic over always hugging single argument multiline strings is that existing calls to dedent need manual updating to match the desired formatting. I think that's fine because the new formatting also requires to manually updating the spacing before the closing """ to match indentation:
dedent(""" Multiline """)
Doesn't look good. You want
dedent(""" Multiline""")
which ruff can't do without changing the string's semantics.
Test Plan
Running Ruff before introducing the multline-string preview style and then this branch:
There are no upgrade changes, compared to the original multiline-string preview style PR that changed+2971 -5900 lines in 374 files in 31 projects.
ℹ️ ecosystem check detected format changes. (+7 -8 lines in 3 files in 3 projects; 1 project error; 39 projects unchanged)
)
if is_error(response):
- error = (- f'Failed retrieving tasks for incident ID {incident["id"]}.\n \+ error = f'Failed retrieving tasks for incident ID {incident["id"]}.\n \
Make sure that the API key configured in the Core REST API integration \
-is one with sufficient permissions to access that incident.\n'- + get_error(response)- )+is one with sufficient permissions to access that incident.\n' + get_error(response)
raise Exception(error)
return response[0]["Contents"]["response"]
if oracle != HistoricalPriceOracle.CRYPTOCOMPARE:
return # only for cryptocompare for now
- with (- contextlib.suppress(UnknownAsset)+ with contextlib.suppress(+ UnknownAsset
): # if suppress -> assets are not crypto or fiat, so we can't query cryptocompare # noqa: E501
self.cryptocompare.create_cache(
from_asset=from_asset,
else:
# one doesn't currently exist, great.
# so we have two options here... we could make a typechecking block from scratch or... we could cheat
- code = textwrap.dedent("""+ code = textwrap.dedent(+ """
if TYPE_CHECKING:
@_generated
def __init__(self):
...
- """)+ """+ )
if_block = cast(
"cst.If", cst.parse_statement(code, config=self.module.config_for_parsing)
)
)
print(
- textwrap.dedent("""+ textwrap.dedent(+ """
Rasa uses telemetry to report anonymous usage information. This information
- is essential to help improve Rasa Open Source for all users.""")+ is essential to help improve Rasa Open Source for all users."""+ )
)
if not is_enabled:
def print_telemetry_reporting_info() -> None:
"""Print telemetry information to std out."""
- message = textwrap.dedent(f"""+ message = textwrap.dedent(+ f"""
Rasa Open Source reports anonymous usage telemetry to help improve the product
for all its users.
If you'd like to opt-out, you can use `rasa telemetry disable`.
- To learn more, check out {DOCS_URL_TELEMETRY}.""").strip()+ To learn more, check out {DOCS_URL_TELEMETRY}."""+ ).strip()
table = SingleTable([[message]])
print(table.table)
tested_slot = "duration"
form_name = "booking_form"
# form required_slots does not include the tested_slot
- domain.write_text(f"""+ domain.write_text(+ f"""
version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
intents:
- state_length_of_time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
formatterRelated to the formatterpreviewRelated to preview mode features
2 participants
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR changes our
multiline_stringpreview style implementation (added in #9243) to only hug strings in call arguments when there's no newline between the(and the string's quotes.This is to address the feedback raised in Black's repository (issue)
The stable formatting always indents strings:
The preview style as it is implemented today (not this PR) tries to hug multiline strings, and only falls back to indenting the string if the first line of the string exceeds the configured line width:
The benefit of the new style is that it reduces vertical spacing (The two strings in the examples aren't equivalent because of the whitespace before the closing quotes but the whitespace before the closing quotes with
indentis only used to align the quotes.The main concern with the new preview style is that it messes up the multiline string formatting for existing Black users because it can dealign the quotes and fixing the quote alignment isn't possible without knowing if the argument is whitespace sensitive or not.
For example, it turns...
into...
which looks worse.
However, the preview style improves formatting for new black users or new code written when you have:
because it no longer changes it to
This PR refiens the
multiline_stringpreview style with a heuristic of when to hug the multiline string and when not.The idea is to honor the author's decision by checking if the opening parentheses and the string both start on the same line. If so, hug the multline string, otherwise don't.
There are two advantages to this:
This heuristic is the same as Prettier uses for single-argument, multiline string call expressions.
Downsides
The downside of this heuristic over always hugging single argument multiline strings is that existing calls to
dedentneed manual updating to match the desired formatting. I think that's fine because the new formatting also requires to manually updating the spacing before the closing"""to match indentation:Doesn't look good. You want
which ruff can't do without changing the string's semantics.
Test Plan
Running Ruff before introducing the
multline-stringpreview style and then this branch:There are no upgrade changes, compared to the original
multiline-stringpreview style PR that changed+2971 -5900 lines in 374 files in 31 projects.ℹ️ ecosystem check detected format changes. (+7 -8 lines in 3 files in 3 projects; 1 project error; 39 projects unchanged)
demisto/content (+2 -5 lines across 1 file)
Packs/IntegrationsAndIncidentsHealthCheck/Scripts/GetFailedTasks/GetFailedTasks.py~L107
) if is_error(response): - error = ( - f'Failed retrieving tasks for incident ID {incident["id"]}.\n \ + error = f'Failed retrieving tasks for incident ID {incident["id"]}.\n \ Make sure that the API key configured in the Core REST API integration \ -is one with sufficient permissions to access that incident.\n' - + get_error(response) - ) +is one with sufficient permissions to access that incident.\n' + get_error(response) raise Exception(error) return response[0]["Contents"]["response"]reflex-dev/reflex (+3 -1 lines across 1 file)
reflex/components/markdown/markdown.py~L253
} # Separate out inline code and code blocks. - components["code"] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{ + components[ + "code" + ] = f"""{{({{node, inline, className, {_CHILDREN._var_name}, {_PROPS._var_name}}}) => {{ const match = (className || '').match(/language-(?<lang>.*)/); const language = match ? match[1] : ''; if (language) {{rotki/rotki (+2 -2 lines across 1 file)
rotkehlchen/rotkehlchen.py~L1196
if oracle != HistoricalPriceOracle.CRYPTOCOMPARE: return # only for cryptocompare for now - with ( - contextlib.suppress(UnknownAsset) + with contextlib.suppress( + UnknownAsset ): # if suppress -> assets are not crypto or fiat, so we can't query cryptocompare # noqa: E501 self.cryptocompare.create_cache( from_asset=from_asset,openai/openai-cookbook (error)
Running ruff preview (main) and then this branch:
Results in no changes
ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)
openai/openai-cookbook (error)