[BE] Apply ufmt to run_test and GitHub Python util scripts (#97588)

This has been bugging me for a while as I'm working on these Python scripts and they are not tracked by ufmt linter.  So I add these script into that linter.

```
[[linter]]
code = 'UFMT'
include_patterns = [
    '.github/**/*.py',
    'test/run_test.py',
```

This change should just work and not break anything as ufmt (black + usort) linter is very safe to use for standalone util scripts.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/97588
Approved by: https://github.com/kit1980
This commit is contained in:
Huy Do
2023-03-26 04:52:55 +00:00
committed by PyTorch MergeBot
parent f09347a9f1
commit 4c0dce50fd
30 changed files with 1955 additions and 1070 deletions

View File

@ -1,7 +1,14 @@
#!/usr/bin/env python3
from gitutils import PeekableIterator, patterns_to_regex, GitRepo, are_ghstack_branches_in_sync, _shasum
from unittest import TestCase, main, SkipTest
from pathlib import Path
from unittest import main, SkipTest, TestCase
from gitutils import (
_shasum,
are_ghstack_branches_in_sync,
GitRepo,
patterns_to_regex,
PeekableIterator,
)
BASE_DIR = Path(__file__).parent
@ -15,6 +22,7 @@ class TestPeekableIterator(TestCase):
def test_is_iterable(self) -> None:
from collections.abc import Iterator
iter_ = PeekableIterator("")
self.assertTrue(isinstance(iter_, Iterator))
@ -35,7 +43,8 @@ class TestPattern(TestCase):
patterns_re = patterns_to_regex(allowed_patterns)
fnames = [
"aten/src/ATen/native/LinearAlgebra.cpp",
"aten/src/ATen/native/cpu/LinearAlgebraKernel.cpp"]
"aten/src/ATen/native/cpu/LinearAlgebraKernel.cpp",
]
for filename in fnames:
self.assertTrue(patterns_re.match(filename))
@ -44,11 +53,13 @@ class TestGitRepo(TestCase):
def setUp(self) -> None:
repo_dir = BASE_DIR.parent.parent.absolute()
if not (repo_dir / ".git").is_dir():
raise SkipTest("Can't find git directory, make sure to run this test on real repo checkout")
raise SkipTest(
"Can't find git directory, make sure to run this test on real repo checkout"
)
self.repo = GitRepo(str(repo_dir))
def _skip_if_ref_does_not_exist(self, ref: str) -> None:
""" Skip test if ref is missing as stale branches are deleted with time """
"""Skip test if ref is missing as stale branches are deleted with time"""
try:
self.repo.show_ref(ref)
except RuntimeError as e:
@ -69,5 +80,6 @@ class TestGitRepo(TestCase):
self._skip_if_ref_does_not_exist(head_ref)
self.assertFalse(are_ghstack_branches_in_sync(self.repo, head_ref))
if __name__ == '__main__':
if __name__ == "__main__":
main()