Skip to content

Commit 14d87bb

Browse files
justinchubypytorchmergebot
authored andcommitted
[BE] Enable ruff's UP rules and autoformat tools and scripts (pytorch#105428)
Pull Request resolved: pytorch#105428 Approved by: https://github.com/albanD, https://github.com/soulitzer, https://github.com/malfet
1 parent 5666d20 commit 14d87bb

Some content is hidden

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

48 files changed

+125
-146
lines changed

.ci/pytorch/create_test_cert.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def sign_certificate_request(path, csr_cert, ca_cert, private_ca_key):
8888

8989

9090
ca_key = genrsa(temp_dir + "/ca.key")
91-
ca_cert = create_cert(temp_dir + "/ca.pem", u"US", u"New York", u"New York", u"Gloo Certificate Authority", ca_key)
91+
ca_cert = create_cert(temp_dir + "/ca.pem", "US", "New York", "New York", "Gloo Certificate Authority", ca_key)
9292

9393
pkey = genrsa(temp_dir + "/pkey.key")
94-
csr = create_req(temp_dir + "/csr.csr", u"US", u"California", u"San Francisco", u"Gloo Testing Company", pkey)
94+
csr = create_req(temp_dir + "/csr.csr", "US", "California", "San Francisco", "Gloo Testing Company", pkey)
9595

9696
cert = sign_certificate_request(temp_dir + "/cert.pem", csr, ca_cert, ca_key)

.ci/pytorch/perf_test/compare_with_baseline.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
elif 'gpu' in test_name:
2020
backend = 'gpu'
2121

22-
data_file_path = '../{}_runtime.json'.format(backend)
22+
data_file_path = f'../{backend}_runtime.json'
2323

2424
with open(data_file_path) as data_file:
2525
data = json.load(data_file)
@@ -69,7 +69,7 @@
6969
print("z-value < 3, no perf regression detected.")
7070
if args.update:
7171
print("We will use these numbers as new baseline.")
72-
new_data_file_path = '../new_{}_runtime.json'.format(backend)
72+
new_data_file_path = f'../new_{backend}_runtime.json'
7373
with open(new_data_file_path) as new_data_file:
7474
new_data = json.load(new_data_file)
7575
new_data[test_name] = {}

.circleci/cimodel/data/binary_build_definitions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import cimodel.lib.conf_tree as conf_tree
66
import cimodel.lib.miniutils as miniutils
77

8-
class Conf(object):
8+
class Conf:
99
def __init__(self, os, gpu_version, pydistro, parms, smoke, libtorch_variant, gcc_config_variant, libtorch_config_variant):
1010

1111
self.os = os

.circleci/cimodel/data/pytorch_build_definitions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def gen_workflow_job(self, phase):
143143

144144

145145
# TODO This is a hack to special case some configs just for the workflow list
146-
class HiddenConf(object):
146+
class HiddenConf:
147147
def __init__(self, name, parent_build=None, filters=None):
148148
self.name = name
149149
self.parent_build = parent_build
@@ -160,7 +160,7 @@ def gen_workflow_job(self, phase):
160160
def gen_build_name(self, _):
161161
return self.name
162162

163-
class DocPushConf(object):
163+
class DocPushConf:
164164
def __init__(self, name, parent_build=None, branch="master"):
165165
self.name = name
166166
self.parent_build = parent_build

.circleci/generate_config_yml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import cimodel.lib.miniyaml as miniyaml
1919

2020

21-
class File(object):
21+
class File:
2222
"""
2323
Verbatim copy the contents of a file into config.yml
2424
"""
@@ -57,7 +57,7 @@ def horizontal_rule():
5757
return "".join("#" * 78)
5858

5959

60-
class Header(object):
60+
class Header:
6161
def __init__(self, title, summary=None):
6262
self.title = title
6363
self.summary_lines = summary or []

.github/scripts/ensure_actions_will_cancel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
def should_check(filename: Path) -> bool:
20-
with open(filename, "r") as f:
20+
with open(filename) as f:
2121
content = f.read()
2222

2323
data = yaml.safe_load(content)
@@ -37,7 +37,7 @@ def should_check(filename: Path) -> bool:
3737
files = [f for f in files if should_check(f)]
3838
names = set()
3939
for filename in files:
40-
with open(filename, "r") as f:
40+
with open(filename) as f:
4141
data = yaml.safe_load(f)
4242

4343
name = data.get("name")

.github/scripts/file_io_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def load_json_file(file_path: Path) -> Any:
4444
"""
4545
Returns the deserialized json object
4646
"""
47-
with open(file_path, "r") as f:
47+
with open(file_path) as f:
4848
return json.load(f)
4949

5050

.github/scripts/filter_test_configs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def process_jobs(
319319
try:
320320
# The job name from github is in the PLATFORM / JOB (CONFIG) format, so breaking
321321
# it into its two components first
322-
current_platform, _ = [n.strip() for n in job_name.split(JOB_NAME_SEP, 1) if n]
322+
current_platform, _ = (n.strip() for n in job_name.split(JOB_NAME_SEP, 1) if n)
323323
except ValueError as error:
324324
warnings.warn(f"Invalid job name {job_name}, returning")
325325
return test_matrix

.github/scripts/generate_pytorch_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_tag() -> str:
5050

5151
def get_base_version() -> str:
5252
root = get_pytorch_root()
53-
dirty_version = open(root / "version.txt", "r").read().strip()
53+
dirty_version = open(root / "version.txt").read().strip()
5454
# Strips trailing a0 from version.txt, not too sure why it's there in the
5555
# first place
5656
return re.sub(LEGACY_BASE_VERSION_SUFFIX_PATTERN, "", dirty_version)

.github/scripts/label_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_last_page_num_from_header(header: Any) -> int:
5151
)
5252

5353

54-
@lru_cache()
54+
@lru_cache
5555
def gh_get_labels(org: str, repo: str) -> List[str]:
5656
prefix = f"https://api.github.com/repos/{org}/{repo}/labels?per_page=100"
5757
header, info = request_for_labels(prefix + "&page=1")

.github/scripts/lint_native_functions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def fn(base: str) -> str:
2626
return str(base / Path("aten/src/ATen/native/native_functions.yaml"))
2727

2828

29-
with open(Path(__file__).parent.parent.parent / fn("."), "r") as f:
29+
with open(Path(__file__).parent.parent.parent / fn(".")) as f:
3030
contents = f.read()
3131

3232
yaml = ruamel.yaml.YAML() # type: ignore[attr-defined]

