New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Black collapses lists in return type annotations onto one line, regardless of the "magic" trailing comma. #3018
Comments
or just: a: list[int,] With |
I've come here searching for the opposite problem. Black keeps splitting my type annotations (e.g. |
@AIGeneratedUsername are you talking about our subpar formatting of the new union syntax (see #2316) or the formatting of type annotations in general? I don't think it's tenable to just leave type annotations alone. |
Thank you for pointing to a related issue. I've found that my complain while not fully, but mostly described by #2316 (comment) |
This is intended behavior (though I'm not sure why), and probably unrelated: black/tests/data/simple_cases/skip_magic_trailing_comma.py Lines 1 to 3 in 3dcacdd
|
@jakkdl We shouldn't remove the trailing comma in that case because it changes the AST. |
I dug further into this, and it ended up revealing that Long return type causes parameter list to get split unnecessarily def foo(a) -> list[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]:
pass
# output
def foo(
a,
) -> list[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
]:
pass magic trailing comma in return type causes param list to get split def foo(a, b) -> list[a, a,]: ...
# output
def foo(
a, b
) -> list[a, a,]: ... replacing square brackets with parens gives same behaviour. So there may be other open issues that would get resolved by the same fix. I should be able to write a PR soonish now that I've nailed down where the problem is. |
@JelleZijlstra (and/or others) I got one case which I'm a bit iffy about, currently the code will reformat very long single-name return types and function names like so: def foo() -> (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
):
return 2
def thiiiiiiiiiiiiiiiiiis_iiiiiiiiiiiiiiiiiiiiiiiiiiiiiis_veeeeeeeeeeeeeeeeeeeeeeery_looooooong() -> (
list[int, float]
): ... but I'm partial to this variant instead, which I especially prefer when there's a return type: def foo(
) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
def thiiiiiiiiiiiiiiiiiis_iiiiiiiiiiiiiiiiiiiiiiiiiiiiiis_veeeeeeeeeeeeeeeeeeeeeeery_looooooong(
) -> list[int, float]: ... I can make the code follow the former and minimize changes no problem, or are we fine with changing to the latter? Admittedly these are very obscure cases so it probably doesn't matter a ton either way, and as such should maybe just minimize any changes. |
I think I prefer the former, it looks weird to have the parentheses on separate lines with nothing in between. |
Describe the bug
Black collapses lists in return type annotations onto one line, regardless of the "magic" trailing comma.
To Reproduce
For example, take this code:
And run it with these arguments:
The resulting file contains:
Expected behavior
I would have expected the code to either
Environment
black, 22.3.0 (compiled: yes)
The text was updated successfully, but these errors were encountered: