Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF012.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
13 changes: 12 additions & 1 deletion crates/ruff_linter/src/rules/ruff/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`
Loading