.github/scripts/run_torchbench.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def extract_models_from_pr(
129129
model_list = []
130130
userbenchmark_list = []
131131
pr_list = []
132-
with open(prbody_file, "r") as pf:
132+
with open(prbody_file) as pf:
133133
lines = (x.strip() for x in pf.read().splitlines())
134134
magic_lines = list(filter(lambda x: x.startswith(MAGIC_PREFIX), lines))
135135
if magic_lines:
@@ -157,7 +157,7 @@ def extract_models_from_pr(
157157

158158
def find_torchbench_branch(prbody_file: str) -> str:
159159
branch_name: str = ""
160-
with open(prbody_file, "r") as pf:
160+
with open(prbody_file) as pf:
161161
lines = (x.strip() for x in pf.read().splitlines())
162162
magic_lines = list(
163163
filter(lambda x: x.startswith(MAGIC_TORCHBENCH_PREFIX), lines)

.github/scripts/test_check_labels.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def mock_parse_args() -> object:
18-
class Object(object):
18+
class Object:
1919
def __init__(self) -> None:
2020
self.pr_num = 76123
2121

.github/scripts/test_trymerge.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def mocked_rockset_results(head_sha: str, merge_base: str, num_retries: int = 3)
114114

115115

116116
def mock_parse_args(revert: bool = False, force: bool = False) -> Any:
117-
class Object(object):
117+
class Object:
118118
def __init__(self) -> None:
119119
self.revert = revert
120120
self.force = force

.github/scripts/trymerge.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1628,10 +1628,8 @@ def validate_revert(
16281628
allowed_reverters.append("CONTRIBUTOR")
16291629
if author_association not in allowed_reverters:
16301630
raise PostCommentError(
1631-
(
1632-
f"Will not revert as @{author_login} is not one of "
1633-
f"[{', '.join(allowed_reverters)}], but instead is {author_association}."
1634-
)
1631+
f"Will not revert as @{author_login} is not one of "
1632+
f"[{', '.join(allowed_reverters)}], but instead is {author_association}."
16351633
)
16361634
skip_internal_checks = can_skip_internal_checks(pr, comment_id)
16371635

.github/scripts/trymerge_explainer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def has_label(labels: List[str], pattern: Pattern[str] = CIFLOW_LABEL) -> bool:
1717
return len(list(filter(pattern.match, labels))) > 0
1818

1919

20-
class TryMergeExplainer(object):
20+
class TryMergeExplainer:
2121
force: bool
2222
labels: List[str]
2323
pr_num: int

docs/caffe2/process.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Module caffe2...caffe2.python.control_test
1010
def insert(originalfile, first_line, description):
11-
with open(originalfile, 'r') as f:
11+
with open(originalfile) as f:
1212
f1 = f.readline()
1313
if(f1.find(first_line) < 0):
1414
docs = first_line + description + f1
@@ -30,7 +30,7 @@ def insert(originalfile, first_line, description):
3030
for file in files:
3131
if (file.endswith(".py") and not file.endswith("_test.py") and not file.endswith("__.py")):
3232
filepath = os.path.join(root, file)
33-
print(("filepath: " + filepath))
33+
print("filepath: " + filepath)
3434
directory = os.path.dirname(filepath)[2:]
3535
directory = directory.replace("/", ".")
3636
print("directory: " + directory)

docs/cpp/source/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# PyTorch documentation build configuration file, created by
43
# sphinx-quickstart on Fri Dec 23 13:31:47 2016.
@@ -99,7 +98,7 @@
9998
############################################################################
10099
# Main library page layout example configuration. #
101100
############################################################################
102-
"afterTitleDescription": textwrap.dedent(u'''
101+
"afterTitleDescription": textwrap.dedent('''
103102
Welcome to the developer reference for the PyTorch C++ API.
104103
'''),
105104
}

docs/source/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# PyTorch documentation build configuration file, created by
43
# sphinx-quickstart on Fri Dec 23 13:31:47 2016.
@@ -624,7 +623,7 @@ def visit_reference(self, node):
624623
anchor = ref_anchor[1]
625624
txt = node.parent.astext()
626625
if txt == anchor or txt == anchor.split('.')[-1]:
627-
self.body.append('<p id="{}"/>'.format(ref_anchor[1]))
626+
self.body.append(f'<p id="{ref_anchor[1]}"/>')
628627
return old_call(self, node)
629628
Klass.visit_reference = visit_reference
630629

docs/source/scripts/exportdb/generate_example_rst.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def generate_example_rst(example_case: ExportCase):
3131
if isinstance(model, torch.nn.Module)
3232
else inspect.getfile(model)
3333
)
34-
with open(source_file, "r") as file:
34+
with open(source_file) as file:
3535
source_code = file.read()
3636
source_code = re.sub(r"from torch\._export\.db\.case import .*\n", "", source_code)
3737
source_code = re.sub(r"@export_case\((.|\n)*?\)\n", "", source_code)
@@ -114,7 +114,7 @@ def generate_index_rst(example_cases, tag_to_modules, support_level_to_modules):
114114

115115
tag_names = "\n ".join(t for t in tag_to_modules.keys())
116116

117-
with open(os.path.join(PWD, "blurb.txt"), "r") as file:
117+
with open(os.path.join(PWD, "blurb.txt")) as file:
118118
blurb = file.read()
119119

120120
# Generate contents of the .rst file

0 commit comments

Comments
 (0)