66from dataclasses import dataclass , field
77from enum import Enum
88from pathlib import Path
9- from typing import NotRequired , TypedDict , override
9+ from typing import NotRequired , Self , TypedDict , override
1010from uuid import UUID
1111
1212import parted
@@ -254,17 +254,17 @@ class Unit(Enum):
254254
255255 sectors = 'sectors' # size in sector
256256
257- @staticmethod
258- def get_all_units () -> list [str ]:
259- return [u .name for u in Unit ]
257+ @classmethod
258+ def get_all_units (cls ) -> list [str ]:
259+ return [u .name for u in cls ]
260260
261- @staticmethod
262- def get_si_units () -> list [Unit ]:
263- return [u for u in Unit if 'i' not in u .name and u .name != 'sectors' ]
261+ @classmethod
262+ def get_si_units (cls ) -> list [Self ]:
263+ return [u for u in cls if 'i' not in u .name and u .name != 'sectors' ]
264264
265- @staticmethod
266- def get_binary_units () -> list [Unit ]:
267- return [u for u in Unit if 'i' in u .name or u .name == 'B' ]
265+ @classmethod
266+ def get_binary_units (cls ) -> list [Self ]:
267+ return [u for u in cls if 'i' in u .name or u .name == 'B' ]
268268
269269
270270class _SectorSizeSerialization (TypedDict ):
@@ -282,9 +282,9 @@ def __post_init__(self) -> None:
282282 case Unit .sectors :
283283 raise ValueError ('Unit type sector not allowed for SectorSize' )
284284
285- @staticmethod
286- def default () -> SectorSize :
287- return SectorSize (512 , Unit .B )
285+ @classmethod
286+ def default (cls ) -> Self :
287+ return cls (512 , Unit .B )
288288
289289 def json (self ) -> _SectorSizeSerialization :
290290 return {
@@ -1087,15 +1087,15 @@ def json(self) -> _LvmVolumeGroupSerialization:
10871087 'volumes' : [vol .json () for vol in self .volumes ],
10881088 }
10891089
1090- @staticmethod
1091- def parse_arg (arg : _LvmVolumeGroupSerialization , disk_config : DiskLayoutConfiguration ) -> LvmVolumeGroup :
1090+ @classmethod
1091+ def parse_arg (cls , arg : _LvmVolumeGroupSerialization , disk_config : DiskLayoutConfiguration ) -> Self :
10921092 lvm_pvs = []
10931093 for mod in disk_config .device_modifications :
10941094 for part in mod .partitions :
10951095 if part .obj_id in arg .get ('lvm_pvs' , []):
10961096 lvm_pvs .append (part )
10971097
1098- return LvmVolumeGroup (
1098+ return cls (
10991099 arg ['name' ],
11001100 lvm_pvs ,
11011101 [LvmVolume .parse_arg (vol ) for vol in arg ['volumes' ]],
@@ -1191,9 +1191,9 @@ def relative_mountpoint(self) -> Path:
11911191
11921192 raise ValueError ('Mountpoint is not specified' )
11931193
1194- @staticmethod
1195- def parse_arg (arg : _LvmVolumeSerialization ) -> LvmVolume :
1196- volume = LvmVolume (
1194+ @classmethod
1195+ def parse_arg (cls , arg : _LvmVolumeSerialization ) -> Self :
1196+ volume = cls (
11971197 status = LvmVolumeStatus (arg ['status' ]),
11981198 name = arg ['name' ],
11991199 fs_type = FilesystemType (arg ['fs_type' ]),
@@ -1296,16 +1296,16 @@ def json(self) -> _LvmConfigurationSerialization:
12961296 'vol_groups' : [vol_gr .json () for vol_gr in self .vol_groups ],
12971297 }
12981298
1299- @staticmethod
1300- def parse_arg (arg : _LvmConfigurationSerialization , disk_config : DiskLayoutConfiguration ) -> LvmConfiguration :
1299+ @classmethod
1300+ def parse_arg (cls , arg : _LvmConfigurationSerialization , disk_config : DiskLayoutConfiguration ) -> Self :
13011301 lvm_pvs = []
13021302 for mod in disk_config .device_modifications :
13031303 for part in mod .partitions :
13041304 # FIXME: 'lvm_pvs' does not seem like it can ever exist in the 'arg' serialization
13051305 if part .obj_id in arg .get ('lvm_pvs' , []): # type: ignore[operator]
13061306 lvm_pvs .append (part )
13071307
1308- return LvmConfiguration (
1308+ return cls (
13091309 config_type = LvmLayoutType (arg ['config_type' ]),
13101310 vol_groups = [LvmVolumeGroup .parse_arg (vol_group , disk_config ) for vol_group in arg ['vol_groups' ]],
13111311 )
@@ -1354,9 +1354,9 @@ class SnapshotConfig:
13541354 def json (self ) -> _SnapshotConfigSerialization :
13551355 return {'type' : self .snapshot_type .value }
13561356
1357- @staticmethod
1358- def parse_args (args : _SnapshotConfigSerialization ) -> SnapshotConfig :
1359- return SnapshotConfig (SnapshotType (args ['type' ]))
1357+ @classmethod
1358+ def parse_args (cls , args : _SnapshotConfigSerialization ) -> Self :
1359+ return cls (SnapshotType (args ['type' ]))
13601360
13611361
13621362@dataclass
@@ -1366,12 +1366,12 @@ class BtrfsOptions:
13661366 def json (self ) -> _BtrfsOptionsSerialization :
13671367 return {'snapshot_config' : self .snapshot_config .json () if self .snapshot_config else None }
13681368
1369- @staticmethod
1370- def parse_arg (arg : _BtrfsOptionsSerialization ) -> BtrfsOptions | None :
1369+ @classmethod
1370+ def parse_arg (cls , arg : _BtrfsOptionsSerialization ) -> Self | None :
13711371 snapshot_args = arg .get ('snapshot_config' )
13721372 if snapshot_args :
13731373 snapshot_config = SnapshotConfig .parse_args (snapshot_args )
1374- return BtrfsOptions (snapshot_config )
1374+ return cls (snapshot_config )
13751375
13761376 return None
13771377
0 commit comments