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

[Python] Migrate from yapf to black #33138

Merged
merged 9 commits into from
Jun 9, 2023
Merged

Conversation

sergiitk
Copy link
Member

@sergiitk sergiitk commented May 16, 2023

  • Switched from yapf to black
  • Reconfigure isort for black
  • Resolve black/pylint idiosyncrasies

Note: I used --experimental-string-processing because black was producing "implicit string concatenation", similar to what described here: psf/black#1837. While currently this feature is experimental, it will be enabled by default: psf/black#2188. After running black with the new string processing so that the generated code merges these "hello" " world" strings concatenations, then I removed --experimental-string-processing for stability, and regenerated the code again.

To the reviewer: don't even try to open "Files Changed" tab 😄 It's better to review commit-by-commit, and ignore run black and isort.

@sergiitk
Copy link
Member Author

Sorry, had to force-push to preserve the commit structure.

@sergiitk
Copy link
Member Author

Yea, sorry, force pushes will have to happen in this PR.

@sergiitk
Copy link
Member Author

sergiitk commented May 16, 2023

In both cases I added more autogenerated files to the exclude list, re-ran black, and replaced the run black and isort commit. Otherwise, I'd have to manually find and restore each autogenerated file, which is prone to errors.

@sergiitk
Copy link
Member Author

Per @ctiller's recommendation, we're postponing this merge until after the end of the fixit. Then I'll sync it with master, and redo the code generation.

pyproject.toml Outdated
Comment on lines 24 to 30
src_paths = [
"examples/python/data_transmission",
"examples/python/async_streaming",
"tools/run_tests/xds_k8s_test_driver",
"src/python/grpcio_tests",
"tools/run_tests",
]
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this part it copied from tools/distrib/isort_code.sh, @gnossen do you know why only those paths are included?

Copy link
Contributor

Choose a reason for hiding this comment

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

For the examples, I think the answer is basically "Lidi wrote these examples." I'm surprised that the implementation directory is not included here, but if it never was to begin with, I'm fine with leaving this the way it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, that's weird. I just translated all the inline arguments to isort into a nice and readable config as is. The only change to isort settings is to use black profile, for obvious reasons.

else
readonly MODE="--in-place"
readonly VERBOSE="--verbose" # print out file names while processing
readonly MODE=""
Copy link
Contributor

Choose a reason for hiding this comment

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

What will this default mode do?

Copy link
Member Author

Choose a reason for hiding this comment

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

Black in the default mode (no args) will rewrite the code in place. yapf needed --in-place to do that, otherwise it'd just print the formatted file contents to the stdout.

Copy link
Contributor

@gnossen gnossen left a comment

Choose a reason for hiding this comment

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

Huge thanks for this. We've been wanting this for literally years.

pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Outdated
Comment on lines 24 to 30
src_paths = [
"examples/python/data_transmission",
"examples/python/async_streaming",
"tools/run_tests/xds_k8s_test_driver",
"src/python/grpcio_tests",
"tools/run_tests",
]
Copy link
Contributor

Choose a reason for hiding this comment

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

For the examples, I think the answer is basically "Lidi wrote these examples." I'm surprised that the implementation directory is not included here, but if it never was to begin with, I'm fine with leaving this the way it is.

@sergiitk
Copy link
Member Author

sergiitk commented Jun 9, 2023

