From 6e01167502577b404479206baae2c1e9823ff5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:31:01 +0200 Subject: [PATCH 01/16] fix `noqa: F811` suppression rule --- Lib/test/test_with.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index e3e2de09496728..1d2ce9eccc4507 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -174,7 +174,7 @@ def shouldThrow(): # 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) From d3f0433eacdfb34a71e4ae1307224d64b8a063fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:39:23 +0200 Subject: [PATCH 02/16] prune valid files from ruff's exclusion list --- Lib/test/.ruff.toml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 1c9bac507209b1..10613e60e53884 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -4,16 +4,17 @@ extend-exclude = [ "test_clinic.py", # Excluded (these aren't actually executed, they're just "data files") "tokenizedata/*.py", - # Failed to lint + # Failed to lint (encoding is not UTF-8, but this is deliberate) "encoded_modules/module_iso_8859_1.py", "encoded_modules/module_koi8_r.py", + # Excluded (some 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", # TODO Fix: F811 Redefinition of unused name - "test_buffer.py", "test_dataclasses/__init__.py", "test_descr.py", "test_enum.py", "test_functools.py", - "test_grammar.py", "test_import/__init__.py", "test_pkg.py", "test_yield_from.py", From 280e6e1589cf94720eb49ddf1c959417fb838162 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:43:41 +0200 Subject: [PATCH 03/16] add `noqa: F811` in `Lib/test/test_dataclasses/__init__.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_dataclasses/__init__.py | 38 +++++++++++++-------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 10613e60e53884..8b432225f14f3d 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_dataclasses/__init__.py", "test_descr.py", "test_enum.py", "test_functools.py", diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 869a043211b0a1..37e7d8b19ab3bd 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -591,7 +591,7 @@ class A: with self.assertRaisesRegex(ValueError, unhashable_re): @dataclass - class A: + class A: # noqa: F811 a: Any = Unhashable() # Make sure that the machinery looking for hashability is using the @@ -601,7 +601,7 @@ class A: # This shouldn't make the variable hashable. unhashable.__hash__ = lambda: 0 @dataclass - class A: + class A: # noqa: F811 a: Any = unhashable def test_hash_field_rules(self): @@ -755,7 +755,7 @@ class Point: f'mutable default {typ} for field ' 'y is not allowed'): @dataclass - class Point: + class Point: # noqa: F811 y: typ = non_empty # Check subtypes also fail. @@ -766,7 +766,7 @@ class Subclass(typ): pass " for field z is not allowed" ): @dataclass - class Point: + class Point: # noqa: F811 z: typ = Subclass() # Because this is a ClassVar, it can be mutable. @@ -776,7 +776,7 @@ class C: # Because this is a ClassVar, it can be mutable. @dataclass - class C: + class C: # noqa: F811 x: ClassVar[typ] = Subclass() def test_deliberately_mutable_defaults(self): @@ -2054,7 +2054,7 @@ class C: # Make sure an empty dict works. d = {} @dataclass - class C: + class C: # noqa: F811 i: int = field(metadata=d) self.assertFalse(fields(C)[0].metadata) self.assertEqual(len(fields(C)[0].metadata), 0) @@ -2648,7 +2648,7 @@ def __lt__(self): 'Cannot overwrite attribute __le__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: + class C: # noqa: F811 x: int def __le__(self): pass @@ -2657,7 +2657,7 @@ def __le__(self): 'Cannot overwrite attribute __gt__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: + class C: # noqa: F811 x: int def __gt__(self): pass @@ -2666,7 +2666,7 @@ def __gt__(self): 'Cannot overwrite attribute __ge__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: + class C: # noqa: F811 x: int def __ge__(self): pass @@ -3178,13 +3178,13 @@ def __setattr__(self): with self.assertRaisesRegex(TypeError, 'Cannot overwrite attribute __delattr__'): @dataclass(frozen=True) - class C: + class C: # noqa: F811 x: int def __delattr__(self): pass @dataclass(frozen=False) - class C: + class C: # noqa: F811 x: int def __setattr__(self, name, value): self.__dict__['x'] = value * 2 @@ -3715,7 +3715,7 @@ class F[T2](WithSlots): self.assertTrue(F.__weakref__) F() - def test_dataclass_derived_generic_from_slotted_base(self): + def test_dataclass_derived_generic_from_slotted_base_with_weakref(self): T = typing.TypeVar('T') class WithWeakrefSlot: @@ -4700,12 +4700,12 @@ class A: with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: + class A: # noqa: F811 a: ClassVar[int] = field(kw_only=False) with self.assertRaisesRegex(TypeError, msg): @dataclass(kw_only=True) - class A: + class A: # noqa: F811 a: ClassVar[int] = field(kw_only=False) def test_field_marked_as_kwonly(self): @@ -4847,7 +4847,7 @@ class A: with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: + class A: # noqa: F811 a: int X: KW_ONLY b: int @@ -4856,7 +4856,7 @@ class A: with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: + class A: # noqa: F811 a: int X: KW_ONLY b: int @@ -4865,7 +4865,7 @@ class A: # But this usage is okay, since it's not using KW_ONLY. @dataclass - class A: + class A: # noqa: F811 a: int _: KW_ONLY b: int @@ -4873,7 +4873,7 @@ class A: # And if inheriting, it's okay. @dataclass - class A: + class A: # noqa: F811 a: int _: KW_ONLY b: int @@ -4892,7 +4892,7 @@ class A: b: int c: int @dataclass - class B(A): + class B(A): # noqa: F811 X: KW_ONLY d: int Y: KW_ONLY From 68186ecd9a261397d98939e03c90dfb7cb7de669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:45:42 +0200 Subject: [PATCH 04/16] add `noqa: F811` in `Lib/test/test_descr.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_descr.py | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 8b432225f14f3d..d046e926af4418 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_descr.py", "test_enum.py", "test_functools.py", "test_import/__init__.py", diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index aa453e438facd5..536c35e0cff55f 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1198,7 +1198,7 @@ class C(object): # from the class above? # Test a single string is not expanded as a sequence. - class C(object): + class C(object): # noqa: F811 __slots__ = "abc" c = C() c.abc = 5 @@ -4159,21 +4159,21 @@ def test_unsubclassable_types(self): class X(type(None)): pass with self.assertRaises(TypeError): - class X(object, type(None)): + class X(object, type(None)): # noqa: F811 pass with self.assertRaises(TypeError): - class X(type(None), object): + class X(type(None), object): # noqa: F811 pass class O(object): pass with self.assertRaises(TypeError): - class X(O, type(None)): + class X(O, type(None)): # noqa: F811 pass with self.assertRaises(TypeError): - class X(type(None), O): + class X(type(None), O): # noqa: F811 pass - class X(object): + class X(object): # noqa: F811 pass with self.assertRaises(TypeError): X.__bases__ = type(None), From f8f019894939759c002a980247dc31704b471f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:46:22 +0200 Subject: [PATCH 05/16] add `noqa: F811` in `Lib/test/test_enum.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_enum.py | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index d046e926af4418..00ea0ad689c2b4 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_enum.py", "test_functools.py", "test_import/__init__.py", "test_pkg.py", diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 1a0026987cd7eb..49a6d1ca5b93ff 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -634,16 +634,16 @@ def test_invalid_names(self): class Wrong(self.enum_type): mro = 9 with self.assertRaises(ValueError): - class Wrong(self.enum_type): + class Wrong(self.enum_type): # noqa: F811 _create_= 11 with self.assertRaises(ValueError): - class Wrong(self.enum_type): + class Wrong(self.enum_type): # noqa: F811 _get_mixins_ = 9 with self.assertRaises(ValueError): - class Wrong(self.enum_type): + class Wrong(self.enum_type): # noqa: F811 _find_new_ = 1 with self.assertRaises(ValueError): - class Wrong(self.enum_type): + class Wrong(self.enum_type): # noqa: F811 _any_name_ = 9 def test_object_str_override(self): @@ -1348,19 +1348,19 @@ class Color(Enum): red = 4 # with self.assertRaises(TypeError): - class Color(Enum): + class Color(Enum): # noqa: F811 red = 1 green = 2 blue = 3 - def red(self): + def red(self): # noqa: F811 return 'red' # with self.assertRaises(TypeError): - class Color(Enum): + class Color(Enum): # noqa: F811 @enum.property def red(self): return 'redder' - red = 1 + red = 1 # noqa: F811 green = 2 blue = 3 @@ -1762,7 +1762,7 @@ class MyInt(int): def repr(self): return hex(self) with self.assertRaisesRegex(TypeError, 'too many data types'): - class Huh(MyStr, MyInt, Enum): + class Huh(MyStr, MyInt, Enum): # noqa: F811 One = 1 @reraise_if_not_enum(Stooges) @@ -2592,7 +2592,7 @@ class Color(UniqueEnum): green = 2 blue = 3 with self.assertRaises(ValueError): - class Color(UniqueEnum): + class Color(UniqueEnum): # noqa: F811 red = 1 green = 2 blue = 3 @@ -3015,11 +3015,11 @@ class ThirdFailedStrEnum(StrEnum): one = '1' two = 2 with self.assertRaisesRegex(TypeError, 'encoding must be a string, not %r' % (sys.getdefaultencoding, )): - class ThirdFailedStrEnum(StrEnum): + class ThirdFailedStrEnum(StrEnum): # noqa: F811 one = '1' two = b'2', sys.getdefaultencoding with self.assertRaisesRegex(TypeError, 'errors must be a string, not 9'): - class ThirdFailedStrEnum(StrEnum): + class ThirdFailedStrEnum(StrEnum): # noqa: F811 one = '1' two = b'2', 'ascii', 9 @@ -3075,12 +3075,12 @@ class ThirdFailedStrEnum(CustomStrEnum): two = 2 # this will become '2' with self.assertRaisesRegex(TypeError, r"argument (2|'encoding') must be str, not "): - class ThirdFailedStrEnum(CustomStrEnum): + class ThirdFailedStrEnum(CustomStrEnum): # noqa: F811 one = '1' two = b'2', sys.getdefaultencoding with self.assertRaisesRegex(TypeError, r"argument (3|'errors') must be str, not "): - class ThirdFailedStrEnum(CustomStrEnum): + class ThirdFailedStrEnum(CustomStrEnum): # noqa: F811 one = '1' two = b'2', 'ascii', 9 @@ -3382,7 +3382,7 @@ class MyEnum(Base, enum.Enum): def __init__(self, y): self.y = y with self.assertRaises(ValueError): - class MyEnum(Base, enum.Enum): + class MyEnum(Base, enum.Enum): # noqa: F811 A = 'a' def __init__(self, y): self.y = y From 8c66a7494746ad64c912b242b7a18aa05ed3e8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:46:39 +0200 Subject: [PATCH 06/16] add `noqa: F811` in `Lib/test/test_functools.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_functools.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 00ea0ad689c2b4..348e20a49b9086 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_functools.py", "test_import/__init__.py", "test_pkg.py", "test_yield_from.py", diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 2b49615178f136..06b0c7bbfc2a17 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -674,10 +674,10 @@ def test_invalid_args(self): class B(object): method = functools.partialmethod(None, 1) with self.assertRaises(TypeError): - class B: + class B: # noqa: F811 method = functools.partialmethod() with self.assertRaises(TypeError): - class B: + class B: # noqa: F811 method = functools.partialmethod(func=capture, a=1) def test_repr(self): From 887075f26a8954697455695092ce69bb95c90301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:48:50 +0200 Subject: [PATCH 07/16] add `noqa: F811` in `Lib/test/test_import/__init__.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_import/__init__.py | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 348e20a49b9086..35e9ac6987f055 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_import/__init__.py", "test_pkg.py", "test_yield_from.py", ] diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 33df4fef0b247c..bbcfbadba1d5df 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -444,7 +444,7 @@ def test_double_const(self): # (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 @@ def test_issue31286(self): # 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. @@ -828,7 +828,7 @@ def test_frozen_module_from_import_error(self): str(cm.exception), ) with self.assertRaises(ImportError) as cm: - from sys import this_will_never_exist + from sys import this_will_never_exist # noqa: F811 self.assertIn( "cannot import name 'this_will_never_exist' from 'sys' (unknown location)", str(cm.exception), From eeaf2f6fdc5b791c7a96fa32da1d73295f39f866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:56:18 +0200 Subject: [PATCH 08/16] fix `ruff(F811)` in `Lib/test/test_pkg.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_pkg.py | 1 - 2 files changed, 2 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 35e9ac6987f055..52a7305740f606 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_pkg.py", "test_yield_from.py", ] diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py index a7a1c2affbe1fb..d2b724db40d3e9 100644 --- a/Lib/test/test_pkg.py +++ b/Lib/test/test_pkg.py @@ -190,7 +190,6 @@ def test_5(self): ] self.mkhier(hier) - import t5 s = """ from t5 import * self.assertEqual(dir(), ['foo', 'self', 'string', 't5']) From b1374b96f44207b0a2dc62b5713b28308df1bb63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:30:43 +0200 Subject: [PATCH 09/16] add `noqa: F811` in `Lib/test/test_with.py` --- Lib/test/test_ctypes/test_struct_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 5c713247a0f418..9b8dc37e0c829d 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -107,7 +107,7 @@ class Z(Structure): class TooBig(Structure): _fields_ = [('largeField', X * (max_field_size + 1))] with self.assertRaises(OverflowError): - class TooBig(Structure): + class TooBig(Structure): # noqa: F811 _fields_ = [('largeField', c_char * (max_field_size + 1))] # Also test around edge case for the bit_size calculation From f1cfd78b9cc17ed17f16475a3e7efd54070b4fb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Wed, 9 Apr 2025 13:05:50 +0200 Subject: [PATCH 10/16] fix `ruff(F811)` in `Lib/test/test_yield_from.py` --- Lib/test/.ruff.toml | 1 - Lib/test/test_yield_from.py | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 52a7305740f606..8a0ba9545d2078 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -11,7 +11,6 @@ extend-exclude = [ # and tests re-use the same names as only the grammar is being checked) "test_grammar.py", # TODO Fix: F811 Redefinition of unused name - "test_yield_from.py", ] [lint] diff --git a/Lib/test/test_yield_from.py b/Lib/test/test_yield_from.py index 06edc18df14944..74c9fa16987638 100644 --- a/Lib/test/test_yield_from.py +++ b/Lib/test/test_yield_from.py @@ -896,6 +896,7 @@ def two(): yield 2 g1 = one() self.assertEqual(list(g1), [0, 1, 2, 3]) + # Check with send g1 = one() res = [next(g1)] @@ -905,6 +906,8 @@ def two(): 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 @@ def two(): 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): From 2a09ca054327b1caa1e20fa8bc26df7af98740db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 12 Apr 2025 02:09:24 +0200 Subject: [PATCH 11/16] Update Lib/test/.ruff.toml Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/test/.ruff.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 8a0ba9545d2078..f44099b10734f2 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -7,8 +7,8 @@ extend-exclude = [ # Failed to lint (encoding is not UTF-8, but this is deliberate) "encoded_modules/module_iso_8859_1.py", "encoded_modules/module_koi8_r.py", - # Excluded (some grammar constructions may not yet be recognized by ruff, - # and tests re-use the same names as only the grammar is being checked) + # 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", # TODO Fix: F811 Redefinition of unused name ] From c816e39cbe7dffc06a78bbf3f9c6e671652cb385 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 12 Apr 2025 02:09:30 +0200 Subject: [PATCH 12/16] Update Lib/test/.ruff.toml Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/test/.ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index f44099b10734f2..97ffe2f65deacf 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -4,7 +4,7 @@ extend-exclude = [ "test_clinic.py", # Excluded (these aren't actually executed, they're just "data files") "tokenizedata/*.py", - # Failed to lint (encoding is not UTF-8, but this is deliberate) + # Non UTF-8 files "encoded_modules/module_iso_8859_1.py", "encoded_modules/module_koi8_r.py", # New grammar constructions may not yet be recognized by Ruff, From de5edc7d152fa6cc6a4bebc9d1dba71c7b0d3fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sat, 12 Apr 2025 02:09:38 +0200 Subject: [PATCH 13/16] Update Lib/test/.ruff.toml Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> --- Lib/test/.ruff.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index 97ffe2f65deacf..fa8b2b42579b4a 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -10,7 +10,6 @@ extend-exclude = [ # 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", - # TODO Fix: F811 Redefinition of unused name ] [lint] From 484cfd03e0c2e22bc8a33f303ff6701b4bd098d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 14 Apr 2025 16:28:08 +0200 Subject: [PATCH 14/16] revert most of noqa: F811 false positive (fixed upstream) --- .pre-commit-config.yaml | 2 +- Lib/test/.ruff.toml | 9 ++++++ Lib/test/test_ctypes/test_struct_fields.py | 2 +- Lib/test/test_dataclasses/__init__.py | 36 +++++++++++----------- Lib/test/test_descr.py | 12 ++++---- Lib/test/test_enum.py | 26 ++++++++-------- Lib/test/test_functools.py | 4 +-- Lib/test/test_import/__init__.py | 2 +- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fb44c27704d455..ec53cf58bf78a6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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/ diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index fa8b2b42579b4a..f759440396e5e4 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -13,6 +13,15 @@ extend-exclude = [ ] [lint] +# Variables with single letters and an optional numeric prefix +# are considered to be used, so that we do not need to always +# add a 'F811' suppression. +dummy-variable-rgx = """(?x:^( \ + _+ \ + |(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?) \ + |([a-zA-Z][0-9]*) \ +)$)""" + select = [ "F811", # Redefinition of unused variable (useful for finding test methods with the same name) ] diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 9b8dc37e0c829d..5c713247a0f418 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -107,7 +107,7 @@ class Z(Structure): class TooBig(Structure): _fields_ = [('largeField', X * (max_field_size + 1))] with self.assertRaises(OverflowError): - class TooBig(Structure): # noqa: F811 + class TooBig(Structure): _fields_ = [('largeField', c_char * (max_field_size + 1))] # Also test around edge case for the bit_size calculation diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 37e7d8b19ab3bd..55cdda46823837 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -591,7 +591,7 @@ class A: with self.assertRaisesRegex(ValueError, unhashable_re): @dataclass - class A: # noqa: F811 + class A: a: Any = Unhashable() # Make sure that the machinery looking for hashability is using the @@ -601,7 +601,7 @@ class A: # noqa: F811 # This shouldn't make the variable hashable. unhashable.__hash__ = lambda: 0 @dataclass - class A: # noqa: F811 + class A: a: Any = unhashable def test_hash_field_rules(self): @@ -755,7 +755,7 @@ class Point: f'mutable default {typ} for field ' 'y is not allowed'): @dataclass - class Point: # noqa: F811 + class Point: y: typ = non_empty # Check subtypes also fail. @@ -766,7 +766,7 @@ class Subclass(typ): pass " for field z is not allowed" ): @dataclass - class Point: # noqa: F811 + class Point: z: typ = Subclass() # Because this is a ClassVar, it can be mutable. @@ -776,7 +776,7 @@ class C: # Because this is a ClassVar, it can be mutable. @dataclass - class C: # noqa: F811 + class C: x: ClassVar[typ] = Subclass() def test_deliberately_mutable_defaults(self): @@ -2054,7 +2054,7 @@ class C: # Make sure an empty dict works. d = {} @dataclass - class C: # noqa: F811 + class C: i: int = field(metadata=d) self.assertFalse(fields(C)[0].metadata) self.assertEqual(len(fields(C)[0].metadata), 0) @@ -2648,7 +2648,7 @@ def __lt__(self): 'Cannot overwrite attribute __le__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: # noqa: F811 + class C: x: int def __le__(self): pass @@ -2657,7 +2657,7 @@ def __le__(self): 'Cannot overwrite attribute __gt__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: # noqa: F811 + class C: x: int def __gt__(self): pass @@ -2666,7 +2666,7 @@ def __gt__(self): 'Cannot overwrite attribute __ge__' '.*using functools.total_ordering'): @dataclass(order=True) - class C: # noqa: F811 + class C: x: int def __ge__(self): pass @@ -3178,13 +3178,13 @@ def __setattr__(self): with self.assertRaisesRegex(TypeError, 'Cannot overwrite attribute __delattr__'): @dataclass(frozen=True) - class C: # noqa: F811 + class C: x: int def __delattr__(self): pass @dataclass(frozen=False) - class C: # noqa: F811 + class C: x: int def __setattr__(self, name, value): self.__dict__['x'] = value * 2 @@ -4700,12 +4700,12 @@ class A: with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: # noqa: F811 + class A: a: ClassVar[int] = field(kw_only=False) with self.assertRaisesRegex(TypeError, msg): @dataclass(kw_only=True) - class A: # noqa: F811 + class A: a: ClassVar[int] = field(kw_only=False) def test_field_marked_as_kwonly(self): @@ -4847,7 +4847,7 @@ class A: with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: # noqa: F811 + class A: a: int X: KW_ONLY b: int @@ -4856,7 +4856,7 @@ class A: # noqa: F811 with self.assertRaisesRegex(TypeError, msg): @dataclass - class A: # noqa: F811 + class A: a: int X: KW_ONLY b: int @@ -4865,7 +4865,7 @@ class A: # noqa: F811 # But this usage is okay, since it's not using KW_ONLY. @dataclass - class A: # noqa: F811 + class A: a: int _: KW_ONLY b: int @@ -4873,7 +4873,7 @@ class A: # noqa: F811 # And if inheriting, it's okay. @dataclass - class A: # noqa: F811 + class A: a: int _: KW_ONLY b: int @@ -4892,7 +4892,7 @@ class A: b: int c: int @dataclass - class B(A): # noqa: F811 + class B(A): X: KW_ONLY d: int Y: KW_ONLY diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 536c35e0cff55f..aa453e438facd5 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1198,7 +1198,7 @@ class C(object): # from the class above? # Test a single string is not expanded as a sequence. - class C(object): # noqa: F811 + class C(object): __slots__ = "abc" c = C() c.abc = 5 @@ -4159,21 +4159,21 @@ def test_unsubclassable_types(self): class X(type(None)): pass with self.assertRaises(TypeError): - class X(object, type(None)): # noqa: F811 + class X(object, type(None)): pass with self.assertRaises(TypeError): - class X(type(None), object): # noqa: F811 + class X(type(None), object): pass class O(object): pass with self.assertRaises(TypeError): - class X(O, type(None)): # noqa: F811 + class X(O, type(None)): pass with self.assertRaises(TypeError): - class X(type(None), O): # noqa: F811 + class X(type(None), O): pass - class X(object): # noqa: F811 + class X(object): pass with self.assertRaises(TypeError): X.__bases__ = type(None), diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 49a6d1ca5b93ff..dde674164f4a52 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -634,16 +634,16 @@ def test_invalid_names(self): class Wrong(self.enum_type): mro = 9 with self.assertRaises(ValueError): - class Wrong(self.enum_type): # noqa: F811 + class Wrong(self.enum_type): _create_= 11 with self.assertRaises(ValueError): - class Wrong(self.enum_type): # noqa: F811 + class Wrong(self.enum_type): _get_mixins_ = 9 with self.assertRaises(ValueError): - class Wrong(self.enum_type): # noqa: F811 + class Wrong(self.enum_type): _find_new_ = 1 with self.assertRaises(ValueError): - class Wrong(self.enum_type): # noqa: F811 + class Wrong(self.enum_type): _any_name_ = 9 def test_object_str_override(self): @@ -1348,7 +1348,7 @@ class Color(Enum): red = 4 # with self.assertRaises(TypeError): - class Color(Enum): # noqa: F811 + class Color(Enum): red = 1 green = 2 blue = 3 @@ -1356,7 +1356,7 @@ def red(self): # noqa: F811 return 'red' # with self.assertRaises(TypeError): - class Color(Enum): # noqa: F811 + class Color(Enum): @enum.property def red(self): return 'redder' @@ -1762,7 +1762,7 @@ class MyInt(int): def repr(self): return hex(self) with self.assertRaisesRegex(TypeError, 'too many data types'): - class Huh(MyStr, MyInt, Enum): # noqa: F811 + class Huh(MyStr, MyInt, Enum): One = 1 @reraise_if_not_enum(Stooges) @@ -2592,7 +2592,7 @@ class Color(UniqueEnum): green = 2 blue = 3 with self.assertRaises(ValueError): - class Color(UniqueEnum): # noqa: F811 + class Color(UniqueEnum): red = 1 green = 2 blue = 3 @@ -3015,11 +3015,11 @@ class ThirdFailedStrEnum(StrEnum): one = '1' two = 2 with self.assertRaisesRegex(TypeError, 'encoding must be a string, not %r' % (sys.getdefaultencoding, )): - class ThirdFailedStrEnum(StrEnum): # noqa: F811 + class ThirdFailedStrEnum(StrEnum): one = '1' two = b'2', sys.getdefaultencoding with self.assertRaisesRegex(TypeError, 'errors must be a string, not 9'): - class ThirdFailedStrEnum(StrEnum): # noqa: F811 + class ThirdFailedStrEnum(StrEnum): one = '1' two = b'2', 'ascii', 9 @@ -3075,12 +3075,12 @@ class ThirdFailedStrEnum(CustomStrEnum): two = 2 # this will become '2' with self.assertRaisesRegex(TypeError, r"argument (2|'encoding') must be str, not "): - class ThirdFailedStrEnum(CustomStrEnum): # noqa: F811 + class ThirdFailedStrEnum(CustomStrEnum): one = '1' two = b'2', sys.getdefaultencoding with self.assertRaisesRegex(TypeError, r"argument (3|'errors') must be str, not "): - class ThirdFailedStrEnum(CustomStrEnum): # noqa: F811 + class ThirdFailedStrEnum(CustomStrEnum): one = '1' two = b'2', 'ascii', 9 @@ -3382,7 +3382,7 @@ class MyEnum(Base, enum.Enum): def __init__(self, y): self.y = y with self.assertRaises(ValueError): - class MyEnum(Base, enum.Enum): # noqa: F811 + class MyEnum(Base, enum.Enum): A = 'a' def __init__(self, y): self.y = y diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 06b0c7bbfc2a17..2b49615178f136 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -674,10 +674,10 @@ def test_invalid_args(self): class B(object): method = functools.partialmethod(None, 1) with self.assertRaises(TypeError): - class B: # noqa: F811 + class B: method = functools.partialmethod() with self.assertRaises(TypeError): - class B: # noqa: F811 + class B: method = functools.partialmethod(func=capture, a=1) def test_repr(self): diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index bbcfbadba1d5df..a745760289b5b8 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -828,7 +828,7 @@ def test_frozen_module_from_import_error(self): str(cm.exception), ) with self.assertRaises(ImportError) as cm: - from sys import this_will_never_exist # noqa: F811 + from sys import this_will_never_exist self.assertIn( "cannot import name 'this_will_never_exist' from 'sys' (unknown location)", str(cm.exception), From e3acc71603fa2aea22595de0d336672b5c8b51d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Mon, 14 Apr 2025 16:43:03 +0200 Subject: [PATCH 15/16] temporary fix --- Lib/test/test_dataclasses/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index 55cdda46823837..cd4bffe010ebaf 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -3715,7 +3715,7 @@ class F[T2](WithSlots): self.assertTrue(F.__weakref__) F() - def test_dataclass_derived_generic_from_slotted_base_with_weakref(self): + def test_dataclass_derived_generic_from_slotted_base(self): # noqa: F811 T = typing.TypeVar('T') class WithWeakrefSlot: From 71364171ccb14571f6a906e9f51f0350739efdcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Fri, 18 Apr 2025 11:38:24 +0200 Subject: [PATCH 16/16] correct false positives --- Lib/test/.ruff.toml | 9 --------- Lib/test/test_dataclasses/__init__.py | 10 +++++----- Lib/test/test_descr.py | 5 ++--- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Lib/test/.ruff.toml b/Lib/test/.ruff.toml index f759440396e5e4..fa8b2b42579b4a 100644 --- a/Lib/test/.ruff.toml +++ b/Lib/test/.ruff.toml @@ -13,15 +13,6 @@ extend-exclude = [ ] [lint] -# Variables with single letters and an optional numeric prefix -# are considered to be used, so that we do not need to always -# add a 'F811' suppression. -dummy-variable-rgx = """(?x:^( \ - _+ \ - |(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?) \ - |([a-zA-Z][0-9]*) \ -)$)""" - select = [ "F811", # Redefinition of unused variable (useful for finding test methods with the same name) ] diff --git a/Lib/test/test_dataclasses/__init__.py b/Lib/test/test_dataclasses/__init__.py index a97b77a68f016c..99fefb57fd0f09 100644 --- a/Lib/test/test_dataclasses/__init__.py +++ b/Lib/test/test_dataclasses/__init__.py @@ -771,12 +771,12 @@ class Point: # 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 A: # 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 A: # 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 diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index aa453e438facd5..a5cec33aec8065 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1192,10 +1192,9 @@ class C(object): 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):