Skip to content

Commit 347b036

Browse files
huydhnpytorchmergebot
authored andcommitted
Apply ufmt linter to all py files under tools (pytorch#81285)
With ufmt in place pytorch#81157, we can now use it to gradually format all files. I'm breaking this down into multiple smaller batches to avoid too many merge conflicts later on. This batch (as copied from the current BLACK linter config): * `tools/**/*.py` Upcoming batchs: * `torchgen/**/*.py` * `torch/package/**/*.py` * `torch/onnx/**/*.py` * `torch/_refs/**/*.py` * `torch/_prims/**/*.py` * `torch/_meta_registrations.py` * `torch/_decomp/**/*.py` * `test/onnx/**/*.py` Once they are all formatted, BLACK linter will be removed. Pull Request resolved: pytorch#81285 Approved by: https://github.com/suo
1 parent d3acbc8 commit 347b036

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+344
-317
lines changed

.lintrunner.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -661,8 +661,11 @@ init_command = [
661661
code = 'UFMT'
662662
# This is a big bang change to reformat every python files in the repo,
663663
# so it's arguably safer to gradually reformat them in subsequent PR.
664-
# After that, we can use **/*.py to apply the formatter to everything
665-
include_patterns = ['tools/linter/**/*.py']
664+
# After that, we can use **/*.py to apply the formatter to all entries
665+
# currently enforced by BLACK linter
666+
include_patterns = [
667+
'tools/**/*.py',
668+
]
666669
command = [
667670
'python3',
668671
'tools/linter/adapters/ufmt_linter.py',

tools/amd_build/build_amd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22

33

4-
import os
54
import argparse
5+
import os
66
import sys
77

88
sys.path.append(

tools/autograd/context.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import functools
2+
from typing import Callable
3+
14
from torchgen.api.autograd import NativeFunctionWithDifferentiabilityInfo as NFWDI
25
from torchgen.context import native_function_manager
36
from torchgen.utils import T
47

5-
import functools
6-
from typing import Callable
7-
88
# Like tools.api.context.with_native_function, but for
99
# NativeFunctionWithDifferentiabilityInfo.
1010
def with_native_function_with_differentiability_info(

tools/autograd/gen_annotated_fn_args.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@
1414
torch/testing/_internal/generated
1515
"""
1616

17-
from collections import defaultdict
1817
import argparse
1918
import os
2019
import textwrap
20+
from collections import defaultdict
2121

22-
from typing import Dict, List, Any
22+
from typing import Any, Dict, List
2323

24-
from torchgen.gen import parse_native_yaml
25-
from torchgen.utils import FileManager
24+
import torchgen.api.python as python
2625
from torchgen.context import with_native_function
26+
27+
from torchgen.gen import parse_native_yaml
2728
from torchgen.model import BaseOperatorName, NativeFunction
28-
import torchgen.api.python as python
29+
from torchgen.utils import FileManager
30+
2931
from .gen_python_functions import (
30-
should_generate_py_binding,
31-
is_py_torch_function,
32-
is_py_nn_function,
32+
is_py_fft_function,
3333
is_py_linalg_function,
34-
is_py_variable_method,
34+
is_py_nn_function,
3535
is_py_special_function,
36-
is_py_fft_function,
36+
is_py_torch_function,
37+
is_py_variable_method,
38+
should_generate_py_binding,
3739
)
3840

3941

tools/autograd/gen_autograd.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@
2525

2626
import argparse
2727
import os
28+
from typing import List
29+
2830
from torchgen.api import cpp
2931
from torchgen.api.autograd import (
3032
match_differentiability_info,
3133
NativeFunctionWithDifferentiabilityInfo,
3234
)
3335
from torchgen.gen import parse_native_yaml
3436
from torchgen.selective_build.selector import SelectiveBuilder
35-
from typing import List
37+
3638
from . import gen_python_functions
3739
from .gen_autograd_functions import (
3840
gen_autograd_functions_lib,
3941
gen_autograd_functions_python,
4042
)
41-
from .gen_trace_type import gen_trace_type
42-
from .gen_variable_type import gen_variable_type
4343
from .gen_inplace_or_view_type import gen_inplace_or_view_type
44+
from .gen_trace_type import gen_trace_type
4445
from .gen_variable_factories import gen_variable_factories
46+
from .gen_variable_type import gen_variable_type
4547
from .load_derivatives import load_derivatives
4648

4749

tools/autograd/gen_autograd_functions.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
# Functions.h/cpp: subclasses of autograd::Node
55
# python_functions.h/cpp: Python bindings for the above classes
66
#
7-
from .gen_inplace_or_view_type import VIEW_FUNCTIONS
8-
97
from typing import List, Sequence, Tuple
108

119
from torchgen.api.autograd import (
@@ -16,25 +14,27 @@
1614
uses_single_grad,
1715
)
1816
from torchgen.api.types import (
19-
Binding,
17+
ArrayRefCType,
2018
BaseCType,
21-
OptionalCType,
22-
tensorT,
23-
longT,
24-
doubleT,
25-
scalarT,
26-
stringT,
19+
Binding,
2720
boolT,
21+
doubleT,
2822
intArrayRefT,
29-
tensorListT,
30-
MutRefCType,
3123
ListCType,
32-
ArrayRefCType,
24+
longT,
25+
MutRefCType,
26+
OptionalCType,
3327
optionalIntArrayRefT,
28+
scalarT,
29+
stringT,
30+
tensorListT,
31+
tensorT,
3432
)
3533
from torchgen.code_template import CodeTemplate
36-
from torchgen.utils import FileManager
3734
from torchgen.model import Argument
35+
from torchgen.utils import FileManager
36+
37+
from .gen_inplace_or_view_type import VIEW_FUNCTIONS
3838

3939
FUNCTION_DECLARATION = CodeTemplate(
4040
"""\

tools/autograd/gen_inplace_or_view_type.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,42 @@
44
# if updates are needed in torch/csrc/autograd/autograd_not_implemented_fallback.cpp
55
# The fallback is expected to mimick this codegen, so we should keep the two in sync.
66

7+
from typing import Dict, List, Optional, Sequence, Tuple
8+
79
from torchgen.api import cpp
810
from torchgen.api.autograd import (
9-
NativeFunctionWithDifferentiabilityInfo,
10-
gen_differentiable_outputs,
1111
dispatch_strategy,
12+
gen_differentiable_outputs,
13+
NativeFunctionWithDifferentiabilityInfo,
1214
)
1315
from torchgen.api.types import (
14-
Binding,
15-
DispatcherSignature,
16-
CType,
1716
BaseCType,
18-
OptionalCType,
19-
longT,
17+
Binding,
2018
boolT,
19+
CType,
20+
DispatcherSignature,
2121
intArrayRefT,
22+
longT,
23+
OptionalCType,
2224
symIntArrayRefT,
2325
)
2426
from torchgen.code_template import CodeTemplate
2527
from torchgen.context import with_native_function
2628
from torchgen.model import (
27-
Type,
2829
NativeFunction,
30+
SchemaKind,
2931
SelfArgument,
3032
TensorOptionsArguments,
31-
SchemaKind,
33+
Type,
3234
)
33-
from typing import List, Optional, Sequence, Tuple, Dict
3435
from torchgen.utils import FileManager
36+
3537
from .context import with_native_function_with_differentiability_info
3638
from .gen_trace_type import (
39+
get_return_value,
3740
MANUAL_AUTOGRAD,
38-
type_wrapper_name,
3941
tie_return_values,
40-
get_return_value,
42+
type_wrapper_name,
4143
)
4244

4345
# See NOTE [ Autograd View Variables ] in variable.h for details.

tools/autograd/gen_python_functions.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,15 @@
3030
# message, but use what's there
3131
#
3232

33-
from collections import defaultdict
3433
import itertools
3534
import re
36-
import yaml
35+
from collections import defaultdict
3736

38-
from .gen_trace_type import should_trace
37+
from typing import Callable, Dict, List, Optional, Sequence, Set, Tuple
3938

40-
from torchgen.code_template import CodeTemplate
39+
import yaml
4140
from torchgen.api import cpp
42-
from torchgen.api.types import CppSignatureGroup
4341
from torchgen.api.python import (
44-
PythonArgument,
45-
PythonSignature,
46-
PythonSignatureDeprecated,
47-
PythonSignatureGroup,
48-
PythonSignatureNativeFunctionPair,
4942
arg_parser_output_exprs,
5043
argument_type_str,
5144
cpp_dispatch_exprs,
@@ -55,20 +48,22 @@
5548
dispatch_lambda_return_str,
5649
has_tensor_options,
5750
namedtuple_fieldnames,
51+
PythonArgument,
52+
PythonSignature,
53+
PythonSignatureDeprecated,
54+
PythonSignatureGroup,
55+
PythonSignatureNativeFunctionPair,
5856
signature,
5957
)
60-
from torchgen.gen import cpp_string, parse_native_yaml, parse_tags_yaml
58+
from torchgen.api.types import CppSignatureGroup
59+
60+
from torchgen.code_template import CodeTemplate
6161
from torchgen.context import with_native_function
62-
from torchgen.model import (
63-
Argument,
64-
BaseOperatorName,
65-
NativeFunction,
66-
Type,
67-
Variant,
68-
)
69-
from torchgen.utils import split_name_params, YamlLoader, FileManager
62+
from torchgen.gen import cpp_string, parse_native_yaml, parse_tags_yaml
63+
from torchgen.model import Argument, BaseOperatorName, NativeFunction, Type, Variant
64+
from torchgen.utils import FileManager, split_name_params, YamlLoader
7065

71-
from typing import Dict, Optional, List, Tuple, Set, Sequence, Callable
66+
from .gen_trace_type import should_trace
7267

7368
#
7469
# declarations blocklist

tools/autograd/gen_trace_type.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import itertools
2-
from typing import List, Sequence, Union, Dict
2+
from typing import Dict, List, Sequence, Union
33

4-
from torchgen.api.types import DispatcherSignature
54
from torchgen.api import cpp
5+
6+
from torchgen.api.types import DispatcherSignature
67
from torchgen.code_template import CodeTemplate
78
from torchgen.context import with_native_function
9+
from torchgen.model import Argument, NativeFunction, SchemaKind, TensorOptionsArguments
810
from torchgen.utils import FileManager
9-
from torchgen.model import (
10-
Argument,
11-
NativeFunction,
12-
SchemaKind,
13-
TensorOptionsArguments,
14-
)
1511

1612
# Note [Manual Backend kernels]
1713
# For these ops, we want to manually register to dispatch key Backend and

tools/autograd/gen_variable_factories.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
# This writes one file: variable_factories.h
44

55
import re
6-
from typing import Optional, List
6+
from typing import List, Optional
77

8-
from torchgen.api.types import CppSignatureGroup
9-
from torchgen.api import cpp
108
import torchgen.api.python as python
11-
from torchgen.gen import parse_native_yaml
9+
from torchgen.api import cpp
10+
11+
from torchgen.api.types import CppSignatureGroup
1212
from torchgen.context import with_native_function
13-
from torchgen.utils import mapMaybe, FileManager
13+
from torchgen.gen import parse_native_yaml
1414
from torchgen.model import NativeFunction, TensorOptionsArguments, Variant
15+
from torchgen.utils import FileManager, mapMaybe
1516

1617
OPTIONAL_TYPE_PATTERN = re.compile(r"c10::optional<(.+)>")
1718
TYPE_PATTERN = re.compile(r"(?:const\s+)?([A-Z]\w+)")

0 commit comments

Comments
 (0)