-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Enable feature string_processing
to be default
#2188
Comments
Ok, so we're running this option on most primer projects now. Left to fix:
Please let us know if your repo has any issues with |
What is this feature supposed to do exactly? I tried it and it transformed the following string, which seems unideal to me, since it breaks the max line length of 88 by a large margin. I'm using black version 21.5b0.
|
Wow, that's pretty unfortunate. It's meant to split strings that are too long, not to make new oversized strings. We should track this as a bug and fix it before turning the feature on by default. |
The SQLAlchemy bug alluded to above is #2271. |
Hi 👋 I've come across this issue while considering adding a linting rule to my codebase to disallow implicit string concatenation for reasons listed in PEP3126, namely that code can look confusing when string concatenation is next to a comma-delimited list (of arguments or list items). It appears that the new string processing is making that confusion worse. For example, here's before, 2 arguments clearly visible: flash(
'None of the email addresses or domains you entered are valid',
'error',
) and here's after, looks too close to a three-argument call if you're not paying very close attention to a missing comma. flash(
'None of the email addresses or domains you entered'
' are valid',
'error',
) I'd like to propose that in cases like these, black would wrap the split string in parenthesis. Here's a desired outcome that would be a lot clearer: flash(
(
'None of the email addresses or domains you entered'
' are valid'
),
'error',
) On top of that, I encountered what appears to be a bug. Here's a simple reproduction case: black, 21.12b0 (compiled: no) x = (
"xxx xxxxxxx xxx "
f'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx{a["key"]}&xxxxxxxxxxxxxxxxx'
)
Contents of error log:
Note that I'm running with |
How about indenting continued line, like this:
|
Aside from bugs, to me, the biggest issue with string processing is probably the lack of any special treatment of Other than that, it seems that people sometimes also don't like that Black doesn't respect user-made splits if it can merge them into a single line though that's currently the case with the non-preview style as well. I do agree that at least some of the shown examples look better when they're split across multiple lines despite fitting in one but I don't think it's universally the case and I would say that I generally quite like that Black tries to merge strings into one line when they fit. Personally, I want Black to notice these kinds of opportunities for joining lines when I don't, even if it means that I'll have to fight with Black on occasion to keep lines separate. |
Hello, I am trying the Example: def open_file_from_long_file_path(
file_path: str = (
"some/really/long/file/path/because/it/has/filehash/as/part/of/path/"
"460e96a3b663f41a8412527424278bc60eb208ec5be1f5943e5dff2fe428a2da/"
"file.txt"
),
) -> list[str]:
with open(file_path) as f:
return [line.rstrip() for line in f] Running def open_file_from_long_file_path(
file_path: str = "some/really/long/file/path/because/it/has/filehash/as/part/of/path/460e96a3b663f41a8412527424278bc60eb208ec5be1f5943e5dff2fe428a2da/file.txt",
) -> list[str]:
with open(file_path) as f:
return [line.rstrip() for line in f] FYI I am running black |
@ikding thanks for letting us know! I've submitted a separate issue for that above 👌 |
It won't split a log line if it enclosed in triple quotes:
Is this the desired behavior? I tested with version 22.12.0 |
Seems to be fixed in #3430 |
Follow the comment at #2188 (comment), the new behavior also removes the Expected (expect actual = "aaa"
expected = "bbb"
message = (
f"Very very very very very very very very very very very long message. Expected\n"
f" {expected!r}\n"
f"but got\n"
f" {actual!r}\n"
f" ^^^"
) I intentionally keep the
actual = "aaa"
expected = "bbb"
message = (
"Very very very very very very very very very very very long message. Expected\n"
f" {expected!r}\n"
"but got\n"
f" {actual!r}\n"
" ^^^"
)
|
Hi, I tried this changes on the Django repository and it's pretty destructive 🙃 as it adds parentheses to all multi-line strings, e.g. diff --git a/tests/template_tests/filter_tests/test_urlize.py b/tests/template_tests/filter_tests/test_urlize.py
index abc227ba6a..d5dbe5925f 100644
--- a/tests/template_tests/filter_tests/test_urlize.py
+++ b/tests/template_tests/filter_tests/test_urlize.py
@@ -24,10 +24,12 @@ class UrlizeTests(SimpleTestCase):
)
self.assertEqual(
output,
- '<a href="http://example.com/?x=&y=" rel="nofollow">'
- "http://example.com/?x=&y=</a> "
- '<a href="http://example.com?x=&y=%3C2%3E" rel="nofollow">'
- "http://example.com?x=&y=<2></a>",
+ (
+ '<a href="http://example.com/?x=&y=" rel="nofollow">'
+ "http://example.com/?x=&y=</a> "
+ '<a href="http://example.com?x=&y=%3C2%3E" rel="nofollow">'
+ "http://example.com?x=&y=<2></a>"
+ ),
)
@setup({"urlize02": "{{ a|urlize }} {{ b|urlize }}"})
@@ -41,10 +43,12 @@ class UrlizeTests(SimpleTestCase):
)
self.assertEqual(
output,
- '<a href="http://example.com/?x=&y=" rel="nofollow">'
- "http://example.com/?x=&y=</a> "
- '<a href="http://example.com?x=&y=" rel="nofollow">'
- "http://example.com?x=&y=</a>",
+ (
+ '<a href="http://example.com/?x=&y=" rel="nofollow">'
+ "http://example.com/?x=&y=</a> "
+ '<a href="http://example.com?x=&y=" rel="nofollow">'
+ "http://example.com?x=&y=</a>"
+ ),
)
|
@felixxm For background, wrapping multi-line strings in parenthesis was first introduced in #3162 for list/set/tuple literals, then extended to all including function calls in #3292 The list/set/tuple literal cases are important for increased readability / bug preventions. The reason this was also extended to function calls is that this makes the behavior more consistent and easy to explain. For function calls, it's a much weaker point for bug preventions since accidentally forgetting/adding a comma in the middle of implicitly concatenated strings would often lead to a runtime error (or type checking error) since it usually makes it mismatch the function signature. One current workaround for not using parenthesis is to use explicit string concatenations, as demonstrated in this playground. However, this does require a lot of adjustments to the existing code to adopt the upcoming new stable style. I do recognize this is quite a disruptive change especially because they are quite common for |
- Switched from yapf to black - Reconfigure isort for black - Resolve black/pylint idiosyncrasies Note: I used `--experimental-string-processing` because black was producing "implicit string concatenation", similar to what described here: psf/black#1837. While currently this feature is experimental, it will be enabled by default: psf/black#2188. After running black with the new string processing so that the generated code merges these `"hello" " world"` strings concatenations, then I removed `--experimental-string-processing` for stability, and regenerated the code again. To the reviewer: don't even try to open "Files Changed" tab 😄 It's better to review commit-by-commit, and ignore `run black and isort`.
- Switched from yapf to black - Reconfigure isort for black - Resolve black/pylint idiosyncrasies Note: I used `--experimental-string-processing` because black was producing "implicit string concatenation", similar to what described here: psf/black#1837. While currently this feature is experimental, it will be enabled by default: psf/black#2188. After running black with the new string processing so that the generated code merges these `"hello" " world"` strings concatenations, then I removed `--experimental-string-processing` for stability, and regenerated the code again. To the reviewer: don't even try to open "Files Changed" tab 😄 It's better to review commit-by-commit, and ignore `run black and isort`.
Promotes compatibility with Black. See psf/black#2188 (comment)
- Switched from yapf to black - Reconfigure isort for black - Resolve black/pylint idiosyncrasies Note: I used `--experimental-string-processing` because black was producing "implicit string concatenation", similar to what described here: psf/black#1837. While currently this feature is experimental, it will be enabled by default: psf/black#2188. After running black with the new string processing so that the generated code merges these `"hello" " world"` strings concatenations, then I removed `--experimental-string-processing` for stability, and regenerated the code again. To the reviewer: don't even try to open "Files Changed" tab 😄 It's better to review commit-by-commit, and ignore `run black and isort`.
How do I use this feature? I have this file long.py with a single line:
When doing |
Just a minor point on this: the raise SystemExit((
"Thing already exists at the place."
"Did you forget to update the other "
"thing?"
)) ...but lints this: raise SystemExit(
"Thing already exists at the place."
"Did you forget to update the other "
"thing?"
) I have no opinion on which tool should do what, if anything, I'm just flagging that it might cause some friction with users if Black's policy is in competition with a popular linting tool. |
Do you not also need the |
We don't split strings that don't have whitespace in them. |
I might have been wrong about this, the table lists it as off for the standard level. It might be a misconfiguration in my project. Sorry for the noise. |
@cooperlees Could you update the issue title to "Enable feature |
--experimental-string-processing
to be defaultstring_processing
to be default
Lets move CI + then the default of black to use the
--experimental-string-processing
behavior.Today, if enabled on all
black-primer
projects, sqlalchemy fails AST check for parenthesis: https://pastebin.com/iaDH1Yg8Lets enable what we can CI wise and get this stable + default.
The text was updated successfully, but these errors were encountered: