8000 Fix incorrect custom breakpoint indices when string group contains fa… · IBMZ-Linux-OSS-Python/black@99b68e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99b68e5

Browse files
authored
Fix incorrect custom breakpoint indices when string group contains fake f-strings (psf#2311)
Fixes psf#2293
1 parent 6380b9f commit 99b68e5

File tree

3 files changed

+102
-11
lines changed

3 files changed

+102
-11
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Fixed option usage when using the `--code` flag (#2259)
1010
- Do not call `uvloop.install()` when _Black_ is used as a library (#2303)
1111
- Added `--required-version` option to require a specific version to be running (#2300)
12+
- Fix incorrect custom breakpoint indices when string group contains fake f-strings
13+
(#2311)
1214

1315
## 21.5b2
1416

src/black/trans.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,21 +1151,32 @@ def more_splits_should_be_made() -> bool:
11511151

11521152
# --- Construct `next_value`
11531153
next_value = rest_value[:break_idx] + QUOTE
1154+
1155+
# HACK: The following 'if' statement is a hack to fix the custom
1156+
# breakpoint index in the case of either: (a) substrings that were
1157+
# f-strings but will have the 'f' prefix removed OR (b) substrings
1158+
# that were not f-strings but will now become f-strings because of
1159+
# redundant use of the 'f' prefix (i.e. none of the substrings
1160+
# contain f-expressions but one or more of them had the 'f' prefix
1161+
# anyway; in which case, we will prepend 'f' to _all_ substrings).
1162+
#
1163+
# There is probably a better way to accomplish what is being done
1164+
# here...
1165+
#
1166+
# If this substring is an f-string, we _could_ remove the 'f'
1167+
# prefix, and the current custom split did NOT originally use a
1168+
# prefix...
11541169
if (
1155-
# Are we allowed to try to drop a pointless 'f' prefix?
1156-
drop_pointless_f_prefix
1157-
# If we are, will we be successful?
1158-
and next_value != self._normalize_f_string(next_value, prefix)
1170+
next_value != self._normalize_f_string(next_value, prefix)
1171+
and use_custom_breakpoints
1172+
and not csplit.has_prefix
11591173
):
1160-
# If the current custom split did NOT originally use a prefix,
1161-
# then `csplit.break_idx` will be off by one after removing
1174+
# Then `csplit.break_idx` will be off by one after removing
11621175
# the 'f' prefix.
1163-
break_idx = (
1164-
break_idx + 1
1165-
if use_custom_breakpoints and not csplit.has_prefix
1166-
else break_idx
1167-
)
1176+
break_idx += 1
11681177
next_value = rest_value[:break_idx] + QUOTE
1178+
1179+
if drop_pointless_f_prefix:
11691180
next_value = self._normalize_f_string(next_value, prefix)
11701181

11711182
# --- Construct `next_leaf`

tests/data/long_strings__regression.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,45 @@ async def foo(self):
455455
)
456456
assert str(suffix_arr) in "['$', 'angaroo$', 'angrykangaroo$', 'aroo$', 'garoo$', 'grykangaroo$', 'kangaroo$', 'ngaroo$', 'ngrykangaroo$', 'o$', 'oo$', 'roo$', 'rykangaroo$', 'ykangaroo$']"
457457
assert str(suffix_arr) not in "['$', 'angaroo$', 'angrykangaroo$', 'aroo$', 'garoo$', 'grykangaroo$', 'kangaroo$', 'ngaroo$', 'ngrykangaroo$', 'o$', 'oo$', 'roo$', 'rykangaroo$', 'ykangaroo$']"
458+
message = (
459+
f"1. Go to Google Developers Console and log in with your Google account."
460+
"(https://console.developers.google.com/)"
461+
"2. You should be prompted to create a new project (name does not matter)."
462+
"3. Click on Enable APIs and Services at the top."
463+
"4. In the list of APIs choose or search for YouTube Data API v3 and "
464+
"click on it. Choose Enable."
465+
"5. Click on Credentials on the left navigation bar."
466+
"6. Click on Create Credential at the top."
467+
'7. At the top click the link for "API key".'
468+
"8. No application restrictions are needed. Click Create at the bottom."
469+
"9. You now have a key to add to `{prefix}set api youtube api_key`"
470+
)
471+
message = (
472+
f"1. Go to Google Developers Console and log in with your Google account."
473+
"(https://console.developers.google.com/)"
474+
"2. You should be prompted to create a new project (name does not matter)."
475+
f"3. Click on Enable APIs and Services at the top."
476+
"4. In the list of APIs choose or search for YouTube Data API v3 and "
477+
"click on it. Choose Enable."
478+
f"5. Click on Credentials on the left navigation bar."
479+
"6. Click on Create Credential at the top."
480+
'7. At the top click the link for "API key".'
481+
"8. No application restrictions are needed. Click Create at the bottom."
482+
"9. You now have a key to add to `{prefix}set api youtube api_key`"
483+
)
484+
message = (
485+
f"1. Go to Google Developers Console and log in with your Google account."
486+
"(https://console.developers.google.com/)"
487+
"2. You should be prompted to create a new project (name does not matter)."
488+
f"3. Click on Enable APIs and Services at the top."
489+
"4. In the list of APIs choose or search for YouTube Data API v3 and "
490+
"click on it. Choose Enable."
491+
f"5. Click on Credentials on the left navigation bar."
492+
"6. Click on Create Credential at the top."
493+
'7. At the top click the link for "API key".'
494+
"8. No application restrictions are needed. Click Create at the bottom."
495+
f"9. You now have a key to add to `{prefix}set api youtube api_key`"
496+
)
458497

459498
# output
460499

@@ -1022,3 +1061,42 @@ async def foo(self):
10221061
" 'kangaroo$', 'ngaroo$', 'ngrykangaroo$', 'o$', 'oo$', 'roo$',"
10231062
" 'rykangaroo$', 'ykangaroo$']"
10241063
)
1064+
message = (
1065+
f"1. Go to Google Developers Console and log in with your Google account."
1066+
f"(https://console.developers.google.com/)"
1067+
f"2. You should be prompted to create a new project (name does not matter)."
1068+
f"3. Click on Enable APIs and Services at the top."
1069+
f"4. In the list of APIs choose or search for YouTube Data API v3 and "
1070+
f"click on it. Choose Enable."
1071+
f"5. Click on Credentials on the left navigation bar."
1072+
f"6. Click on Create Credential at the top."
1073+
f'7. At the top click the link for "API key".'
1074+
f"8. No application restrictions are needed. Click Create at the bottom."
1075+
f"9. You now have a key to add to `{{prefix}}set api youtube api_key`"
1076+
)
1077+
message = (
1078+
f"1. Go to Google Developers Console and log in with your Google account."
1079+
f"(https://console.developers.google.com/)"
1080+
f"2. You should be prompted to create a new project (name does not matter)."
1081+
f"3. Click on Enable APIs and Services at the top."
1082+
f"4. In the list of APIs choose or search for YouTube Data API v3 and "
1083+
f"click on it. Choose Enable."
1084+
f"5. Click on Credentials on the left navigation bar."
1085+
f"6. Click on Create Credential at the top."
1086+
f'7. At the top click the link for "API key".'
1087+
f"8. No application restrictions are needed. Click Create at the bottom."
1088+
f"9. You now have a key to add to `{{prefix}}set api youtube api_key`"
1089+
)
1090+
message = (
1091+
"1. Go to Google Developers Console and log in with your Google account."
1092+
"(https://console.developers.google.com/)"
1093+
"2. You should be prompted to create a new project (name does not matter)."
1094+
"3. Click on Enable APIs and Services at the top."
1095+
"4. In the list of APIs choose or search for YouTube Data API v3 and "
1096+
"click on it. Choose Enable."
1097+
"5. Click on Credentials on the left navigation bar."
1098+
"6. Click on Create Credential at the top."
1099+
'7. At the top click the link for "API key".'
1100+
"8. No application restrictions are needed. Click Create at the bottom."
1101+
f"9. You now have a key to add to `{prefix}set api youtube api_key`"
1102+
)

0 commit comments

Comments
 (0)
0