-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Group function definition parameters with return type annotations #6410
Conversation
fbff995
to
4f7d0fd
Compare
As I said in the summary, I'd like feedback on |
4f7d0fd
to
311c695
Compare
311c695
to
7452536
Compare
(I think this also breaks the behavior whereby we insert a trailing comma if the parameters group breaks, but I believe I can fix that.) |
6fcd9af
to
8e4f85e
Compare
PR Check ResultsBenchmarkLinux
Windows
|
03eb93f
to
862e7ad
Compare
crates/ruff_python_formatter/src/statement/stmt_function_def.rs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the black test cases, i think the following is another one of those where black doesn't add parentheses if it doesn't help with fitting:
black:
def foo(
a: int,
b: int,
c: int,
) -> intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds:
return 2
ours:
def foo(
a: int,
b: int,
c: int,
) -> (
intsdfsafafafdfdsasdfsfsdfasdfafdsafdfdsfasdskdsdsfdsafdsafsdfdasfffsfdsfdsafafhdskfhdsfjdslkfdlfsdkjhsdfjkdshfkljds
):
return 2
Otherwise black_compatibility@simple_cases__return_annotation_brackets.py.snap looks good
2161c23
to
8cc0a1d
Compare
7452536
to
a4f7190
Compare
Okay, this got way simpler thanks for the feedback. |
…6413) ## Summary Given: ```python def double(a: int) -> ( # Hello int ): return 2*a ``` We currently treat `# Hello` as a trailing comment on the parameters (`(a: int)`). This PR adds a placement method to instead treat it as a dangling comment on the function definition itself, so that it gets formatted at the end of the definition, like: ```python def double(a: int) -> int: # Hello return 2*a ``` The formatting in this case is unchanged, but it's incorrect IMO for that to be a trailing comment on the parameters, and that placement leads to an instability after changing the grouping in #6410. Fixing this led to a _different_ instability related to tuple return type annotations, like: ```python def zrevrangebylex(self, name: _Key, max: _Value, min: _Value, start: int | None = None, num: int | None = None) -> ( # type: ignore[override] ): ... ``` (This is a real example.) To fix, I had to special-case tuples in that spot, though I'm not certain that's correct.
a4f7190
to
f248478
Compare
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
Co-authored-by: Micha Reiser <micha@reiser.io>
…stral-sh#6413) ## Summary Given: ```python def double(a: int) -> ( # Hello int ): return 2*a ``` We currently treat `# Hello` as a trailing comment on the parameters (`(a: int)`). This PR adds a placement method to instead treat it as a dangling comment on the function definition itself, so that it gets formatted at the end of the definition, like: ```python def double(a: int) -> int: # Hello return 2*a ``` The formatting in this case is unchanged, but it's incorrect IMO for that to be a trailing comment on the parameters, and that placement leads to an instability after changing the grouping in astral-sh#6410. Fixing this led to a _different_ instability related to tuple return type annotations, like: ```python def zrevrangebylex(self, name: _Key, max: _Value, min: _Value, start: int | None = None, num: int | None = None) -> ( # type: ignore[override] ): ... ``` (This is a real example.) To fix, I had to special-case tuples in that spot, though I'm not certain that's correct.
…tral-sh#6410) ## Summary This PR removes the group around function definition parameters, instead grouping the parameters with the type parameters and return type annotation. This increases Zulip's similarity score from 0.99385 to 0.99699, so it's a meaningful improvement. However, there's at least one stability error that I'm working on, and I'm really just looking for high-level feedback at this point, because I'm not happy with the solution. Closes astral-sh#6352. ## Test Plan Before: - `zulip`: 0.99396 - `django`: 0.99784 - `warehouse`: 0.99578 - `build`: 0.75436 - `transformers`: 0.99407 - `cpython`: 0.75987 - `typeshed`: 0.74432 After: - `zulip`: 0.99702 - `django`: 0.99784 - `warehouse`: 0.99585 - `build`: 0.75623 - `transformers`: 0.99470 - `cpython`: 0.75988 - `typeshed`: 0.74853
Summary
This PR removes the group around function definition parameters, instead grouping the parameters with the type parameters and return type annotation.
This increases Zulip's similarity score from 0.99385 to 0.99699, so it's a meaningful improvement. However, there's at least one stability error that I'm working on, and I'm really just looking for high-level feedback at this point, because I'm not happy with the solution.
Closes #6352.
Test Plan
Before:
zulip
: 0.99396django
: 0.99784warehouse
: 0.99578build
: 0.75436transformers
: 0.99407cpython
: 0.75987typeshed
: 0.74432After:
zulip
: 0.99702django
: 0.99784warehouse
: 0.99585build
: 0.75623transformers
: 0.99470cpython
: 0.75988typeshed
: 0.74853