Visitar URL original
gh-132396: address `redefinition of unused name` errors in `Lib/test` by picnixz · Pull Request #132397 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
revert most of noqa: F811 false positive (fixed upstream)
  • Loading branch information
picnixz committed Apr 14, 2025
commit 484cfd03e0c2e22bc8a33f303ff6701b4bd098d8
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
picnixz marked this conversation as resolved.
Outdated
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)
]
2 changes: 1 addition & 1 deletion Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -4865,15 +4865,15 @@ 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
c: int = field(kw_only=True)

# And if inheriting, it's okay.
@dataclass
class A: # noqa: F811
class A:
a: int
_: KW_ONLY
b: int
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
26 changes: 13 additions & 13 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1348,15 +1348,15 @@ class Color(Enum):
red = 4
#
with self.assertRaises(TypeError):
class Color(Enum): # noqa: F811
class Color(Enum):
red = 1
green = 2
blue = 3
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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down