From 73c2d5514ce604141abe176b1f3e5cd35ff51d56 Mon Sep 17 00:00:00 2001 From: "Yilei \"Dolee\" Yang" Date: Tue, 20 Dec 2022 14:59:38 -0800 Subject: [PATCH] Fix a crash in ESP where a standalone comment is placed before a dict's value (#3469) --- CHANGES.md | 2 ++ src/black/trans.py | 2 +- tests/data/preview/long_strings__regression.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c29933f..1e51f3e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,8 @@ and except clauses (#3423) - Fix a crash in preview advanced string processing where mixed implicitly concatenated regular and f-strings start with an empty span (#3463) +- Fix a crash in preview advanced string processing where a standalone comment is placed + before a dict's value (#3469) - Do not put the closing quotes in a docstring on a separate line, even if the line is too long (#3430) - Long values in dict literals are now wrapped in parentheses; correspondingly diff --git a/src/black/trans.py b/src/black/trans.py index a5cf495..0eb53e2 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -1866,7 +1866,7 @@ class StringParenWrapper(BaseStringSplitter, CustomSplitMapMixin): for i, leaf in enumerate(LL): # We MUST find a colon, it can either be dict's or lambda's colon... - if leaf.type == token.COLON: + if leaf.type == token.COLON and i < len(LL) - 1: idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1 # That colon MUST be followed by a string... diff --git a/tests/data/preview/long_strings__regression.py b/tests/data/preview/long_strings__regression.py index 5e8f012..ef9007f 100644 --- a/tests/data/preview/long_strings__regression.py +++ b/tests/data/preview/long_strings__regression.py @@ -543,6 +543,13 @@ xxxx( f'xxxxxxxxxxxxx xxxx xx xxxxxxxxxx. xxxxx: {x.xxx}', ) +# Regression test for https://github.com/psf/black/issues/3455. +a_dict = { + "/this/is/a/very/very/very/very/very/very/very/very/very/very/long/key/without/spaces": + # And there is a comment before the value + ("item1", "item2", "item3"), +} + # output @@ -1221,3 +1228,10 @@ xxxx( f"xxxxxxxxxxxxx xxxx xx xxxxxxxxxx. xxxxx: {x.xxx}" ), ) + +# Regression test for https://github.com/psf/black/issues/3455. +a_dict = { + "/this/is/a/very/very/very/very/very/very/very/very/very/very/long/key/without/spaces": + # And there is a comment before the value + ("item1", "item2", "item3"), +} -- 2.39.5