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

PEP 604 type hints in a class definition are not broken up when hitting the line length limit #3834

Closed
mmohrhard opened this issue Aug 11, 2023 · 2 comments
Labels
T: bug Something isn't working

Comments

@mmohrhard
Copy link

I have checked with the online formatter that it is not fixed in the most up to date development branch.

Describe the bug

The following code has a line that is longer than 88 characters and yet is not split by black:

class LongNameLongName:
    pass


class LongNameLongName2:
    pass


class LongNameLongName3:
    pass


class LongNameLongName4:
    pass


class LongNameLongName5:
    pass


class LongNameLongName6:
    pass


class ClassWithTypeDefs:
    name: str
    field: LongNameLongName | LongNameLongName2 | LongNameLongName3 | LongNameLongName4 | LongNameLongName5 | LongNameLongName6


def func(
    a: LongNameLongName
    | LongNameLongName2
    | LongNameLongName3
    | LongNameLongName4
    | LongNameLongName5
    | LongNameLongName6,
):
    pass

As can be seen, for a function black breaks the typing statement into multiple lines but not for the class definition.

$ flake8 --max-line-length 88 test.py
test.py:27:89: E501 line too long (127 > 88 characters)
$ black -l 88 -t py311 test.py
All done! ✨ 🍰 ✨
1 file left unchanged.

Expected behavior

black should break the line or at least not reformat the code to a line longer than the line width limit if the line was broken up before.

Environment

Linux
$ black --version
black, 23.7.0 (compiled: no)
Python (CPython) 3.11.4
@mmohrhard mmohrhard added the T: bug Something isn't working label Aug 11, 2023
@jakkdl
Copy link
Contributor

jakkdl commented Oct 7, 2023

This gets wrapped with parentheses and no longer breaks the line length after #3899, and can be closed.

@JelleZijlstra
Copy link
Collaborator

Indeed

% black --preview -c '''class ClassWithTypeDefs:
    name: str
    field: LongNameLongName | LongNameLongName2 | LongNameLongName3 | LongNameLongName4 | LongNameLongName5 | LongNameLongName6
'''
class ClassWithTypeDefs:
    name: str
    field: (
        LongNameLongName
        | LongNameLongName2
        | LongNameLongName3
        | LongNameLongName4
        | LongNameLongName5
        | LongNameLongName6
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants