Skip to content

Commit d2dc173

Browse files
XuehaiPanpytorchmergebot
authored andcommitted
Remove lint dependency ufmt (pytorch#132573)
`ufmt` is a combination of `black + usort`. This PR removes `ufmt` and run `black` and `usort` separately. Pull Request resolved: pytorch#132573 Approved by: https://github.com/ezyang ghstack dependencies: pytorch#129769, pytorch#132572
1 parent f7aeb39 commit d2dc173

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

.lintrunner.toml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,16 @@ init_command = [
10081008
'PyYAML==6.0.1',
10091009
]
10101010

1011-
# Black + usort
1011+
# usort + black
10121012
[[linter]]
1013-
code = 'UFMT'
1013+
code = 'PYFMT'
10141014
include_patterns = [
10151015
'**/*.py',
10161016
'**/*.pyi',
10171017
]
10181018
command = [
10191019
'python3',
1020-
'tools/linter/adapters/ufmt_linter.py',
1020+
'tools/linter/adapters/pyfmt_linter.py',
10211021
'--',
10221022
'@{{PATHSFILE}}'
10231023
]
@@ -1475,7 +1475,6 @@ init_command = [
14751475
'--dry-run={{DRYRUN}}',
14761476
'--no-black-binary',
14771477
'black==23.12.1',
1478-
'ufmt==2.7.0',
14791478
'usort==1.0.8.post1',
14801479
'isort==5.13.2',
14811480
]

.vscode/extensions.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"recommendations": [
33
"ms-python.python",
4-
"omnilib.ufmt"
54
]
65
}

.vscode/settings_recommended.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
},
55
"files.associations": {
66
"*.py.in": "python",
7-
"*.pyi.in": "python",
8-
"editor.defaultFormatter": "omnilib.ufmt"
7+
"*.pyi.in": "python"
98
},
109
"files.eol": "\n",
1110
"files.insertFinalNewline": true,
1211
"files.trimFinalNewlines": true,
1312
"files.trimTrailingWhitespace": true,
14-
"python.formatting.provider": "none",
1513
"python.linting.enabled": true,
1614
"python.linting.flake8Enabled": true
1715
}

tools/linter/adapters/ufmt_linter.py tools/linter/adapters/pyfmt_linter.py

+48-35
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
from pathlib import Path
1212
from typing import Any, NamedTuple
1313

14+
import black
1415
import isort
15-
from ufmt.core import ufmt_string
16-
from ufmt.util import make_black_config
17-
from usort import Config as UsortConfig
16+
import usort
1817

1918

2019
IS_WINDOWS: bool = os.name == "nt"
@@ -53,7 +52,7 @@ def format_error_message(filename: str, err: Exception) -> LintMessage:
5352
path=filename,
5453
line=None,
5554
char=None,
56-
code="UFMT",
55+
code="PYFMT",
5756
severity=LintSeverity.ADVICE,
5857
name="command-failed",
5958
original=None,
@@ -62,39 +61,53 @@ def format_error_message(filename: str, err: Exception) -> LintMessage:
6261
)
6362

6463

64+
def run_isort(content: str, path: Path) -> str:
65+
isort_config = isort.Config(settings_path=str(REPO_ROOT))
66+
67+
is_this_file = path.samefile(__file__)
68+
if not is_this_file:
69+
content = re.sub(r"(#.*\b)usort:\s*skip\b", r"\g<1>isort: split", content)
70+
71+
content = isort.code(content, config=isort_config, file_path=path)
72+
73+
if not is_this_file:
74+
content = re.sub(r"(#.*\b)isort: split\b", r"\g<1>usort: skip", content)
75+
76+
return content
77+
78+
79+
def run_usort(content: str, path: Path) -> str:
80+
usort_config = usort.Config.find(path)
81+
82+
return usort.usort_string(content, path=path, config=usort_config)
83+
84+
85+
def run_black(content: str, path: Path) -> str:
86+
black_config = black.parse_pyproject_toml(black.find_pyproject_toml((str(path),))) # type: ignore[attr-defined,arg-type]
87+
# manually patch options that do not have a 1-to-1 match in Mode arguments
88+
black_config["target_versions"] = {
89+
black.TargetVersion[ver.upper()] # type: ignore[attr-defined]
90+
for ver in black_config.pop("target_version", [])
91+
}
92+
black_config["string_normalization"] = not black_config.pop(
93+
"skip_string_normalization", False
94+
)
95+
black_mode = black.Mode(**black_config)
96+
black_mode.is_pyi = path.suffix.lower() == ".pyi"
97+
black_mode.is_ipynb = path.suffix.lower() == ".ipynb"
98+
99+
return black.format_str(content, mode=black_mode)
100+
101+
65102
def check_file(filename: str) -> list[LintMessage]:
66103
path = Path(filename).absolute()
67-
original = path.read_text(encoding="utf-8")
104+
original = replacement = path.read_text(encoding="utf-8")
68105

69106
try:
70-
isort_config = isort.Config(settings_path=str(REPO_ROOT))
71-
usort_config = UsortConfig.find(path)
72-
black_config = make_black_config(path)
73-
74-
if not path.samefile(__file__):
75-
isorted_replacement = re.sub(
76-
r"(#.*\b)isort: split\b",
77-
r"\g<1>usort: skip",
78-
isort.code(
79-
re.sub(r"(#.*\b)usort:\s*skip\b", r"\g<1>isort: split", original),
80-
config=isort_config,
81-
file_path=path,
82-
),
83-
)
84-
else:
85-
isorted_replacement = isort.code(
86-
original,
87-
config=isort_config,
88-
file_path=path,
89-
)
90-
91-
# Use UFMT API to call both usort and black
92-
replacement = ufmt_string(
93-
path=path,
94-
content=isorted_replacement,
95-
usort_config=usort_config,
96-
black_config=black_config,
97-
)
107+
# NB: run isort first to enforce style for blank lines
108+
replacement = run_isort(replacement, path=path)
109+
replacement = run_usort(replacement, path=path)
110+
replacement = run_black(replacement, path=path)
98111

99112
if original == replacement:
100113
return []
@@ -104,7 +117,7 @@ def check_file(filename: str) -> list[LintMessage]:
104117
path=filename,
105118
line=None,
106119
char=None,
107-
code="UFMT",
120+
code="PYFMT",
108121
severity=LintSeverity.WARNING,
109122
name="format",
110123
original=original,
@@ -118,7 +131,7 @@ def check_file(filename: str) -> list[LintMessage]:
118131

119132
def main() -> None:
120133
parser = argparse.ArgumentParser(
121-
description="Format files with ufmt (black + usort).",
134+
description="Format files with usort + black.",
122135
fromfile_prefix_chars="@",
123136
)
124137
parser.add_argument(

torch/nested/_internal/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ def sum_dim_IntList(func, *args, **kwargs):
963963
new_kwargs["dim"],
964964
reduce_on_batch,
965965
reduce_on_ragged,
966-
reduce_on_non_batch, # noqa: UFMT
966+
reduce_on_non_batch,
967967
) = _wrap_jagged_dims(
968968
inp.dim(),
969969
new_kwargs["dim"],
@@ -1325,7 +1325,7 @@ def mean_dim(func, *args, **kwargs):
13251325
new_kwargs["dim"],
13261326
reduce_on_batch,
13271327
reduce_on_ragged,
1328-
reduce_on_non_batch, # noqa: UFMT
1328+
reduce_on_non_batch,
13291329
) = _wrap_jagged_dims(
13301330
inp.dim(),
13311331
new_kwargs["dim"],

0 commit comments

Comments
 (0)