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 instability: own-line comments on function parameter defaults #7603

Closed
charliermarsh opened this issue Sep 22, 2023 · 6 comments · Fixed by #7870
Closed

Formatter instability: own-line comments on function parameter defaults #7603

charliermarsh opened this issue Sep 22, 2023 · 6 comments · Fixed by #7870
Assignees
Labels
accepted Ready for implementation bug Something isn't working formatter Related to the formatter

Comments

@charliermarsh
Copy link
Member

Given:

def f2(x:e2=
    #
"foo") -> r2:
    pass

def f3(#
 x:e3#=
  = #
    #
 123#
#
) -> r3:
    pass

We first format as:

def f2(
    x: e2 = #
    "foo",
) -> r2:
    pass


def f3(  #
    x: e3 = #  # =  #
    123,  #
    #
) -> r3:
    pass

And then as:

def f2(
    x: e2 = "foo",  #
) -> r2:
    pass


def f3(  #
    x: e3 = 123,  #  # =  #  #
    #
) -> r3:
    pass

We probably need to treat those comments as dangling, as for comments around colons.

Sourced from #7590 (A_870428921595896654.py).

@charliermarsh charliermarsh added bug Something isn't working formatter Related to the formatter labels Sep 22, 2023
@charliermarsh charliermarsh added this to the Formatter: Beta milestone Sep 22, 2023
@MichaReiser
Copy link
Member

It may also be possible (at last for case 1) to enforce a hard line break if the default value has a leading comment.

@charliermarsh
Copy link
Member Author

The challenge there is that the leading comment could be within parentheses.

@MichaReiser
Copy link
Member

The challenge there is that the leading comment could be within parentheses.

I see. Would be good if we have a solution for this that works consistently. E.g. a has_leading_comments_outside_parentheses. There's another issue around this as well but I can't find it right now

@MichaReiser
Copy link
Member

As a diff

---
--- Formatted once
+++ Formatted twice
@@ -1,13 +1,11 @@
 def f2(
-    x: e2 = #
-    "foo",
+    x: e2 = "foo",  #
 ) -> r2:
     pass
 
 
 def f3(  #
-    x: e3 = #  # =  #
-    123,  #
+    x: e3 = 123,  #  # =  #  #
     #
 ) -> r3:
     pass
---

@charliermarsh
Copy link
Member Author

Keeping this for the Beta. It's likely uncommon but should be fixed.

@charliermarsh charliermarsh added the accepted Ready for implementation label Sep 27, 2023
@charliermarsh
Copy link
Member Author

(I'd suggest taking a look at #7493 and #7495, we can handle similarly to those.)

@konstin konstin self-assigned this Oct 9, 2023
konstin added a commit that referenced this issue Oct 9, 2023
**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.
konstin added a commit that referenced this issue Oct 12, 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.

```python
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants