-
Notifications
You must be signed in to change notification settings - Fork 4.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
[BEAM-9175] Introduce an autoformatting tool to Python SDK #10684
Conversation
sdks/python/apache_beam/typehints/trivial_inference_test_py3.py
Outdated
Show resolved
Hide resolved
retest this please |
We should add a pre-commit lint job to run From the docs:
Once this is in place we can dismantle the pylint and flake8 checks that overlap with yapf. |
I would suggest COALESCE_BRACKETS , SPLIT_BEFORE_FIRST_ARGUMENT, SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES, and possibly SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN to reduce churn. SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED could also give authors a bit more freedom. |
Why was SPLIT_BEFORE_LOGICAL_OPERATOR disabled? Also, +1 to a precommit. |
|
@robertwb I managed to add all knobs you asked for. I think the code looks better now (much less weird lines, that's for sure). I also added a pre-commit job that runs yapf with --diff option. |
There is still a number of pylint issues beam/sdks/python/apache_beam/io/gcp/bigquery_test.py Lines 908 to 910 in 7db61fb
I don't know yet how to deal with it. Unless there is a knob for this, I'll just put a # yapf: disable comments in these places. |
Is it possible to split change to one that adds formatter and one that shows what formatting actually looks like? |
+1 for running yapf on pre-commit. (Can be done in separate PR) |
If we adopt yapf's indentation style, then we can disable the same check in pylint. |
If we utilize autoformatter, especially on pre-commit, it would be feasible to disable all formatting checks on linter. They become obsolete. |
It's already split like this. The first commit only adds the formatter, and the second commit is the one that applies formatting changes to the code. Or do you mean creating a separate PR? |
That's true. If that's so, I can disable that particular pylint check which causes problems. The question is, are we OK with how some lambdas are formatted? |
retest this please |
2 similar comments
retest this please |
retest this please |
Run Java_Examples_Dataflow PreCommit |
1 similar comment
Run Java_Examples_Dataflow PreCommit |
Run seed job |
run seed job |
Run PythonFormatter PreCommit |
I'm not a huge fan of how lambdas are formatted (tempted to file a bug against yapf), but there aren't too many of them so I'd be fine either disabling lint or disabling yapf for those lines. |
Pylint shows 67 bad-continuation warnings, but some of them refer to the same malformed lambda. This means that we have 30-40 malformed lambdas in the whole project. I agree this isn't a large number. @robertwb I've already disable lint for those lines in this PR |
|
LGTM |
Run seed job |
retest this please |
Run PythonFormatter PreCommit |
Run Python PreCommit |
1 similar comment
Run Python PreCommit |
Is it possible to update some docs/wiki, explaining how users could run this locally? |
Yes, luckily I have edit access to the wiki. I'll prepare an instruction. I think Python Tips is the best place for this. |
run seed job |
retest this please |
run seed job |
1 similar comment
run seed job |
retest this please |
Run PythonFormatter PreCommit |
Thanks everyone! I've added an appropriate instruction here: https://cwiki.apache.org/confluence/display/BEAM/Python+Tips |
@@ -35,6 +35,9 @@ lint.dependsOn lintPy37 | |||
toxTask "mypyPy37", "py37-mypy" | |||
lint.dependsOn mypyPy37 | |||
|
|||
toxTask "formatter", "py3-yapf-check" | |||
check.dependsOn formatter |
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.
I missed this in my review. The lint precommit runs :pythonLintPreCommit, which depends on lint
tasks.
Suggestion:
lint.dependsOn formatter
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.
Nevermind, I see there's a new job named PythonFormatter
This PR adds yapf configuration and all the changes made after running it over the entire Python SDK codebase.
The result is fully compliant with PEP8 (lint check passes, the issues were manually resolved).
Some of the examples and rules of the newly enforced style (they can be put under discussion and reviewed):
Multiple dict entries can be put in the same line.
before:
beam/sdks/python/apache_beam/io/avroio_test.py
Lines 72 to 91 in b689fff
after:
beam/sdks/python/apache_beam/io/avroio_test.py
Lines 71 to 76 in cfa9034
With this setting, it seems the result conforms more to the already used style (lower number of lines changed).
Yapf tries to put the first parameter on the same line where the function begins:
beam/sdks/python/apache_beam/utils/annotations_test.py
Lines 56 to 61 in cfa9034
beam/sdks/python/apache_beam/io/gcp/bigquery.py
Lines 398 to 407 in cfa9034
...unless some of parameters have type annotation or are too long (then it does the split before the first argument):
beam/sdks/python/apache_beam/runners/direct/watermark_manager.py
Lines 129 to 136 in cfa9034
Indent width is 2 and continuation indent is 4.
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
R: @username
).[BEAM-XXX] Fixes bug in ApproximateQuantiles
, where you replaceBEAM-XXX
with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.See the Contributor Guide for more tips on how to make review process smoother.
Post-Commit Tests Status (on master branch)
Pre-Commit Tests Status (on master branch)
See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.