-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 removes parentheses around call chaining #7050
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Interesting, it looks like Black will preserve parentheses around the first part of a call chain, no matter how many components are included in the parentheses (example). Not obvious to me whether we want to preserve this or not. |
Hmm maybe I'm misunderstanding your example. It seems to preserve with Ruff (playground). But I can boil my original example down as: Within some parenthesized context you can get the following behavior: I can get both Ruff and Black to preserve the parenthesized dataframe/series expression with one method call # Input
[(df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"]).groupby()]
# Black
[(df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"]).groupby()]
# Ruff
[(df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"]).groupby()] When you add a second method call Ruff will strip the parentheses # Input
[(df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"]).groupby().sum()]
# Black
[(df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"]).groupby().sum()]
# Ruff
[df1_aaaaaaaaaaaa.loc[df1_aaaaaaaaaaaa["a"] == "aaaaaaaaaaaaaaaaa"].groupby().sum()] What I want to play with would be examples requiring us to retain order of operations for dataframe/series operations. |
And even simpler the same behavior occurs with: [((d)).call()] Which is a good example of what you're pointing out. |
Oh wow, you don't need the parenthesized context either. ((d)).call() |
Ruff should preserve expression-parentheses except in clause headers (like |
@charliermarsh are you working on this or is it up for grabs? |
I was working on this but didn’t get to PR yet. I believe it’s specific to call chain formatting. |
If I don’t find time to fix today, I’ll unassign myself :) |
sounds good. @konstin has context on this as well. |
For reference, using this to demonstrate Black's behavior are parenthesized "heads" on call chains. |
I made some progress on this at |
Version: ruff 0.0.287
Line-length: 88
Related: #5343
My team does a lot of exploratory scripting with Pandas (most are analysts, not developers), so often some analysts will chain together Pandas methods for SQL-like operations.
Note that the removed parentheses still results in valid Python. I actually prefer this change since the expression is already wrapped by
pd.concat
and shouldn't confuse the original author or subsequent readers. The removed parentheses, IMO, are redundant (see(...).groupby(..)
).I figured this kind of code is probably common outside the current ecosystem checks (and in Python's data analysis world), especially with Pandas users.
The following is an example of a diff snippet from one of our repositories:
Input, formatted with
black
(23.7.0):ruff format
diff:Playground: https://play.ruff.rs/11aa202b-fee6-4a8f-bc61-330acffeca9c
More to come :)
The text was updated successfully, but these errors were encountered: