Skip to content

Commit

Permalink
pythongh-110558: Enable ruff's pyupgrade rules when running on Argume…
Browse files Browse the repository at this point in the history
…nt Clinic
  • Loading branch information
AlexWaygood committed Oct 10, 2023
1 parent d5ec77f commit b1beca0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
args: [--exit-non-zero-on-fix]
files: ^Lib/test/
- id: ruff
name: Run Ruff on Tools/clinic/
name: Run Ruff on Argument Clinic
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]
files: ^Tools/clinic/|Lib/test/test_clinic.py

Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2398,15 +2398,15 @@ def expect_failure(self, *args):
def test_external(self):
CLINIC_TEST = 'clinic.test.c'
source = support.findfile(CLINIC_TEST)
with open(source, 'r', encoding='utf-8') as f:
with open(source, encoding='utf-8') as f:
orig_contents = f.read()

# Run clinic CLI and verify that it does not complain.
self.addCleanup(unlink, TESTFN)
out = self.expect_success("-f", "-o", TESTFN, source)
self.assertEqual(out, "")

with open(TESTFN, 'r', encoding='utf-8') as f:
with open(TESTFN, encoding='utf-8') as f:
new_contents = f.read()

self.assertEqual(new_contents, orig_contents)
Expand Down Expand Up @@ -2466,7 +2466,7 @@ def test_cli_force(self):
"/*[clinic end generated code: "
"output=c16447c01510dfb3 input=9543a8d2da235301]*/\n"
)
with open(fn, 'r', encoding='utf-8') as f:
with open(fn, encoding='utf-8') as f:
generated = f.read()
self.assertTrue(generated.endswith(checksum),
(generated, checksum))
Expand Down
12 changes: 12 additions & 0 deletions Tools/clinic/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ target-version = "py310"
fix = true
select = [
"F", # Enable all pyflakes rules
"UP", # Enable all pyupgrade rules by default
"RUF100", # Ban unused `# noqa` comments
"PGH004", # Ban blanket `# noqa` comments (only ignore specific error codes)
]
ignore = [
# Unnecessary parentheses to functools.lru_cache: just leads to unnecessary churn.
# https://github.com/python/cpython/pull/104684#discussion_r1199653347.
"UP011",
# Use format specifiers instead of %-style formatting.
# Doesn't always make code more readable.
"UP031",
# Use PEP-604 unions rather than tuples for isinstance() checks.
# Makes code slower and more verbose. https://github.com/astral-sh/ruff/issues/7871.
"UP038",
]
unfixable = [
# The autofixes sometimes do the wrong things for these;
# it's better to have to manually look at the code and see how it needs fixing
Expand Down
8 changes: 4 additions & 4 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,7 +2423,7 @@ def dump(self) -> str:

def write_file(filename: str, new_contents: str) -> None:
try:
with open(filename, 'r', encoding="utf-8") as fp:
with open(filename, encoding="utf-8") as fp:
old_contents = fp.read()

if old_contents == new_contents:
Expand Down Expand Up @@ -4759,9 +4759,9 @@ def set_template_dict(self, template_dict: TemplateDict) -> None:
'({0} == base_tp || {0}->tp_init == base_tp->tp_init)'
).format(self.name)
else:
type_check = ('(Py_IS_TYPE({0}, base_tp) ||\n '
' Py_TYPE({0})->tp_new == base_tp->tp_new)'
).format(self.name)
type_check = (f'(Py_IS_TYPE({self.name}, base_tp) ||\n '
f' Py_TYPE({self.name})->tp_new == base_tp->tp_new)'
)

line = f'{type_check} &&\n '
template_dict['self_type_check'] = line
Expand Down

0 comments on commit b1beca0

Please sign in to comment.