Just a note: The last force push was regenerating the code after with target-version updated to py37...py311. It had no effect on the generated code (and that's OK).

@sergiitk
Copy link
Member Author

sergiitk commented Jun 9, 2023

PSM Interop (using reformatted framework and rebuilding the test images from this branch):

@sergiitk sergiitk requested review from gnossen and XuanWang-Amos June 9, 2023 08:24
@sergiitk
Copy link
Member Author

sergiitk commented Jun 9, 2023

@XuanWang-Amos Note I made a small change to src/python/grpcio/grpc/_observability.py - after reformatting pylint was complaining on

src/python/grpcio/grpc/_observability.py:33:4: E0601: Using variable 'ObservabilityPlugin' before assignment (used-before-assignment)

I just used the typehint notation made for this case - putting the class declared later in the same file in quotes.

veblush added a commit that referenced this pull request Jun 9, 2023
@sergiitk sergiitk merged commit de6ed9b into grpc:master Jun 9, 2023
@copybara-service copybara-service bot added the imported Specifies if the PR has been imported to the internal repository label Jun 9, 2023
@sergiitk sergiitk deleted the python-black branch June 9, 2023 22:41
sergiitk added a commit to sergiitk/grpc that referenced this pull request Jun 9, 2023
sergiitk added a commit that referenced this pull request Jun 10, 2023
PR #33138 is the Large Scale Change in which python style is changed
from yapf to black. Nearly all python files were reformatted. This PR
configures git clients supporting `.git-blame-ignore-revs` feature to
show the original author in `git blame`.

Details:
https://black.readthedocs.io/en/stable/guides/introducing_black_to_your_project.html
GitHub uses this feature by default:
https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view
Local git clients can enable this feature with `git config
blame.ignoreRevsFile .git-blame-ignore-revs`.
mario-vimal pushed a commit to mario-vimal/grpc that referenced this pull request Jun 15, 2023
mario-vimal pushed a commit to mario-vimal/grpc that referenced this pull request Jun 15, 2023
- Switched  from yapf to black
- Reconfigure isort for black
- Resolve black/pylint idiosyncrasies 

Note: I used `--experimental-string-processing` because black was
producing "implicit string concatenation", similar to what described
here: psf/black#1837. While currently this
feature is experimental, it will be enabled by default:
psf/black#2188. After running black with the
new string processing so that the generated code merges these `"hello" "
world"` strings concatenations, then I removed
`--experimental-string-processing` for stability, and regenerated the
code again.

To the reviewer: don't even try to open "Files Changed" tab 😄 It's
better to review commit-by-commit, and ignore `run black and isort`.
mario-vimal pushed a commit to mario-vimal/grpc that referenced this pull request Jun 15, 2023
PR grpc#33138 is the Large Scale Change in which python style is changed
from yapf to black. Nearly all python files were reformatted. This PR
configures git clients supporting `.git-blame-ignore-revs` feature to
show the original author in `git blame`.

Details:
https://black.readthedocs.io/en/stable/guides/introducing_black_to_your_project.html
GitHub uses this feature by default:
https://docs.github.com/en/repositories/working-with-files/using-files/viewing-a-file#ignore-commits-in-the-blame-view
Local git clients can enable this feature with `git config
blame.ignoreRevsFile .git-blame-ignore-revs`.
@jtattermusch
Copy link
Contributor

@sergiitk It would be good if the PR description contained info on what is the right command to run now (instead of tools/distrib/yapf_format.sh)

@sergiitk
Copy link
Member Author

@jtattermusch Sorry, this was a large PR, easy to miss stuff.

tools/distrib/yapf_code.sh was replaced by tools/distrib/black_code.sh. They are the scripts themselves are compatible API- (argument-) wise. See a7447dc.

sergiitk added a commit to sergiitk/grpc that referenced this pull request Dec 8, 2023
- Switched  from yapf to black
- Reconfigure isort for black
- Resolve black/pylint idiosyncrasies 

Note: I used `--experimental-string-processing` because black was
producing "implicit string concatenation", similar to what described
here: psf/black#1837. While currently this
feature is experimental, it will be enabled by default:
psf/black#2188. After running black with the
new string processing so that the generated code merges these `"hello" "
world"` strings concatenations, then I removed
`--experimental-string-processing` for stability, and regenerated the
code again.

To the reviewer: don't even try to open "Files Changed" tab 😄 It's
better to review commit-by-commit, and ignore `run black and isort`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants