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

Format comment before parameter default correctly #7870

Merged
merged 1 commit into from
Oct 12, 2023
Merged

Format comment before parameter default correctly #7870

merged 1 commit into from
Oct 12, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Oct 9, 2023

Summary Handle comment before the default values of function parameters correctly by inserting a line break instead of space after the equals sign where required.

def f(
    a = # parameter trailing comment; needs line break
    1,
    b =
    # default leading comment; needs line break
    2,
    c = ( # the default leading can only be end-of-line with parentheses; no line break
        3
    ),
    d = (
        # own line leading comment with parentheses; no line break
        4
    )
)

Fixes #7603

Test Plan Added the different cases and one more complex case as fixtures.

**Handle comment before the default values of function parameters correctly by inserting a line break instead of space after the equals sign where required.

```python
def f(
    a = # parameter trailing comment; needs line break (1)
    1,
    b =
    # default leading comment; needs line break (2)
    2,
    c = ( # the default leading can only be end-of-line with parentheses; no line break
        3
    ),
    d = (
        # own line leading comment with parentheses; no line break
        4
    )
)
```

Fixes #7603

**Test Plan** Added the different cases and one more complex case as fixtures.
Comment on lines +63 to +64
(!needs_line_break).then_some(space),
needs_line_break.then_some(hard_line_break()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was meant as an if-then-else, not sure if it's better to split the write! into parts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as-is.

Comment on lines +63 to +64
(!needs_line_break).then_some(space),
needs_line_break.then_some(hard_line_break()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine as-is.

@charliermarsh charliermarsh added bug Something isn't working formatter Related to the formatter labels Oct 12, 2023
@konstin konstin merged commit 3944c42 into main Oct 12, 2023
16 checks passed
@konstin konstin deleted the fix-7603 branch October 12, 2023 15:50
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 this pull request may close these issues.

Formatter instability: own-line comments on function parameter defaults
2 participants