]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132396: Resolve 'redefinition of unused name' errors in ``Lib/test/`` (#132397)
authorBénédikt Tran <10796600+picnixz@users.noreply.github.com>
Fri, 18 Apr 2025 17:14:54 +0000 (19:14 +0200)
committerGitHub <noreply@github.com>
Fri, 18 Apr 2025 17:14:54 +0000 (18:14 +0100)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
.pre-commit-config.yaml
Lib/test/.ruff.toml
Lib/test/test_dataclasses/__init__.py
Lib/test/test_descr.py
Lib/test/test_enum.py
Lib/test/test_import/__init__.py
Lib/test/test_pkg.py
Lib/test/test_with.py
Lib/test/test_yield_from.py

index fb44c27704d455b856575546ac2d1e00701a1ec9..ec53cf58bf78a6a2736996a2c165a93636db174d 100644 (file)
@@ -1,6 +1,6 @@
 repos:
   - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: v0.9.1
+    rev: v0.11.4
     hooks:
       - id: ruff
         name: Run Ruff (lint) on Doc/
index 1c9bac507209b1214efd9d5011f34f3fd542c9b5..fa8b2b42579b4a2a531d16a45bd6eda77e4acc69 100644 (file)
@@ -4,19 +4,12 @@ extend-exclude = [
     "test_clinic.py",
     # Excluded (these aren't actually executed, they're just "data files")
     "tokenizedata/*.py",
-    # Failed to lint
+    # Non UTF-8 files
     "encoded_modules/module_iso_8859_1.py",
     "encoded_modules/module_koi8_r.py",
-    # TODO Fix: F811 Redefinition of unused name
-    "test_buffer.py",
-    "test_dataclasses/__init__.py",
-    "test_descr.py",
-    "test_enum.py",
-    "test_functools.py",
+    # New grammar constructions may not yet be recognized by Ruff,
+    # and tests re-use the same names as only the grammar is being checked.
     "test_grammar.py",
-    "test_import/__init__.py",
-    "test_pkg.py",
-    "test_yield_from.py",
 ]
 
 [lint]
index a97b77a68f016c97f106081428818a482029a8ba..99fefb57fd0f0922e1eab2a1dd0b61fbf0d05798 100644 (file)
@@ -771,12 +771,12 @@ class TestCase(unittest.TestCase):
 
                 # Because this is a ClassVar, it can be mutable.
                 @dataclass
-                class C:
+                class UsesMutableClassVar:
                     z: ClassVar[typ] = typ()
 
                 # Because this is a ClassVar, it can be mutable.
                 @dataclass
-                class C:
+                class UsesMutableClassVarWithSubType:
                     x: ClassVar[typ] = Subclass()
 
     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.
         @dataclass
-        class A:
+        class NoDuplicateKwOnlyAnnotation:
             a: int
             _: KW_ONLY
             b: int
@@ -4872,13 +4872,13 @@ class TestKeywordArgs(unittest.TestCase):
 
         # And if inheriting, it's okay.
         @dataclass
-        class A:
+        class BaseUsesKwOnly:
             a: int
             _: KW_ONLY
             b: int
             c: int
         @dataclass
-        class B(A):
+        class SubclassUsesKwOnly(BaseUsesKwOnly):
             _: KW_ONLY
             d: int
 
index 749f27bbd3a662bc89bb105b3472180a5a5270cd..8e9d44a583cb31e478886416a8fce3a02cf0f232 100644 (file)
@@ -1192,10 +1192,9 @@ class ClassPropertiesAndMethods(unittest.TestCase):
             pass
         else:
             self.fail("[''] slots not caught")
-        class C(object):
+
+        class WithValidIdentifiers(object):
             __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.
         class C(object):
index 1a0026987cd7eb57aa4a087c79ed77695882fd38..dde674164f4a520983292a73d72ff4df4a61aba2 100644 (file)
@@ -1352,7 +1352,7 @@ class TestSpecial(unittest.TestCase):
                 red = 1
                 green = 2
                 blue = 3
-                def red(self):
+                def red(self):  # noqa: F811
                     return 'red'
         #
         with self.assertRaises(TypeError):
@@ -1360,7 +1360,7 @@ class TestSpecial(unittest.TestCase):
                 @enum.property
                 def red(self):
                     return 'redder'
-                red = 1
+                red = 1  # noqa: F811
                 green = 2
                 blue = 3
 
index 33df4fef0b247c03943ebb6aa66ab0a9ffda674a..a745760289b5b8e896c24ba3a202e3f21ccd0d1b 100644 (file)
@@ -444,7 +444,7 @@ class ImportTests(unittest.TestCase):
         # (SF bug 422177).
         from test.test_import.data import 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_with_extension(ext):
@@ -573,7 +573,7 @@ class ImportTests(unittest.TestCase):
 
         # import in a 'for' loop resulted in segmentation fault
         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):
         # A failing reload should leave the module object in sys.modules.
index a7a1c2affbe1fb31079465d7124da531bfcb2563..d2b724db40d3e9b6e640c8f511505ad04e6c9476 100644 (file)
@@ -190,7 +190,6 @@ class TestPkg(unittest.TestCase):
          ]
         self.mkhier(hier)
 
-        import t5
         s = """
             from t5 import *
             self.assertEqual(dir(), ['foo', 'self', 'string', 't5'])
index e3e2de094967284ee7efec763251b8e35949c9de..1d2ce9eccc4507aa718d3ba555fbc27500e2a728 100644 (file)
@@ -174,7 +174,7 @@ class FailureTestCase(unittest.TestCase):
             # Ruff complains that we're redefining `self.foo` here,
             # but the whole point of the test is to check that `self.foo`
             # is *not* redefined (because `__enter__` raises)
-            with ct as self.foo:  # ruff: noqa: F811
+            with ct as self.foo:  # noqa: F811
                 pass
         self.assertRaises(RuntimeError, shouldThrow)
         self.assertEqual(self.foo, None)
index 06edc18df149443410f93529fbe2fd1c4e33c0e5..74c9fa16987638479c1d1a9d3abd04ee0e908862 100644 (file)
@@ -896,6 +896,7 @@ class TestPEP380Operation(unittest.TestCase):
             yield 2
         g1 = one()
         self.assertEqual(list(g1), [0, 1, 2, 3])
+
         # Check with send
         g1 = one()
         res = [next(g1)]
@@ -905,6 +906,8 @@ class TestPEP380Operation(unittest.TestCase):
         except StopIteration:
             pass
         self.assertEqual(res, [0, 1, 2, 3])
+
+    def test_delegating_generators_claim_to_be_running_with_throw(self):
         # Check with throw
         class MyErr(Exception):
             pass
@@ -941,8 +944,10 @@ class TestPEP380Operation(unittest.TestCase):
         except:
             self.assertEqual(res, [0, 1, 2, 3])
             raise
+
+    def test_delegating_generators_claim_to_be_running_with_close(self):
         # Check with close
-        class MyIt(object):
+        class MyIt:
             def __iter__(self):
                 return self
             def __next__(self):