Skip to content

Commit 12f2a4d

Browse files
authored
Remove unused globals complexity (#35)
* Remove unused globals complexity * Fix lint
1 parent ca0c6ed commit 12f2a4d

13 files changed

Lines changed: 120 additions & 126 deletions

‎ext4/__init__.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,7 @@
8383
)
8484

8585
__all__ = [
86-
"BlockDescriptor",
87-
"BlockDevice",
88-
"BlockIO",
89-
"BlockIOBlocks",
90-
"CharacterDevice",
91-
"ChecksumError",
92-
"Directory",
93-
"DirectoryEntry",
94-
"DirectoryEntry2",
95-
"DirectoryEntryHash",
96-
"DirectoryEntryTail",
97-
"DotDirectoryEntry2",
9886
"DX_HASH",
99-
"DXEntry",
100-
"DXRoot",
101-
"DXRootInfo",
10287
"EXT2_FLAGS",
10388
"EXT4_BG",
10489
"EXT4_CHKSUM",
@@ -119,6 +104,23 @@
119104
"EXT4_NAME_LEN",
120105
"EXT4_OS",
121106
"EXT4_REV",
107+
"FS_ENCRYPTION_MODE",
108+
"MODE",
109+
"BlockDescriptor",
110+
"BlockDevice",
111+
"BlockIO",
112+
"BlockIOBlocks",
113+
"CharacterDevice",
114+
"ChecksumError",
115+
"DXEntry",
116+
"DXRoot",
117+
"DXRootInfo",
118+
"Directory",
119+
"DirectoryEntry",
120+
"DirectoryEntry2",
121+
"DirectoryEntryHash",
122+
"DirectoryEntryTail",
123+
"DotDirectoryEntry2",
122124
"ExtendedAttributeEntry",
123125
"ExtendedAttributeError",
124126
"ExtendedAttributeHeader",
@@ -130,7 +132,6 @@
130132
"ExtentTail",
131133
"Fifo",
132134
"File",
133-
"FS_ENCRYPTION_MODE",
134135
"Hurd1",
135136
"Hurd2",
136137
"Inode",
@@ -141,7 +142,6 @@
141142
"MagicError",
142143
"Masix1",
143144
"Masix2",
144-
"MODE",
145145
"Osd1",
146146
"Osd2",
147147
"Socket",

‎ext4/_compat.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ def assert_cast(obj: Any, t: type[T], /) -> T:
4141
return obj
4242

4343

44-
__all__ = ["override", "ReadableStream", "PeekableStream", "assert_cast"]
44+
__all__ = ["PeekableStream", "ReadableStream", "assert_cast", "override"]

‎ext4/blockdescriptor.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class BlockDescriptor(Ext4Struct):
2525

2626
_pack_ = 1
2727
# _anonymous_ = ("bg_reserved",)
28-
_fields_ = [
28+
_fields_ = (
2929
("bg_block_bitmap_lo", c_uint32),
3030
("bg_inode_bitmap_lo", c_uint32),
3131
("bg_inode_table_lo", c_uint32),
@@ -49,7 +49,7 @@ class BlockDescriptor(Ext4Struct):
4949
("bg_block_bitmap_csum_hi", c_uint16),
5050
("bg_inode_bitmap_csum_hi", c_uint16),
5151
("bg_reserved", c_uint32),
52-
]
52+
)
5353

5454
def __init__(self, volume: "Volume", offset: int, bg_no: int) -> None:
5555
super().__init__(volume, offset)

‎ext4/directory.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ class DirectoryEntry(DirectoryEntryBase):
6565
__slots__ = ()
6666
_pack_ = 1
6767
# _anonymous_ = ("l_i_reserved",)
68-
_fields_ = [
68+
_fields_ = (
6969
("inode", c_uint32),
7070
("rec_len", c_uint16),
7171
("name_len", c_uint16),
7272
("name", c_char * EXT4_NAME_LEN),
73-
]
73+
)
7474

7575

7676
@final
7777
class DirectoryEntry2(DirectoryEntryBase):
7878
__slots__ = ()
7979
_pack_ = 1
8080
# _anonymous_ = ("l_i_reserved",)
81-
_fields_ = [
81+
_fields_ = (
8282
("inode", c_uint32),
8383
("rec_len", c_uint16),
8484
("name_len", c_uint8),
8585
("file_type", EXT4_FT.basetype),
8686
("name", c_char * EXT4_NAME_LEN),
87-
]
87+
)
8888

8989
@DirectoryEntryBase.is_fake_entry.getter
9090
def is_fake_entry(self) -> bool:
@@ -97,13 +97,13 @@ class DirectoryEntryTail(DirectoryEntryStruct):
9797
__slots__ = ()
9898
_pack_ = 1
9999
# _anonymous_ = ("det_reserved_zero1", "det_reserved_zero2",)
100-
_fields_ = [
100+
_fields_ = (
101101
("det_reserved_zero1", c_uint32),
102102
("det_rec_len", c_uint16),
103103
("det_reserved_zero2", c_uint8),
104104
("det_reserved_ft", c_uint8), # EXT4_FT.DIR_CSUM
105105
("det_checksum", c_uint32),
106-
]
106+
)
107107

108108
@Ext4Struct.magic.getter
109109
def magic(self) -> int:
@@ -120,7 +120,7 @@ class DirectoryEntryHash(DirectoryEntryStruct):
120120
__slots__ = ()
121121
_pack_ = 1
122122
# _anonymous_ = ("det_reserved_zero1", "det_reserved_zero2",)
123-
_fields_ = [
123+
_fields_ = (
124124
("hash", c_uint32),
125125
("minor_hash", c_uint32),
126-
]
126+
)

‎ext4/enum.py‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import (
77
TYPE_CHECKING,
88
Any,
9+
Self,
910
cast,
1011
final,
1112
)
@@ -17,8 +18,13 @@
1718

1819

1920
def TypedEnumerationType(_type: type["SimpleCData"]): # noqa: ANN201
20-
class EnumerationType(type(_type)): # type: ignore # pyright: ignore[reportGeneralTypeIssues, reportUntypedBaseClass] #noqa: ANN201
21-
def __new__(cls, name: str, bases: tuple[type, ...], data: dict[str, Any]): # pyright: ignore[reportExplicitAny, reportUnknownParameterType] # noqa: ANN204
21+
class EnumerationType(type(_type)): # type: ignore # pyright: ignore[reportGeneralTypeIssues, reportUntypedBaseClass]
22+
def __new__(
23+
cls,
24+
name: str,
25+
bases: tuple[type, ...],
26+
data: dict[str, Any], # pyright: ignore[reportExplicitAny]
27+
) -> Self:
2228
_members_: dict[str, Any] # pyright: ignore[reportExplicitAny]
2329
if "_members_" not in data:
2430
_members_ = {}
@@ -32,11 +38,7 @@ def __new__(cls, name: str, bases: tuple[type, ...], data: dict[str, Any]): # p
3238
_members_ = cast(dict[str, Any], data["_members_"]) # pyright: ignore[reportExplicitAny]
3339

3440
data["_reverse_map_"] = {v: k for k, v in _members_.items()} # pyright: ignore[reportAny]
35-
cls = type(_type).__new__(cls, name, bases, data) # pyright: ignore[reportCallIssue, reportUnknownVariableType] # noqa: PLW0642
36-
for key, value in cast(dict[str, Any], cls._members_).items(): # pyright: ignore[reportExplicitAny, reportAny]
37-
globals()[key] = value
38-
39-
return cls # pyright: ignore[reportUnknownVariableType]
41+
return type(_type).__new__(cls, name, bases, data) # pyright: ignore[reportCallIssue, reportUnknownVariableType]
4042

4143
@override
4244
def __repr__(self) -> str:
@@ -46,8 +48,8 @@ def __repr__(self) -> str:
4648

4749

4850
def TypedCEnumeration(_type: type["SimpleCData"]): # noqa: ANN201
49-
class CEnumeration(_type, metaclass=TypedEnumerationType(_type)): # pyright: ignore[reportGeneralTypeIssues, reportUntypedBaseClass] # noqa: ANN201,PLW1641,PLW1641
50-
_members_: dict[str, Any] = {} # pyright: ignore[reportExplicitAny]
51+
class CEnumeration(_type, metaclass=TypedEnumerationType(_type)): # pyright: ignore[reportGeneralTypeIssues, reportUntypedBaseClass] # noqa: PLW1641
52+
_members_: dict[str, Any] = {} # pyright: ignore[reportExplicitAny] # noqa: RUF012
5153
basetype: type["SimpleCData"] = _type
5254

5355
@override
@@ -56,18 +58,18 @@ def __repr__(self) -> str:
5658
return f"<{self.__class__.__name__}.{self._reverse_map_.get(value, '(unknown)')}: {value}>" # pyright: ignore[reportUnknownMemberType]
5759

5860
@override
59-
def __eq__(self, other: Any) -> bool: # pyright: ignore[reportExplicitAny, reportAny]
61+
def __eq__(self, other: object) -> bool:
6062
if isinstance(other, int):
6163
return self.value == other # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
6264

63-
return isinstance(self, type(other)) and self.value == other.value # pyright: ignore[reportUnknownVariableType, reportAny, reportUnknownMemberType]
65+
return isinstance(self, type(other)) and self.value == other.value # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType, reportAttributeAccessIssue]
6466

6567
@override
66-
def __ne__(self, other: Any) -> bool: # pyright: ignore[reportExplicitAny, reportAny]
68+
def __ne__(self, other: object) -> bool:
6769
if isinstance(other, int):
6870
return self.value != other # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
6971

70-
return isinstance(self, type(other)) and self.value != other.value # pyright: ignore[reportUnknownVariableType, reportAny, reportUnknownMemberType]
72+
return isinstance(self, type(other)) and self.value != other.value # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType, reportAttributeAccessIssue]
7173

7274
@override
7375
def __and__(self, other: Any) -> int | bool: # pyright: ignore[reportExplicitAny, reportAny, reportGeneralTypeIssues]

‎ext4/extent.py‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ class ExtentHeader(Ext4Struct):
8989

9090
_pack_ = 1
9191
# _anonymous_ = ()
92-
_fields_ = [
92+
_fields_ = (
9393
("eh_magic", c_uint16),
9494
("eh_entries", c_uint16),
9595
("eh_max", c_uint16),
9696
("eh_depth", c_uint16),
9797
("eh_generation", c_uint32),
98-
]
98+
)
9999

100100
def __init__(self, tree: "ExtentTree", offset: int) -> None:
101101
self.tree: ExtentTree = tree
@@ -106,7 +106,7 @@ def __init__(self, tree: "ExtentTree", offset: int) -> None:
106106

107107
offset = self.offset + self.size
108108
eh_entries = assert_cast(self.eh_entries, int) # pyright: ignore[reportAny]
109-
for i in range(0, eh_entries):
109+
for i in range(eh_entries):
110110
if self.eh_depth == 0:
111111
self.extents.append(Extent(self, offset, i))
112112
offset += sizeof(Extent)
@@ -173,12 +173,12 @@ class ExtentIndex(Ext4Struct):
173173

174174
_pack_ = 1
175175
# _anonymous_ = ("ei_unused",)
176-
_fields_ = [
176+
_fields_ = (
177177
("ei_block", c_uint32),
178178
("ei_leaf_lo", c_uint32),
179179
("ei_leaf_hi", c_uint16),
180180
("ei_unused", c_uint16),
181-
]
181+
)
182182

183183
def __init__(self, header: ExtentHeader, offset: int, ei_no: int) -> None:
184184
self.ei_no: int = ei_no
@@ -210,12 +210,12 @@ class Extent(Ext4Struct):
210210

211211
_pack_ = 1
212212
# _anonymous_ = ("ei_unused",)
213-
_fields_ = [
213+
_fields_ = (
214214
("ee_block", c_uint32),
215215
("ee_len", c_uint16),
216216
("ee_start_hi", c_uint16),
217217
("ee_start_lo", c_uint32),
218-
]
218+
)
219219

220220
def __init__(self, header: ExtentHeader, offset: int, ee_no: int) -> None:
221221
super().__init__(header.inode.volume, offset)
@@ -258,9 +258,7 @@ class ExtentTail(Ext4Struct):
258258
__slots__ = ("header",)
259259

260260
_pack_ = 1
261-
_fields_ = [
262-
("et_checksum", c_uint32),
263-
]
261+
_fields_ = (("et_checksum", c_uint32),)
264262

265263
def __init__(self, header: ExtentHeader, offset: int) -> None:
266264
self.header: ExtentHeader = header

‎ext4/htree.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ class DotDirectoryEntry2(LittleEndianStructureWithVolume):
5959

6060
_pack_ = 1
6161
# _anonymous_ = ()
62-
_fields_ = [
62+
_fields_ = (
6363
("inode", c_uint32),
6464
("rec_len", c_uint16),
6565
("name_len", c_uint8),
6666
("file_type", c_uint8),
6767
("name", c_char * 4), # b".\0\0\0" or b"..\0\0"
68-
]
68+
)
6969

7070
def verify(self) -> None:
7171
name = assert_cast(self.name, bytes) # pyright: ignore[reportAny]
@@ -89,13 +89,13 @@ class DXRootInfo(LittleEndianStructure):
8989
__slots__ = ()
9090
_pack_ = 1
9191
# _anonymous_ = ("reserved_zero")
92-
_fields_ = [
92+
_fields_ = (
9393
("reserved_zero", c_uint32),
9494
("hash_version", DX_HASH.basetype),
9595
("info_length", c_uint8),
9696
("indirect_levels", c_uint8),
9797
("unused_flags", c_uint8),
98-
]
98+
)
9999

100100

101101
class DXBase(Ext4Struct):
@@ -128,10 +128,10 @@ class DXEntry(DXBase):
128128

129129
_pack_ = 1
130130
# _anonymous_ = ("")
131-
_fields_ = [
131+
_fields_ = (
132132
("hash", c_uint32),
133133
("block", c_uint32),
134-
]
134+
)
135135

136136
def __init__(self, parent: "DXEntriesBase", index: int) -> None:
137137
self.index: int = index
@@ -321,15 +321,15 @@ def __init__(self, inode: "Directory") -> None:
321321
super().__init__(inode, 0)
322322

323323
# _anonymous_ = ("")
324-
_fields_ = [
324+
_fields_ = (
325325
("dot", DotDirectoryEntry2),
326326
("dotdot", DotDirectoryEntry2),
327327
("dx_root_info", DXRootInfo),
328328
("limit", c_uint16),
329329
("count", c_uint16),
330330
("block", c_uint32),
331331
# ("entries", DXEntry * self.count),
332-
]
332+
)
333333

334334
def compute_dx_hash(self, name: bytes, hash_version: int) -> tuple[int, int]:
335335
seed = self.directory.volume.superblock.s_hash_seed # pyright: ignore[reportAny]
@@ -511,10 +511,10 @@ class DXFake(LittleEndianStructure):
511511
__slots__ = ()
512512
_pack_ = 1
513513
# _anonymous_ = ("")
514-
_fields_ = [
514+
_fields_ = (
515515
("inode", c_uint32), # 0
516516
("rec_len", c_uint16),
517-
]
517+
)
518518

519519
@property
520520
def expected_magic(self) -> int:
@@ -532,15 +532,15 @@ class DXNode(DXEntriesBase):
532532

533533
_pack_ = 1
534534
# _anonymous_ = ("")
535-
_fields_ = [
535+
_fields_ = (
536536
("fake", DXFake),
537537
("name_len", c_uint8),
538538
("file_type", c_uint8),
539539
("limit", c_uint16),
540540
("count", c_uint16),
541541
("block", c_uint32),
542542
# ("entries", DXEntry * self.count),
543-
]
543+
)
544544

545545
def __init__(self, directory: "Directory", offset: int) -> None:
546546
super().__init__(directory, offset)
@@ -552,10 +552,10 @@ class DXTail(DXBase):
552552

553553
_pack_ = 1
554554
# _anonymous_ = ("dt_reserved")
555-
_fields_ = [
555+
_fields_ = (
556556
("dt_reserved", c_uint32),
557557
("dt_checksum", c_uint16),
558-
]
558+
)
559559

560560
def __init__(self, parent: DXNode) -> None:
561561
self.parent = parent

0 commit comments

Comments
 (0)