diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py index e5ec4d610ba517..af37af2333fb67 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py @@ -150,3 +150,42 @@ class S(ctypes.Structure): ("propagation", ctypes.c_uint64), ("userns_fd", ctypes.c_uint64), ] + +class LES(ctypes.LittleEndianStructure): + test = [""] + _fields_ = [ + ("attr_set", ctypes.c_uint64), + ("attr_clr", ctypes.c_uint64), + ("propagation", ctypes.c_uint64), + ("userns_fd", ctypes.c_uint64), + ] + +class BES(ctypes.BigEndianStructure): + test = [""] + _fields_ = [ + ("attr_set", ctypes.c_uint64), + ("attr_clr", ctypes.c_uint64), + ("propagation", ctypes.c_uint64), + ("userns_fd", ctypes.c_uint64), + ] + +class U(ctypes.Union): + test = [""] + _fields_ = [ + ("a", LES), + ("b", BES), + ] + +class LEU(ctypes.LittleEndianUnion): + test = [""] + _fields_ = [ + ("a", LES), + ("b", BES), + ] + +class BEU(ctypes.BigEndianUnion): + test = [""] + _fields_ = [ + ("a", LES), + ("b", BES), + ] diff --git a/crates/ruff_linter/src/rules/ruff/helpers.rs b/crates/ruff_linter/src/rules/ruff/helpers.rs index ba28950a92d54e..a6ebb82fb16bbe 100644 --- a/crates/ruff_linter/src/rules/ruff/helpers.rs +++ b/crates/ruff_linter/src/rules/ruff/helpers.rs @@ -252,7 +252,18 @@ pub(super) fn is_ctypes_structure_fields( ) -> bool { let is_ctypes_structure = analyze::class::any_qualified_base_class(class_def, semantic, |qualified_name| { - matches!(qualified_name.segments(), ["ctypes", "Structure"]) + matches!( + qualified_name.segments(), + [ + "ctypes", + "Structure" + | "BigEndianStructure" + | "LittleEndianStructure" + | "Union" + | "BigEndianUnion" + | "LittleEndianUnion" + ] + ) }); let is_fields = matches!( diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF012_RUF012.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF012_RUF012.py.snap index 5cc2fd8c41a52f..fdd09af79c1c94 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF012_RUF012.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF012_RUF012.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs +assertion_line: 133 --- RUF012 Mutable default value for class attribute --> RUF012.py:9:34 @@ -139,3 +140,58 @@ RUF012 Mutable default value for class attribute 148 | ("attr_set", ctypes.c_uint64), | help: Consider initializing in `__init__` or annotating with `typing.ClassVar` + +RUF012 Mutable default value for class attribute + --> RUF012.py:155:12 + | +154 | class LES(ctypes.LittleEndianStructure): +155 | test = [""] + | ^^^^ +156 | _fields_ = [ +157 | ("attr_set", ctypes.c_uint64), + | +help: Consider initializing in `__init__` or annotating with `typing.ClassVar` + +RUF012 Mutable default value for class attribute + --> RUF012.py:164:12 + | +163 | class BES(ctypes.BigEndianStructure): +164 | test = [""] + | ^^^^ +165 | _fields_ = [ +166 | ("attr_set", ctypes.c_uint64), + | +help: Consider initializing in `__init__` or annotating with `typing.ClassVar` + +RUF012 Mutable default value for class attribute + --> RUF012.py:173:12 + | +172 | class U(ctypes.Union): +173 | test = [""] + | ^^^^ +174 | _fields_ = [ +175 | ("a", LES), + | +help: Consider initializing in `__init__` or annotating with `typing.ClassVar` + +RUF012 Mutable default value for class attribute + --> RUF012.py:180:12 + | +179 | class LEU(ctypes.LittleEndianUnion): +180 | test = [""] + | ^^^^ +181 | _fields_ = [ +182 | ("a", LES), + | +help: Consider initializing in `__init__` or annotating with `typing.ClassVar` + +RUF012 Mutable default value for class attribute + --> RUF012.py:187:12 + | +186 | class BEU(ctypes.BigEndianUnion): +187 | test = [""] + | ^^^^ +188 | _fields_ = [ +189 | ("a", LES), + | +help: Consider initializing in `__init__` or annotating with `typing.ClassVar`