Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01b8d3d

Browse files
authored
Do not add trailing commas to return type annotations using PEP 604 unions (psf#3735)
Fix psf#3638: Do not add trailing commas to return type annotations using PEP 604 unions.
1 parent 35722df commit 01b8d3d

3 files changed

Lines changed: 35 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
<!-- Changes that affect Black's stable style -->
1212

13+
- Fix a bug where an illegal trailing comma was added to return type annotations using
14+
PEP 604 unions (#3735)
15+
1316
### Preview style
1417

1518
<!-- Changes that affect Black's preview style -->

src/black/linegen.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,13 @@ def bracket_split_build_line(
918918
)
919919
if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf)
920920
)
921+
# Except the false negatives above for PEP 604 unions where we
922+
# can't add the comma.
923+
and not (
924+
leaves[0].parent
925+
and leaves[0].parent.next_sibling
926+
and leaves[0].parent.next_sibling.type == token.VBAR
927+
)
921928
)
922929

923930
if original.is_import or no_commas:

tests/data/simple_cases/pep_604.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def some_very_long_name_function() -> my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | None:
2+
pass
3+
4+
5+
def some_very_long_name_function() -> my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | my_module.EvenMoreType | None:
6+
pass
7+
8+
9+
# output
10+
11+
12+
def some_very_long_name_function() -> (
13+
my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | None
14+
):
15+
pass
16+
17+
18+
def some_very_long_name_function() -> (
19+
my_module.Asdf
20+
| my_module.AnotherType
21+
| my_module.YetAnotherType
22+
| my_module.EvenMoreType
23+
| None
24+
):
25+
pass

0 commit comments

Comments
 (0)