Skip to content
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

Don't split type annotation with alternative union syntax #2038

Closed
cdce8p opened this issue Mar 12, 2021 · 2 comments
Closed

Don't split type annotation with alternative union syntax #2038

cdce8p opened this issue Mar 12, 2021 · 2 comments
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?

Comments

@cdce8p
Copy link
Contributor

cdce8p commented Mar 12, 2021

Describe the style change
With postponed evaluations of type annotations it's now possible to write a Union as int | None. With black 20.8b1 this can sometimes be formatted in a misleading way.

Examples in the current Black style

from __future__ import annotations

def f():
    args: None
    | tuple = None,  # this is a really long comment with some extra information

Desired style

from __future__ import annotations

def f():
    args: (
        None | tuple
    ) = None,  # this is a really long comment with some extra information

Additional context
With from __future__ import annotations, this syntax is valid in Python 3.7+
https://www.python.org/dev/peps/pep-0604/

@cdce8p cdce8p added the T: style What do we want Blackened code to look like? label Mar 12, 2021
@ambv
Copy link
Collaborator

ambv commented Apr 26, 2021

Note: the example isn't about variable annotations where this sort of split would be illegal.

It's about function arguments:

def f(
    args: tuple 
    | None = None,
):
    ...

Indeed, it would be nicer to format like what Black does for variable annotations:

-    some_argument: tuple | None = None  # very very very very very very very very very very very very
+    some_argument: tuple | None = (
+        None  # very very very very very very very very very very very very
+    )

Note that if there is no assignment, Black doesn't even attempt a split on a line that's too long:

some_argument: tuple | None  # very very very very very very very very very very very very

It's left as is.

@ichard26 ichard26 added the S: accepted The changes in this design / enhancement issue have been accepted and can be implemented label Apr 28, 2021
@JelleZijlstra JelleZijlstra added the F: linebreak How should we split up lines? label May 30, 2021
@JelleZijlstra
Copy link
Collaborator

Duplicate of #2316

@JelleZijlstra JelleZijlstra marked this as a duplicate of #2316 Jan 29, 2022
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

4 participants