@@ -1229,7 +1229,7 @@ def normalize_prefix(leaf: Leaf, *, inside_brackets: bool) -> None:
12291229 leaf .prefix = ""
12301230
12311231
1232- def normalize_invisible_parens (
1232+ def normalize_invisible_parens ( # noqa: C901
12331233 node : Node , parens_after : Set [str ], * , mode : Mode , features : Collection [Feature ]
12341234) -> None :
12351235 """Make existing optional parentheses invisible or create new ones.
@@ -1260,6 +1260,17 @@ def normalize_invisible_parens(
12601260 child , parens_after = parens_after , mode = mode , features = features
12611261 )
12621262
1263+ # Fixes a bug where invisible parens are not properly wrapped around
1264+ # case blocks.
1265+ if (
1266+ isinstance (child , Node )
1267+ and child .type == syms .case_block
1268+ and Preview .long_case_block_line_splitting in mode
1269+ ):
1270+ normalize_invisible_parens (
1271+ child , parens_after = {"case" }, mode = mode , features = features
1272+ )
1273+
12631274 # Add parentheses around long tuple unpacking in assignments.
12641275 if (
12651276 index == 0
@@ -1305,6 +1316,17 @@ def normalize_invisible_parens(
13051316 # invisible parentheses to work more precisely.
13061317 continue
13071318
1319+ elif (
1320+ isinstance (child , Leaf )
1321+ and child .next_sibling is not None
1322+ and child .next_sibling .type == token .COLON
1323+ and child .value == "case"
1324+ and Preview .long_case_block_line_splitting in mode
1325+ ):
1326+ # A special patch for "case case:" scenario, the second occurrence
1327+ # of case will be not parsed as a Python keyword.
1328+ break
1329+
13081330 elif not (isinstance (child , Leaf ) and is_multiline_string (child )):
13091331 wrap_in_parentheses (node , child , visible = False )
13101332
0 commit comments