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

add ufmt as code formatter #4384

Merged
merged 24 commits into from
Oct 4, 2021
Merged

add ufmt as code formatter #4384

merged 24 commits into from
Oct 4, 2021

Conversation

pmeier
Copy link
Collaborator

@pmeier pmeier commented Sep 8, 2021

Supersedes #4178. Instead of using isort and black individually, ufmt uses usort and black in an atomic way.

To keep this PR reviewable, I'm only going to pull the trigger on all formatting changes if the PR is approved otherwise.

This PR is blocked for now until ufmt>=1.3 is released.

@pmeier pmeier linked an issue Sep 8, 2021 that may be closed by this pull request
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Sweet, thanks @pmeier ! I made a quick pass, the strategy sounds good.

Do we know when the ufmt release will be out?

pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@@ -2,7 +2,7 @@
from torch import nn, Tensor

import torchvision
from torchvision.ops import roi_align
from .roi_align import roi_align
Copy link
Member

Choose a reason for hiding this comment

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

just curious, was this flagged somehow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See #4178 (comment). In short: we relied on a specific order of the imports due to a naming conflict. Without this fix we need to exclude these imports from being sorted.

Copy link
Member

Choose a reason for hiding this comment

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

There might have been a reason for that initially, but I'm wondering why we even use absolute imports within the torchvision package. Every torchvision improt should be relative there. Perhaps @fmassa knows?

