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

Formatter undocumented deviation: Formatting of long lambda as keyword argument #8179

Open
henribru opened this issue Oct 24, 2023 · 10 comments
Labels
bug Something isn't working formatter Related to the formatter

Comments

@henribru
Copy link

henribru commented Oct 24, 2023

Black formatting:

def a():
    return b(
        c,
        d,
        e,
        f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
            *args, **kwargs
        ),
    )

Ruff formatting:

def a():
    return b(
        c,
        d,
        e,
        f=lambda self,
        *args,
        **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
    )

In addition to being incompatible, this formatting seems very strange and not very readable, as the *args and **kwargs suddenly look like they are arguments to b.

@henribru henribru changed the title Formatter undocumented deviation: Formatter undocumented deviation: Formatting of long lambda as keyword argument Oct 24, 2023
@charliermarsh
Copy link
Member

Thanks!

@charliermarsh charliermarsh added the formatter Related to the formatter label Oct 24, 2023
@charliermarsh charliermarsh added this to the Formatter: Stable milestone Oct 24, 2023
@charliermarsh charliermarsh added the bug Something isn't working label Oct 24, 2023
@charliermarsh
Copy link
Member

Yeah, looks like we have some refinements to make in the lambda formatting. On it! (Thanks for sharing playground links, it helps a lot.)

@DarthMuzammil
Copy link

@charliermarsh Can i take this issue up?

@MichaReiser
Copy link
Member

@DarthMuzammil sure. Let me know if you need any help

@charliermarsh
Copy link
Member

@DarthMuzammil -- have you had a chance to look into this? If not, do you still plan to? (No pressure, I just want to make sure we find an owner soon if it's not being addressed.)

@MichaReiser
Copy link
Member

MichaReiser commented Nov 2, 2023

It seems black never inserts soft line breaks between parameters. This would be an easy fix... except for comments

Although Black's formatting also has its shortcomings when you have default values:

def a():
    return b(
        c,
        d,
        e,
        f=lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
            *args, **kwargs
        ), e=1, f=2, g=2: d,
    )

It could be nice to simply parenthesize the lambda expression and indent it by 1 level (and otherwise keep our formatting). However, that would also mean that the formatter must remove the parentheses again when the lambda fits on a single line—something we haven't really done before in sub-expression positions. And there's also no real good place to add it, because this should be done when formatting lambas in any position.

@DarthMuzammil
Copy link

@charliermarsh Hey, sorry for the delay i just saw this message, yes i will start working on this today

@DarthMuzammil
Copy link

It seems black never inserts soft line breaks between parameters. This would be an easy fix... except for comments

Although Black's formatting also has its shortcomings when you have default values:

def a():
    return b(
        c,
        d,
        e,
        f=lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
            *args, **kwargs
        ), e=1, f=2, g=2: d,
    )

It could be nice to simply parenthesize the lambda expression and indent it by 1 level (and otherwise keep our formatting). However, that would also mean that the formatter must remove the parentheses again when the lambda fits on a single line—something we haven't really done before in sub-expression positions. And there's also no real good place to add it, because this should be done when formatting lambas in any position.

@MichaReiser How should i go about solving this issue?

@MichaReiser
Copy link
Member

MichaReiser commented Nov 3, 2023

@DarthMuzammil not sure. I think coming up with a style that works with the presence of comments (which black does not always) is the main work of this issue. My preferred solution would be to just parenthesize the lambda parameters if it breaks, but Python3 removed support for parenthesized lambda parameters for some reason :(

# Using parentheses is a syntax error
def a():
    return b(
        c,
        d,
        e,
        f=lambda (
            self, 
            araa, 
            kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
                *args, **kwargs
            ), 
            e=1, 
            f=2, 
            g=2
        ): d,
    )

Related black issue: psf/black#809

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

Successfully merging a pull request may close this issue.

4 participants