gh-132396: Resolve 'redefinition of unused name' errors in `Lib/test/` (#132397)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
Bénédikt Tran 2025-04-18 19:14:54 +02:00 committed by GitHub
parent 4c3d187d9f
commit 1d5dc5f1c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 22 additions and 26 deletions

View file

@ -1,6 +1,6 @@
repos: repos:
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.1 rev: v0.11.4
hooks: hooks:
- id: ruff - id: ruff
name: Run Ruff (lint) on Doc/ name: Run Ruff (lint) on Doc/

View file

@ -4,19 +4,12 @@ extend-exclude = [
"test_clinic.py", "test_clinic.py",
# Excluded (these aren't actually executed, they're just "data files") # Excluded (these aren't actually executed, they're just "data files")
"tokenizedata/*.py", "tokenizedata/*.py",
# Failed to lint # Non UTF-8 files
"encoded_modules/module_iso_8859_1.py", "encoded_modules/module_iso_8859_1.py",
"encoded_modules/module_koi8_r.py", "encoded_modules/module_koi8_r.py",
# TODO Fix: F811 Redefinition of unused name # New grammar constructions may not yet be recognized by Ruff,
"test_buffer.py", # and tests re-use the same names as only the grammar is being checked.
"test_dataclasses/__init__.py",
"test_descr.py",
"test_enum.py",
"test_functools.py",
"test_grammar.py", "test_grammar.py",
"test_import/__init__.py",
"test_pkg.py",
"test_yield_from.py",
] ]
[lint] [lint]

View file

@ -771,12 +771,12 @@ class TestCase(unittest.TestCase):
# Because this is a ClassVar, it can be mutable. # Because this is a ClassVar, it can be mutable.
@dataclass @dataclass
class C: class UsesMutableClassVar:
z: ClassVar[typ] = typ() z: ClassVar[typ] = typ()
# Because this is a ClassVar, it can be mutable. # Because this is a ClassVar, it can be mutable.
@dataclass @dataclass
class C: class UsesMutableClassVarWithSubType:
x: ClassVar[typ] = Subclass() x: ClassVar[typ] = Subclass()
def test_deliberately_mutable_defaults(self): def test_deliberately_mutable_defaults(self):
@ -4864,7 +4864,7 @@ class TestKeywordArgs(unittest.TestCase):
# But this usage is okay, since it's not using KW_ONLY. # But this usage is okay, since it's not using KW_ONLY.
@dataclass @dataclass
class A: class NoDuplicateKwOnlyAnnotation:
a: int a: int
_: KW_ONLY _: KW_ONLY
b: int b: int
@ -4872,13 +4872,13 @@ class TestKeywordArgs(unittest.TestCase):
# And if inheriting, it's okay. # And if inheriting, it's okay.
@dataclass @dataclass
class A: class BaseUsesKwOnly:
a: int a: int
_: KW_ONLY _: KW_ONLY
b: int b: int
c: int c: int
@dataclass @dataclass
class B(A): class SubclassUsesKwOnly(BaseUsesKwOnly):
_: KW_ONLY _: KW_ONLY
d: int d: int

View file

@ -1192,10 +1192,9 @@ class ClassPropertiesAndMethods(unittest.TestCase):
pass pass
else: else:
self.fail("[''] slots not caught") self.fail("[''] slots not caught")
class C(object):
class WithValidIdentifiers(object):
__slots__ = ["a", "a_b", "_a", "A0123456789Z"] __slots__ = ["a", "a_b", "_a", "A0123456789Z"]
# XXX(nnorwitz): was there supposed to be something tested
# from the class above?
# Test a single string is not expanded as a sequence. # Test a single string is not expanded as a sequence.
class C(object): class C(object):

View file

@ -1352,7 +1352,7 @@ class TestSpecial(unittest.TestCase):
red = 1 red = 1
green = 2 green = 2
blue = 3 blue = 3
def red(self): def red(self): # noqa: F811
return 'red' return 'red'
# #
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
@ -1360,7 +1360,7 @@ class TestSpecial(unittest.TestCase):
@enum.property @enum.property
def red(self): def red(self):
return 'redder' return 'redder'
red = 1 red = 1 # noqa: F811
green = 2 green = 2
blue = 3 blue = 3

View file

@ -444,7 +444,7 @@ class ImportTests(unittest.TestCase):
# (SF bug 422177). # (SF bug 422177).
from test.test_import.data import double_const from test.test_import.data import double_const
unload('test.test_import.data.double_const') unload('test.test_import.data.double_const')
from test.test_import.data import double_const from test.test_import.data import double_const # noqa: F811
def test_import(self): def test_import(self):
def test_with_extension(ext): def test_with_extension(ext):
@ -573,7 +573,7 @@ class ImportTests(unittest.TestCase):
# import in a 'for' loop resulted in segmentation fault # import in a 'for' loop resulted in segmentation fault
for i in range(2): for i in range(2):
import test.support.script_helper as x import test.support.script_helper as x # noqa: F811
def test_failing_reload(self): def test_failing_reload(self):
# A failing reload should leave the module object in sys.modules. # A failing reload should leave the module object in sys.modules.

View file

@ -190,7 +190,6 @@ class TestPkg(unittest.TestCase):
] ]
self.mkhier(hier) self.mkhier(hier)
import t5
s = """ s = """
from t5 import * from t5 import *
self.assertEqual(dir(), ['foo', 'self', 'string', 't5']) self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])

View file

@ -174,7 +174,7 @@ class FailureTestCase(unittest.TestCase):
# Ruff complains that we're redefining `self.foo` here, # Ruff complains that we're redefining `self.foo` here,
# but the whole point of the test is to check that `self.foo` # but the whole point of the test is to check that `self.foo`
# is *not* redefined (because `__enter__` raises) # is *not* redefined (because `__enter__` raises)
with ct as self.foo: # ruff: noqa: F811 with ct as self.foo: # noqa: F811
pass pass
self.assertRaises(RuntimeError, shouldThrow) self.assertRaises(RuntimeError, shouldThrow)
self.assertEqual(self.foo, None) self.assertEqual(self.foo, None)

View file

@ -896,6 +896,7 @@ class TestPEP380Operation(unittest.TestCase):
yield 2 yield 2
g1 = one() g1 = one()
self.assertEqual(list(g1), [0, 1, 2, 3]) self.assertEqual(list(g1), [0, 1, 2, 3])
# Check with send # Check with send
g1 = one() g1 = one()
res = [next(g1)] res = [next(g1)]
@ -905,6 +906,8 @@ class TestPEP380Operation(unittest.TestCase):
except StopIteration: except StopIteration:
pass pass
self.assertEqual(res, [0, 1, 2, 3]) self.assertEqual(res, [0, 1, 2, 3])
def test_delegating_generators_claim_to_be_running_with_throw(self):
# Check with throw # Check with throw
class MyErr(Exception): class MyErr(Exception):
pass pass
@ -941,8 +944,10 @@ class TestPEP380Operation(unittest.TestCase):
except: except:
self.assertEqual(res, [0, 1, 2, 3]) self.assertEqual(res, [0, 1, 2, 3])
raise raise
def test_delegating_generators_claim_to_be_running_with_close(self):
# Check with close # Check with close
class MyIt(object): class MyIt:
def __iter__(self): def __iter__(self):
return self return self
def __next__(self): def __next__(self):