(anyway, this is for another PR, let's leave it as is :) )

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps because absolute import is recommended by PEP8?

pyproject.toml Show resolved Hide resolved
Copy link
Collaborator Author

@pmeier pmeier left a comment

Choose a reason for hiding this comment

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

The lint workflow is failing, because the version of ufmt that we need. Otherwise this should be good to go.

@NicolasHug: in #4178 we talked about adding pre-commit hooks for this. I have a PR up that adds this to ufmt: omnilib/ufmt#15. Since they are not necessary to run ufmt, IMO we can add them in the future.

pyproject.toml Outdated Show resolved Hide resolved
pyproject.toml Outdated
Comment on lines 16 to 20
local_utils = [
"_utils_internal",
"common_utils",
"dataset_utils",
]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These are local utilities in our test/ folder. Since its not a package, i.e. tests/__init__.py is missing, usort does not pick up on the fact that these are local dependencies and sorts them into the third party category.

Copy link
Member

Choose a reason for hiding this comment

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

Let's add this comment in the file to provide context for future us :)

Also to avoid conflicting it with other utils, can we change the name to local_test_utils? Alternatively we could just add these into the "first-party" category?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alternatively we could just add these into the "first-party" category?

We could, but if you don't have a strong opinion about this, I would leave them in an extra category. IMO, first party and local should be separated, because the latter indicates that this cannot be import the "normal" way. In fact, isort has a section for local imports where all the relative imports go. Unfortunately, usort does not have that. I've asked about it in facebook/usort#30.

Comment on lines +12 to +18
ignore = E203, E402, W503, W504, F821
per-file-ignores =
__init__.py: F401, F403, F405
./hubconf.py: F401
torchvision/models/mobilenet.py: F401, F403
torchvision/models/quantization/mobilenet.py: F401, F403
test/smoke_test.py: F401
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See #4178 (comment) for details on this change.

@pmeier pmeier marked this pull request as ready for review September 9, 2021 06:40
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @pmeier

@NicolasHug: in #4178 we talked about adding pre-commit hooks for this. I have a PR up that adds this to ufmt: omnilib/ufmt#15. Since they are not necessary to run ufmt, IMO we can add them in the future.

I'm a bit confused, do we need pre-commit hooks in the ufmt repo in order to have pre-commit hooks in torchvision?

pyproject.toml Outdated
Comment on lines 16 to 20
local_utils = [
"_utils_internal",
"common_utils",
"dataset_utils",
]
Copy link
Member

Choose a reason for hiding this comment

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

Let's add this comment in the file to provide context for future us :)

Also to avoid conflicting it with other utils, can we change the name to local_test_utils? Alternatively we could just add these into the "first-party" category?

@@ -2,7 +2,7 @@
from torch import nn, Tensor

import torchvision
from torchvision.ops import roi_align
from .roi_align import roi_align
Copy link
Member

Choose a reason for hiding this comment

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

There might have been a reason for that initially, but I'm wondering why we even use absolute imports within the torchvision package. Every torchvision improt should be relative there. Perhaps @fmassa knows?

(anyway, this is for another PR, let's leave it as is :) )

pyproject.toml Outdated Show resolved Hide resolved
@pmeier
Copy link
Collaborator Author

pmeier commented Sep 9, 2021

I'm a bit confused, do we need pre-commit hooks in the ufmt repo in order to have pre-commit hooks in torchvision?

Just to be sure: we are talking about hooks added by the pre-commit framework to add these hooks and not provide a homebrew solution, right?

If yes, we don't need pre-commit hooks in the ufmt config, but a configuration telling pre-commit how to use the ufmt repository. The confusing thing about this is probably the file names:

  • .pre-commit-config.yaml defines what hooks should be run before every commit (this is the file we need)
  • .pre-commit-hooks.yaml defines what hooks the repository provides in case it is used by pre-commit (this is the file ufmt needs)

@NicolasHug
Copy link
Member

Oh OK, thanks. I didn't know the "upstream" repo (ufmt) had to be configured as well for commit hooks to work.

I feel like we might want to wait for commit hooks to be available though. It will probably make the transition smoother for most contributors / developers, and we will just be able to run pre-commit run --all-files to make sure to avoid any potential future change

@pmeier
Copy link
Collaborator Author

pmeier commented Sep 10, 2021

@NicolasHug I've added support for pre-commit hooks in #4387. If that is landed and ufmt supports being used as one, we only need to add a few lines to the .pre-commit-config.yaml file.

@pmeier
Copy link
Collaborator Author

pmeier commented Sep 15, 2021

Blocked by omnilib/ufmt#13 and omnilib/ufmt#15. After these PRs are in we also need to wait for the next release, which I'm hoping will be quick, since we added some important functionality.

pyproject.toml Outdated Show resolved Hide resolved
@pmeier
Copy link
Collaborator Author

pmeier commented Sep 18, 2021

The PRs I sent to ufmt were landed so this PR is almost ready to go. As soon as ufmt has its next release (@jreese is there a timeline for that?) I'll push the changes. I have them locally by running ufmt's main branch as reference.

Since this PR will touch (almost?) any *.py file in our repository, we will probably get merge conflicts for every open PR. I'll comment an upgrade guide, so we can point contributors here. cc @fmassa @datumbox @NicolasHug @prabhat00155

@pmeier
Copy link
Collaborator Author

pmeier commented Sep 18, 2021

If you are reading this, you were probably prompted to fix the code format of your PR. Here is what you do:

$ git checkout main
$ git pull
$ git checkout $MY_FEATURE_BRANCH
$ git merge -X ours main
$ pre-commit run --all-files
$ git commit -am "fix code format"
$ git push

If you don't have pre-commit installed, you can install it with pip install pre-commit or conda install -c conda-forge pre-commit. To avoid running this manually in the future, you can pre-commit install some hooks that will be run everytime you git commit something in this repository.



PYTHON_VERSIONS = ["3.6", "3.7", "3.8", "3.9"]

RC_PATTERN = r"/v[0-9]+(\.[0-9]+)*-rc[0-9]+/"


def build_workflows(prefix='', filter_branch=None, upload=False, indentation=6, windows_latest_only=False):
def build_workflows(prefix="", filter_branch=None, upload=False, indentation=6, windows_latest_only=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the a way to configure ufmt to avoid making this ' -> " change?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so, using double quotes is black's convention

Copy link
Collaborator Author

@pmeier pmeier Oct 4, 2021

Choose a reason for hiding this comment

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

black is the tool doing this. If we don't want it, we could put a skip_string_normalization = true line into its section in the pyproject.toml.

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel this is adding a number of unnecessary changes, given pep8 doesn't recommend one over the other.

Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately this isn't something we can change, as the internal arc lint would force these back to double quotes

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought we were not using the default internal linter. Can't we configure that the same way as pointed above by Philip?

Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's possible, but even if it were I wouldn't recommend it. It's better to stick to the usual convention as much as possible. The only one we don't properly respect is the line length, for historical reasons. Are you mostly concerned about git blame noise?

Copy link
Contributor

@prabhat00155 prabhat00155 Oct 4, 2021

Choose a reason for hiding this comment

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

I have the following concerns here:

  1. Unnecessary changes.
  2. I am not convinced with the idea of forcing " over '. This should be a user choice given that pep8 doesn't recommend either. Also, this is not the usual convention. Line length is something recommend by pep8.
  3. Python by default uses ' for representing string:
>>> a = "abc"
>>> a
'abc'
  1. ' uses 1 key stroke against 2 for ".

Copy link
Member

Choose a reason for hiding this comment

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

regarding 4, you're free to use single quotes if you want and let black automatically format them :)

For the rest, I understand your point, but one of the goal of using black is precisely to avoid that kind of discussion about style preferences. It may not be perfect and suit everyone's taste (it doesn't suit mine either), but at least it's a standard that we can all automatically comply to.

@NicolasHug
Copy link
Member

Sync is looking good now (it should look like D31379666 and the few diffs below).

I'll merge this when green. We can address the flake8 issues later, they're almost all unused imports

@NicolasHug
Copy link
Member

Tests are green. Let's get this in.
🥵
May the syncing gods be with me

@NicolasHug NicolasHug merged commit 5f0edb9 into pytorch:main Oct 4, 2021
@github-actions
Copy link

github-actions bot commented Oct 4, 2021

Hey @NicolasHug!

You merged this PR, but no labels were added.

@NicolasHug
Copy link
Member

A million thanks to you @pmeier for your help

@pmeier pmeier deleted the ufmt branch October 4, 2021 14:33
@NicolasHug NicolasHug mentioned this pull request Oct 4, 2021
facebook-github-bot pushed a commit that referenced this pull request Oct 7, 2021
Summary:
* add ufmt as code formatter

* cleanup

* quote ufmt requirement

* split imports into more groups

* regenerate circleci config

* fix CI

* clarify local testing utils section

* use ufmt pre-commit hook

* split relative imports into local category

* Revert "split relative imports into local category"

This reverts commit f2e224c.

* pin black and usort dependencies

* fix local test utils detection

* fix ufmt rev

* add reference utils to local category

* fix usort config

* remove custom categories sorting

* Run pre-commit without fixing flake8

* got a double import in merge

Reviewed By: kazhang

Differential Revision: D31380329

fbshipit-source-id: c1ee3a57589401d88877201fbf40cc968681b640

Co-authored-by: Nicolas Hug <nicolashug@fb.com>
mszhanyi pushed a commit to mszhanyi/vision that referenced this pull request Oct 19, 2021
* add ufmt as code formatter

* cleanup

* quote ufmt requirement

* split imports into more groups

* regenerate circleci config

* fix CI

* clarify local testing utils section

* use ufmt pre-commit hook

* split relative imports into local category

* Revert "split relative imports into local category"

This reverts commit f2e224c.

* pin black and usort dependencies

* fix local test utils detection

* fix ufmt rev

* add reference utils to local category

* fix usort config

* remove custom categories sorting

* Run pre-commit without fixing flake8

* got a double import in merge

Co-authored-by: Nicolas Hug <nicolashug@fb.com>
cyyever pushed a commit to cyyever/vision that referenced this pull request Nov 16, 2021
* add ufmt as code formatter

* cleanup

* quote ufmt requirement

* split imports into more groups

* regenerate circleci config

* fix CI

* clarify local testing utils section

* use ufmt pre-commit hook

* split relative imports into local category

* Revert "split relative imports into local category"

This reverts commit f2e224c.

* pin black and usort dependencies

* fix local test utils detection

* fix ufmt rev

* add reference utils to local category

* fix usort config

* remove custom categories sorting

* Run pre-commit without fixing flake8

* got a double import in merge

Co-authored-by: Nicolas Hug <nicolashug@fb.com>
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.

Use black?
7 participants