Skip to content

Conversation

izaitsevfb
Copy link
Contributor

Summary:
Adding usort linter to lintrunner to ensure the imports are sorted consistently. Using https://github.com/pytorch/torchrec as the reference for the linter integration.

Test Plan:
lintrunner init
lintrunner
lintrunner f
lintrunner -a
lintrunner torch/jit/_script.py torch/jit/_trace.py

Lintrunner results screenshot

Reviewers: osalpekar, suo
Subscribers: osalpekar, suo
Tasks: T123437457
Tags: lint, usort, bootcamp

Summary:
Adding [usort](https://pypi.org/project/usort/) linter to lintrunner to ensure the imports are sorted consistently. Using https://github.com/pytorch/torchrec as the reference for the linter integration.

Test Plan:
    lintrunner init
    lintrunner
    lintrunner f
    lintrunner -a
    lintrunner torch/jit/_script.py torch/jit/_trace.py

    <img width="930" alt="Lintrunner results screenshot" src="https://user-images.githubusercontent.com/108101595/175748204-0f675c99-6122-4ace-be1d-075ab9142a82.png">

Reviewers: osalpekar, suo
Subscribers: osalpekar, suo
Tasks: T123437457
Tags: lint, usort, bootcamp
@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Jun 24, 2022

🔗 Helpful links

❌ 2 New Failures

As of commit bf98801 (more details on the Dr. CI page):

Expand to see more
  • 2/2 failures introduced in this PR

🕵️ 2 new failures recognized by patterns

The following CI failures do not appear to be due to upstream breakages

See GitHub Actions build pull / win-vs2019-cpu-py3 / test (default, 1, 2, windows.4xlarge) (1/2)

Step: "Test" (full log | diagnosis details | 🔁 rerun)

2022-06-25T01:28:18.2669720Z RuntimeError: test_proxy_tensor failed!
2022-06-25T01:28:17.8688397Z FAILED (skipped=23, expected failures=29, unexpected successes=1)
2022-06-25T01:28:17.8688732Z 
2022-06-25T01:28:17.8688817Z Generating XML reports...
2022-06-25T01:28:17.8689162Z Generated XML report: test-reports\python-unittest\test_proxy_tensor\TEST-TestProxyTensorCPU-20220625012734.xml
2022-06-25T01:28:17.8689664Z Generated XML report: test-reports\python-unittest\test_proxy_tensor\TEST-TestProxyTensorOpInfoCPU-20220625012734.xml
2022-06-25T01:28:18.2659587Z Traceback (most recent call last):
2022-06-25T01:28:18.2668499Z   File "run_test.py", line 945, in <module>
2022-06-25T01:28:18.2668821Z     main()
2022-06-25T01:28:18.2669033Z   File "run_test.py", line 923, in main
2022-06-25T01:28:18.2669493Z     raise RuntimeError(err_message)
2022-06-25T01:28:18.2669720Z RuntimeError: test_proxy_tensor failed!
2022-06-25T01:28:18.5647919Z 
2022-06-25T01:28:18.5648381Z (base) C:\actions-runner\_work\pytorch\pytorch\test>if ERRORLEVEL 1 goto fail 
2022-06-25T01:28:18.5649603Z 
2022-06-25T01:28:18.5650139Z (base) C:\actions-runner\_work\pytorch\pytorch\test>exit /b 1 
2022-06-25T01:28:18.5708557Z ##[error]Process completed with exit code 1.
2022-06-25T01:28:18.5853151Z Prepare all required actions
2022-06-25T01:28:18.5853690Z Getting action download info
2022-06-25T01:28:18.7286194Z Download action repository 'nick-fields/retry@71062288b76e2b6214ebde0e673ce0de1755740a' (SHA:71062288b76e2b6214ebde0e673ce0de1755740a)
2022-06-25T01:28:18.9570265Z ##[group]Run ./.github/actions/get-workflow-job-id
2022-06-25T01:28:18.9570476Z with:

See GitHub Actions build Lint / lintrunner (2/2)

Step: "Run lintrunner on all files" (full log | diagnosis details | 🔁 rerun)

2022-06-25T00:02:34.3062539Z �[0m �[2m 7 8�[0m |�[1m�[2m �[0m�[2mexcept ModuleNotFoundError:
2022-06-25T00:02:34.3060884Z 
2022-06-25T00:02:34.3061052Z     �[2m     1�[0m |�[32m�[1m+�[0m�[32mimport collections
2022-06-25T00:02:34.3061222Z �[0m    �[2m     2�[0m |�[32m�[1m+�[0m�[32mimport functools
2022-06-25T00:02:34.3061363Z �[0m    �[2m     3�[0m |�[32m�[1m+�[0m�[32m
2022-06-25T00:02:34.3061521Z �[0m    �[2m 1   4�[0m |�[1m�[2m �[0m�[2mimport torch
2022-06-25T00:02:34.3061688Z �[0m    �[2m 1    �[0m |�[31m�[1m-�[0m�[31mimport functools
2022-06-25T00:02:34.3061860Z �[0m    �[2m 2    �[0m |�[31m�[1m-�[0m�[31mimport collections
2022-06-25T00:02:34.3062001Z �[0m    �[2m 4   5�[0m |�[1m�[2m �[0m�[2mtry:
2022-06-25T00:02:34.3062169Z �[0m    �[2m 5   6�[0m |�[1m�[2m �[0m�[2m    import numpy as np
2022-06-25T00:02:34.3062339Z �[0m    �[2m 6   7�[0m |�[1m�[2m �[0m�[2m    HAS_NUMPY = True
2022-06-25T00:02:34.3062539Z �[0m    �[2m 7   8�[0m |�[1m�[2m �[0m�[2mexcept ModuleNotFoundError:
2022-06-25T00:02:34.3062761Z �[0m    �[2m 8   9�[0m |�[1m�[2m �[0m�[2m    np = None  # type: ignore[assignment]
2022-06-25T00:02:34.3062945Z �[0m    �[2m    10�[0m |�[32m�[1m+�[0m�[32mfrom typing import Any
2022-06-25T00:02:34.3063197Z �[0m    �[2m    11�[0m |�[32m�[1m+�[0m�[32m
2022-06-25T00:02:34.3063410Z �[0m    �[2m 9  12�[0m |�[1m�[2m �[0m�[2mfrom torch._six import string_classes
2022-06-25T00:02:34.3063588Z �[0m    �[2m 9    �[0m |�[31m�[1m-�[0m�[31mfrom typing import Any
2022-06-25T00:02:34.3063714Z �[0m    �[2m11  13�[0m |�[1m�[2m �[0m�[2m
2022-06-25T00:02:34.3063849Z �[0m    �[2m12  14�[0m |�[1m�[2m �[0m�[2m
2022-06-25T00:02:34.3064086Z �[0m    �[2m13  15�[0m |�[1m�[2m �[0m�[2mclass autocast(torch.amp.autocast_mode.autocast):
2022-06-25T00:02:34.3064171Z �[0m
2022-06-25T00:02:34.3064177Z 

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@izaitsevfb izaitsevfb requested review from osalpekar and suo June 24, 2022 23:48
@izaitsevfb
Copy link
Contributor Author

usort found lots of existing issues in the codebase:
https://github.com/pytorch/pytorch/runs/7050073098?check_suite_focus=true#step:7:1

I'm not sure that reformatting the whole codebase is a good idea. Perhaps, it's possible to configure CI to run usort only on the the files affected by the PR.

is_formatter = true

[[linter]]
code = 'USORT'
Copy link
Member

Choose a reason for hiding this comment

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

my impression was that usort and black would fight with one another depending on which one would run first, hence why stuff like ufmt exists.

Can we do a similar thing? e.g. have a PYFMT linter that first runs usort, then runs black

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joshuadeng, you've replaced ufmt with lintrunner in torchrec, could you please provide any insight into whether there is any interference between black and usort when called from lintrunner?

Copy link
Member

Choose a reason for hiding this comment

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

I never implemented applying suggestions to the same file from two different linters: https://github.com/suo/lintrunner/blob/main/src/lib.rs#L55-L59

So it's probably better to combine them into a single linter, since I think quite frequently they will both suggest changes.

@suo
Copy link
Member

suo commented Jun 25, 2022

I'm not sure that reformatting the whole codebase is a good idea. Perhaps, it's possible to configure CI to run usort only on the the files affected by the PR.

We don't do this in general, as for some linters (e.g. mypy) it introduces inconsistency in the results between PR CI and trunk CI.

My recommendation would be to run it on the codebase and commit a big codemod PR after confirming that changes in import order didn't have weird side effects.

Copy link
Member

@suo suo left a comment

Choose a reason for hiding this comment

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

looks pretty good so far!



[tool.usort]
first_party_detection = false
Copy link
Member

Choose a reason for hiding this comment

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

curious: what motivated this config?

Copy link
Contributor Author

@izaitsevfb izaitsevfb Jun 25, 2022

Choose a reason for hiding this comment

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

I decided to add it, as both torchrec and torchvision use it, so I thought pytorch should follow their style for consistency.

Here is some information on what this setting does.

pytorchmergebot pushed a commit that referenced this pull request Jul 11, 2022
This reworks [80257](#80257) a bit to use ufmt:

* ufmt https://ufmt.omnilib.dev/en/stable/ unifies both black and usort to automatically format the file in the "most Pythonic" way
* Also make a demo run for all files in `tools/linter/**/*.py`

Pull Request resolved: #81157
Approved by: https://github.com/suo
facebook-github-bot pushed a commit that referenced this pull request Jul 12, 2022
Summary:
This reworks [80257](#80257) a bit to use ufmt:

* ufmt https://ufmt.omnilib.dev/en/stable/ unifies both black and usort to automatically format the file in the "most Pythonic" way
* Also make a demo run for all files in `tools/linter/**/*.py`

Pull Request resolved: #81157
Approved by: https://github.com/suo

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/ef035d083ea8ec1048f3f90562e9e761cc6a32ac

Reviewed By: DanilBaibak

Differential Revision: D37781945

Pulled By: huydhn

fbshipit-source-id: e5a0037f670a5276e3fc9eb22881fcf6b5d0063e
@github-actions
Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Aug 24, 2022
@github-actions github-actions bot closed this Sep 23, 2022
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.

3 participants