Do not add trailing commas to return type annotations using PEP 604 unions (#3735)

Fix #3638: Do not add trailing commas to return type annotations using PEP 604 unions.
This commit is contained in:
Yilei "Dolee" Yang 2023-06-15 17:08:26 -07:00 committed by GitHub
parent 35722dff62
commit 01b8d3d409
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -10,6 +10,9 @@
<!-- Changes that affect Black's stable style -->
- Fix a bug where an illegal trailing comma was added to return type annotations using
PEP 604 unions (#3735)
### Preview style
<!-- Changes that affect Black's preview style -->

View File

@ -918,6 +918,13 @@ def bracket_split_build_line(
)
if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf)
)
# Except the false negatives above for PEP 604 unions where we
# can't add the comma.
and not (
leaves[0].parent
and leaves[0].parent.next_sibling
and leaves[0].parent.next_sibling.type == token.VBAR
)
)
if original.is_import or no_commas:

View File

@ -0,0 +1,25 @@
def some_very_long_name_function() -> my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | None:
pass
def some_very_long_name_function() -> my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | my_module.EvenMoreType | None:
pass
# output
def some_very_long_name_function() -> (
my_module.Asdf | my_module.AnotherType | my_module.YetAnotherType | None
):
pass
def some_very_long_name_function() -> (
my_module.Asdf
| my_module.AnotherType
| my_module.YetAnotherType
| my_module.EvenMoreType
| None
):
pass