Skip to content

Commit 55250b3

Browse files
cyyeverpytorchmergebot
authored andcommittedDec 2, 2024
[1/N] Apply py39 ruff fixes (pytorch#138578)
Pull Request resolved: pytorch#138578 Approved by: https://github.com/Skylion007
1 parent b47bdb0 commit 55250b3

26 files changed

+118
-42
lines changed
 

‎mypy-strict.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# files.
77

88
[mypy]
9-
python_version = 3.8
9+
python_version = 3.9
1010
plugins = mypy_plugins/check_mypy_version.py, numpy.typing.mypy_plugin
1111

1212
cache_dir = .mypy_cache/strict

‎torchgen/api/autograd.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import re
44
from dataclasses import dataclass
5-
from typing import cast, Sequence
5+
from typing import cast, TYPE_CHECKING
66

77
from torchgen import local
88
from torchgen.api import cpp
@@ -20,6 +20,10 @@
2020
from torchgen.utils import IDENT_REGEX
2121

2222

23+
if TYPE_CHECKING:
24+
from collections.abc import Sequence
25+
26+
2327
# Represents a saved attribute involved in backward calculation.
2428
# Note that it can be a derived property of an input argument, e.g.:
2529
# we could save `other.scalar_type()` instead of the entire `other` tensor.

‎torchgen/api/cpp.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Sequence
3+
from typing import TYPE_CHECKING
44

55
from torchgen import local
66
from torchgen.api.types import (
@@ -51,6 +51,10 @@
5151
from torchgen.utils import assert_never
5252

5353

54+
if TYPE_CHECKING:
55+
from collections.abc import Sequence
56+
57+
5458
# This file describes the translation of JIT schema to the public C++
5559
# API, which is what people use when they call functions like at::add.
5660
#

‎torchgen/api/dispatcher.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import itertools
4-
from typing import Sequence
4+
from typing import TYPE_CHECKING
55

66
from torchgen.api import cpp
77
from torchgen.api.types import ArgName, Binding, CType, NamedCType
@@ -16,6 +16,10 @@
1616
from torchgen.utils import assert_never, concatMap
1717

1818

19+
if TYPE_CHECKING:
20+
from collections.abc import Sequence
21+
22+
1923
# This file describes the translation of JIT schema to the dispatcher
2024
# API, the *unboxed* calling convention by which invocations through
2125
# the dispatcher are made. Historically, the dispatcher API matched

‎torchgen/api/native.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Sequence
3+
from typing import TYPE_CHECKING
44

55
from torchgen import local
66
from torchgen.api import cpp
@@ -32,6 +32,10 @@
3232
from torchgen.utils import assert_never
3333

3434

35+
if TYPE_CHECKING:
36+
from collections.abc import Sequence
37+
38+
3539
# This file describes the translation of JIT schema to the native functions API.
3640
# This looks a lot like the C++ API (which makes historical sense, because the
3741
# idea was you wrote native functions to implement functions in the C++ API),

‎torchgen/api/python.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import Sequence
4+
from typing import TYPE_CHECKING
55

66
from torchgen.api import cpp
77
from torchgen.api.types import Binding, CppSignature, CppSignatureGroup
@@ -20,6 +20,10 @@
2020
)
2121

2222

23+
if TYPE_CHECKING:
24+
from collections.abc import Sequence
25+
26+
2327
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
2428
#
2529
# Data Models

‎torchgen/api/translate.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import NoReturn, Sequence
3+
from typing import NoReturn, TYPE_CHECKING
44

55
from torchgen.api.types import (
66
ArrayRefCType,
@@ -36,6 +36,10 @@
3636
)
3737

3838

39+
if TYPE_CHECKING:
40+
from collections.abc import Sequence
41+
42+
3943
# This file implements a small program synthesis engine that implements
4044
# conversions between one API to another.
4145
#

‎torchgen/api/types/signatures.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import Iterator, Sequence, TYPE_CHECKING
4+
from typing import TYPE_CHECKING
55

66
from torchgen.api.types.types_base import Binding, CType, Expr
77

88

99
if TYPE_CHECKING:
10+
from collections.abc import Iterator, Sequence
11+
1012
from torchgen.model import (
1113
BackendIndex,
1214
FunctionSchema,

‎torchgen/code_template.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

33
import re
4-
from typing import Mapping, Sequence
4+
from typing import TYPE_CHECKING
5+
6+
7+
if TYPE_CHECKING:
8+
from collections.abc import Mapping, Sequence
59

610

711
# match $identifier or ${identifier} and replace with value in env

‎torchgen/context.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import contextlib
44
import functools
5-
from typing import Any, Callable, Iterator, List, Optional, Tuple, TypeVar, Union
5+
from typing import Any, Callable, List, Optional, Tuple, TYPE_CHECKING, TypeVar, Union
66

77
import torchgen.local as local
88
from torchgen.model import (
@@ -15,6 +15,10 @@
1515
from torchgen.utils import context, S, T
1616

1717

18+
if TYPE_CHECKING:
19+
from collections.abc import Iterator
20+
21+
1822
# Helper functions for defining generators on things in the model
1923

2024
F = TypeVar(

‎torchgen/dest/ufunc.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import Sequence, TYPE_CHECKING
4+
from typing import TYPE_CHECKING
55

66
import torchgen.api.ufunc as ufunc
77
from torchgen.api.translate import translate
@@ -30,6 +30,8 @@
3030

3131

3232
if TYPE_CHECKING:
33+
from collections.abc import Sequence
34+
3335
from torchgen.api.ufunc import UfunctorBindings
3436

3537

‎torchgen/executorch/api/custom_ops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections import defaultdict
44
from dataclasses import dataclass
5-
from typing import Sequence, TYPE_CHECKING
5+
from typing import TYPE_CHECKING
66

77
from torchgen import dest
88

@@ -15,6 +15,8 @@
1515

1616

1717
if TYPE_CHECKING:
18+
from collections.abc import Sequence
19+
1820
from torchgen.executorch.model import ETKernelIndex
1921
from torchgen.selective_build.selector import SelectiveBuilder
2022

‎torchgen/executorch/api/et_cpp.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Sequence
3+
from typing import TYPE_CHECKING
44

55
from torchgen import local
66
from torchgen.api.types import (
@@ -40,6 +40,10 @@
4040
from torchgen.utils import assert_never
4141

4242

43+
if TYPE_CHECKING:
44+
from collections.abc import Sequence
45+
46+
4347
"""
4448
This file describes the translation of JIT schema to the public C++ API, which is what people use when they call
4549
functions like at::add. It also serves as a native function API, which is the signature of kernels,

‎torchgen/executorch/api/unboxing.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import Callable, Sequence, TYPE_CHECKING
4+
from typing import Callable, TYPE_CHECKING
55

66
from torchgen.model import (
77
Argument,
@@ -15,6 +15,8 @@
1515

1616

1717
if TYPE_CHECKING:
18+
from collections.abc import Sequence
19+
1820
from torchgen.api.types import Binding, CType, NamedCType
1921

2022

‎torchgen/gen.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import defaultdict, namedtuple, OrderedDict
99
from dataclasses import dataclass, field
1010
from pathlib import Path
11-
from typing import Any, Callable, Literal, Sequence, TypeVar
11+
from typing import Any, Callable, Literal, TYPE_CHECKING, TypeVar
1212

1313
import yaml
1414

@@ -96,6 +96,10 @@
9696
from torchgen.yaml_utils import YamlDumper, YamlLoader
9797

9898

99+
if TYPE_CHECKING:
100+
from collections.abc import Sequence
101+
102+
99103
T = TypeVar("T")
100104

101105
# Welcome to the ATen code generator v2! The ATen code generator is
@@ -229,7 +233,7 @@ def parse_tags_yaml_struct(es: object, path: str = "<stdin>") -> set[str]:
229233
return rs
230234

231235

232-
@functools.lru_cache(maxsize=None)
236+
@functools.cache
233237
def parse_tags_yaml(path: str) -> set[str]:
234238
global _GLOBAL_PARSE_TAGS_YAML_CACHE
235239
if path not in _GLOBAL_PARSE_TAGS_YAML_CACHE:

‎torchgen/gen_aoti_c_shim.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import textwrap
44
from dataclasses import dataclass
5-
from typing import Sequence
5+
from typing import TYPE_CHECKING
66

77
from torchgen.api.types import DispatcherSignature
88
from torchgen.api.types.signatures import CppSignature, CppSignatureGroup
@@ -24,6 +24,10 @@
2424
from torchgen.utils import mapMaybe
2525

2626

27+
if TYPE_CHECKING:
28+
from collections.abc import Sequence
29+
30+
2731
base_type_to_c_type = {
2832
BaseTy.Tensor: "AtenTensorHandle",
2933
BaseTy.bool: "int32_t", # Use int to pass bool
@@ -114,14 +118,14 @@ def convert_arg_type_and_name( # type: ignore[return]
114118
new_aten_types.append(f"::std::optional<{aten_type}>")
115119
base_type = aten_type[len("c10::ArrayRef<") : -1]
116120
new_callsite_exprs.append(
117-
f"pointer_to_optional_list<{base_type}>({names[j]}, {names[j+1]})"
121+
f"pointer_to_optional_list<{base_type}>({names[j]}, {names[j + 1]})"
118122
)
119123
j += 2
120124
elif aten_type == "c10::Device":
121125
# Device is passed as device_type + device_index
122126
new_aten_types.append("::std::optional<c10::Device>")
123127
new_callsite_exprs.append(
124-
f"pointer_to_optional_device({names[j]}, {names[j+1]})"
128+
f"pointer_to_optional_device({names[j]}, {names[j + 1]})"
125129
)
126130
j += 2
127131
else:

‎torchgen/gen_backend_stubs.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
from collections import Counter, defaultdict, namedtuple
77
from pathlib import Path
8-
from typing import Sequence
8+
from typing import TYPE_CHECKING
99

1010
import yaml
1111

@@ -28,6 +28,10 @@
2828
from torchgen.yaml_utils import YamlLoader
2929

3030

31+
if TYPE_CHECKING:
32+
from collections.abc import Sequence
33+
34+
3135
# Parses the external backend's yaml, and adds a new BackendIndex for the backend's dispatch key.
3236
# Returns a Tuple of (backend_key, autograd_key, cpp_namespace, updated BackendIndex mapping)
3337
ParsedExternalYaml = namedtuple(

‎torchgen/gen_executorch.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import defaultdict
66
from dataclasses import dataclass
77
from pathlib import Path
8-
from typing import Any, Callable, Sequence, TextIO, TYPE_CHECKING
8+
from typing import Any, Callable, TextIO, TYPE_CHECKING
99

1010
import yaml
1111

@@ -57,6 +57,8 @@
5757

5858

5959
if TYPE_CHECKING:
60+
from collections.abc import Sequence
61+
6062
from torchgen.selective_build.selector import SelectiveBuilder
6163

6264

‎torchgen/gen_lazy_tensor.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
from collections import namedtuple
66
from pathlib import Path
7-
from typing import Any, Callable, Iterable, Iterator, Sequence
7+
from typing import Any, Callable, TYPE_CHECKING
88

99
import yaml
1010

@@ -25,6 +25,10 @@
2525
from torchgen.yaml_utils import YamlLoader
2626

2727

28+
if TYPE_CHECKING:
29+
from collections.abc import Iterable, Iterator, Sequence
30+
31+
2832
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
2933
#
3034
# Lazy Tensor Codegen

‎torchgen/gen_vmap_plumbing.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import textwrap
44
from dataclasses import dataclass
5-
from typing import Sequence
5+
from typing import TYPE_CHECKING
66

77
from torchgen.api.translate import translate
88
from torchgen.api.types import DispatcherSignature
@@ -22,6 +22,10 @@
2222
from torchgen.utils import mapMaybe
2323

2424

25+
if TYPE_CHECKING:
26+
from collections.abc import Sequence
27+
28+
2529
def is_tensor(typ: Type) -> bool:
2630
return isinstance(typ, BaseType) and typ.name == BaseTy.Tensor
2731

@@ -111,7 +115,7 @@ def gen_returns(
111115
idx += 2
112116
elif is_tensor_list(ret.type):
113117
wrapped_returns.append(
114-
f"makeBatchedVector(std::get<{idx}>({results_var}), std::get<{idx+1}>({results_var}), {cur_level_var})"
118+
f"makeBatchedVector(std::get<{idx}>({results_var}), std::get<{idx + 1}>({results_var}), {cur_level_var})"
115119
)
116120
idx += 2
117121
else:

‎torchgen/local.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import threading
44
from contextlib import contextmanager
5-
from typing import Iterator
5+
from typing import TYPE_CHECKING
6+
7+
8+
if TYPE_CHECKING:
9+
from collections.abc import Iterator
610

711

812
# Simple dynamic scoping implementation. The name "parametrize" comes

‎torchgen/model.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
import re
66
from dataclasses import dataclass
77
from enum import auto, Enum
8-
from typing import Callable, Iterator, List, Sequence
8+
from typing import Callable, List, TYPE_CHECKING
99

1010
from torchgen.utils import assert_never, NamespaceHelper, OrderedSet
1111

1212

13+
if TYPE_CHECKING:
14+
from collections.abc import Iterator, Sequence
15+
16+
1317
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
1418
#
1519
# DATA MODEL

‎torchgen/native_function_generation.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import string
44
from collections import defaultdict
5-
from typing import Sequence
5+
from typing import TYPE_CHECKING
66

77
import torchgen.api.dispatcher as dispatcher
88
from torchgen.api.translate import translate
@@ -30,6 +30,10 @@
3030
from torchgen.utils import concatMap
3131

3232

33+
if TYPE_CHECKING:
34+
from collections.abc import Sequence
35+
36+
3337
# See Note: [Out ops with functional variants that don't get grouped properly]
3438
OUT_OPS_THAT_DONT_GET_GROUPED_PROPERLY = [
3539
# This has a functional variant, but it's currently marked private.

‎torchgen/static_runtime/gen_static_runtime_ops.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import itertools
55
import os
6-
from typing import Sequence, TypeVar, Union
6+
from typing import TYPE_CHECKING, TypeVar, Union
77

88
from libfb.py.log import set_simple_logging # type: ignore[import]
99

@@ -13,6 +13,10 @@
1313
from torchgen.static_runtime import config, generator
1414

1515

16+
if TYPE_CHECKING:
17+
from collections.abc import Sequence
18+
19+
1620
# Given a list of `grouped_native_functions` sorted by their op names, return a list of
1721
# lists each of which groups ops that share the base name. For example, `mean` and
1822
# `mean.dim` are grouped together by this function.

‎torchgen/static_runtime/generator.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import logging
55
import math
6-
from typing import Sequence
6+
from typing import TYPE_CHECKING
77

88
import torchgen.api.cpp as cpp
99
from torchgen.context import native_function_manager
@@ -23,6 +23,10 @@
2323
from torchgen.static_runtime import config
2424

2525

26+
if TYPE_CHECKING:
27+
from collections.abc import Sequence
28+
29+
2630
logger: logging.Logger = logging.getLogger()
2731

2832

‎torchgen/utils.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,15 @@
1010
from dataclasses import fields, is_dataclass
1111
from enum import auto, Enum
1212
from pathlib import Path
13-
from typing import (
14-
Any,
15-
Callable,
16-
Generic,
17-
Iterable,
18-
Iterator,
19-
Literal,
20-
NoReturn,
21-
Sequence,
22-
TYPE_CHECKING,
23-
TypeVar,
24-
)
13+
from typing import Any, Callable, Generic, Literal, NoReturn, TYPE_CHECKING, TypeVar
2514
from typing_extensions import Self
2615

2716
from torchgen.code_template import CodeTemplate
2817

2918

3019
if TYPE_CHECKING:
3120
from argparse import Namespace
21+
from collections.abc import Iterable, Iterator, Sequence
3222

3323

3424
REPO_ROOT = Path(__file__).absolute().parent.parent
@@ -113,7 +103,7 @@ def assert_never(x: NoReturn) -> NoReturn:
113103
raise AssertionError(f"Unhandled type: {type(x).__name__}")
114104

115105

116-
@functools.lru_cache(maxsize=None)
106+
@functools.cache
117107
def _read_template(template_fn: str) -> CodeTemplate:
118108
return CodeTemplate.from_file(template_fn)
119109

0 commit comments

Comments
 (0)
Please sign in to comment.