Skip to content

Commit 3374b47

Browse files
authored
Use Self for return instances of cls (archlinux#4116)
1 parent bde3b0e commit 3374b47

10 files changed

Lines changed: 39 additions & 38 deletions

File tree

archinstall/lib/args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from dataclasses import dataclass, field
88
from importlib.metadata import version
99
from pathlib import Path
10-
from typing import Any
10+
from typing import Any, Self
1111
from urllib.request import Request, urlopen
1212

1313
from pydantic.dataclasses import dataclass as p_dataclass
@@ -131,7 +131,7 @@ def safe_json(self) -> dict[str, Any]:
131131
return config
132132

133133
@classmethod
134-
def from_config(cls, args_config: dict[str, Any], args: Arguments) -> 'ArchConfig':
134+
def from_config(cls, args_config: dict[str, Any], args: Arguments) -> Self:
135135
arch_config = cls()
136136

137137
arch_config.locale_config = LocaleConfiguration.parse_arg(args_config)

archinstall/lib/hardware.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from enum import Enum
33
from functools import cached_property
44
from pathlib import Path
5+
from typing import Self
56

67
from .exceptions import SysCallError
78
from .general import SysCommand
@@ -16,7 +17,7 @@ class CpuVendor(Enum):
1617
_Unknown = 'unknown'
1718

1819
@classmethod
19-
def get_vendor(cls, name: str) -> 'CpuVendor':
20+
def get_vendor(cls, name: str) -> Self:
2021
if vendor := getattr(cls, name, None):
2122
return vendor
2223
else:

archinstall/lib/models/bootloader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from dataclasses import dataclass
55
from enum import Enum
6-
from typing import Any
6+
from typing import Any, Self
77

88
from archinstall.lib.translationhandler import tr
99

@@ -33,7 +33,7 @@ def json(self) -> str:
3333
return self.value
3434

3535
@classmethod
36-
def get_default(cls) -> Bootloader:
36+
def get_default(cls) -> Self:
3737
from ..args import arch_config_handler
3838

3939
if arch_config_handler.args.skip_boot:
@@ -44,7 +44,7 @@ def get_default(cls) -> Bootloader:
4444
return cls.Grub
4545

4646
@classmethod
47-
def from_arg(cls, bootloader: str, skip_boot: bool) -> Bootloader:
47+
def from_arg(cls, bootloader: str, skip_boot: bool) -> Self:
4848
# to support old configuration files
4949
bootloader = bootloader.capitalize()
5050

@@ -68,14 +68,14 @@ def json(self) -> dict[str, Any]:
6868
return {'bootloader': self.bootloader.json(), 'uki': self.uki, 'removable': self.removable}
6969

7070
@classmethod
71-
def parse_arg(cls, config: dict[str, Any], skip_boot: bool) -> BootloaderConfiguration:
71+
def parse_arg(cls, config: dict[str, Any], skip_boot: bool) -> Self:
7272
bootloader = Bootloader.from_arg(config.get('bootloader', ''), skip_boot)
7373
uki = config.get('uki', False)
7474
removable = config.get('removable', True)
7575
return cls(bootloader=bootloader, uki=uki, removable=removable)
7676

7777
@classmethod
78-
def get_default(cls) -> BootloaderConfiguration:
78+
def get_default(cls) -> Self:
7979
bootloader = Bootloader.get_default()
8080
removable = SysInfo.has_uefi() and bootloader.has_removable_support()
8181
uki = SysInfo.has_uefi() and bootloader.has_uki_support()

archinstall/lib/models/device.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def parse_arg(
8686
cls,
8787
disk_config: _DiskLayoutConfigurationSerialization,
8888
enc_password: Password | None = None,
89-
) -> DiskLayoutConfiguration | None:
89+
) -> Self | None:
9090
from archinstall.lib.disk.device_handler import device_handler
9191

9292
device_modifications: list[DeviceModification] = []
@@ -223,7 +223,7 @@ def is_mbr(self) -> bool:
223223
return self == PartitionTable.MBR
224224

225225
@classmethod
226-
def default(cls) -> PartitionTable:
226+
def default(cls) -> Self:
227227
return cls.GPT if SysInfo.has_uefi() else cls.MBR
228228

229229

@@ -293,7 +293,7 @@ def json(self) -> _SectorSizeSerialization:
293293
}
294294

295295
@classmethod
296-
def parse_args(cls, arg: _SectorSizeSerialization) -> SectorSize:
296+
def parse_args(cls, arg: _SectorSizeSerialization) -> Self:
297297
return cls(
298298
arg['value'],
299299
Unit[arg['unit']],
@@ -330,7 +330,7 @@ def json(self) -> _SizeSerialization:
330330
}
331331

332332
@classmethod
333-
def parse_args(cls, size_arg: _SizeSerialization) -> Size:
333+
def parse_args(cls, size_arg: _SizeSerialization) -> Self:
334334
sector_size = size_arg['sector_size']
335335

336336
return cls(
@@ -537,7 +537,7 @@ def from_partition(
537537
lsblk_info: LsblkInfo,
538538
fs_type: FilesystemType | None,
539539
btrfs_subvol_infos: list[_BtrfsSubvolumeInfo] = [],
540-
) -> _PartitionInfo:
540+
) -> Self:
541541
partition_type = PartitionType.get_type_from_code(partition.type)
542542
flags = [f for f in PartitionFlag if partition.getFlag(f.flag_id)]
543543

@@ -595,7 +595,7 @@ def table_data(self) -> dict[str, str | int | bool]:
595595
}
596596

597597
@classmethod
598-
def from_disk(cls, disk: Disk) -> _DeviceInfo:
598+
def from_disk(cls, disk: Disk) -> Self:
599599
device = disk.device
600600
if device.type == 18:
601601
device_type = 'loop'
@@ -631,11 +631,11 @@ class SubvolumeModification:
631631
mountpoint: Path | None = None
632632

633633
@classmethod
634-
def from_existing_subvol_info(cls, info: _BtrfsSubvolumeInfo) -> SubvolumeModification:
634+
def from_existing_subvol_info(cls, info: _BtrfsSubvolumeInfo) -> Self:
635635
return cls(info.name, mountpoint=info.mountpoint)
636636

637637
@classmethod
638-
def parse_args(cls, subvol_args: list[_SubvolumeModificationSerialization]) -> list[SubvolumeModification]:
638+
def parse_args(cls, subvol_args: list[_SubvolumeModificationSerialization]) -> list[Self]:
639639
mods = []
640640
for entry in subvol_args:
641641
if not entry.get('name', None) or not entry.get('mountpoint', None):
@@ -721,7 +721,7 @@ class PartitionType(Enum):
721721
_Unknown = 'unknown'
722722

723723
@classmethod
724-
def get_type_from_code(cls, code: int) -> PartitionType:
724+
def get_type_from_code(cls, code: int) -> Self:
725725
if code == parted.PARTITION_NORMAL:
726726
return cls.Primary
727727
else:
@@ -754,7 +754,7 @@ def description(self) -> str:
754754
return self.alias or self.name.lower()
755755

756756
@classmethod
757-
def from_string(cls, s: str) -> PartitionFlag | None:
757+
def from_string(cls, s: str) -> Self | None:
758758
s = s.lower()
759759

760760
for partition_flag in cls:
@@ -911,7 +911,7 @@ def safe_fs_type(self) -> FilesystemType:
911911
return self.fs_type
912912

913913
@classmethod
914-
def from_existing_partition(cls, partition_info: _PartitionInfo) -> PartitionModification:
914+
def from_existing_partition(cls, partition_info: _PartitionInfo) -> Self:
915915
if partition_info.btrfs_subvol_infos:
916916
mountpoint = None
917917
subvol_mods = []
@@ -1431,7 +1431,7 @@ class EncryptionType(Enum):
14311431
LuksOnLvm = 'luks_on_lvm'
14321432

14331433
@classmethod
1434-
def _encryption_type_mapper(cls) -> dict[str, 'EncryptionType']:
1434+
def _encryption_type_mapper(cls) -> dict[str, Self]:
14351435
return {
14361436
tr('No Encryption'): cls.NoEncryption,
14371437
tr('LUKS'): cls.Luks,
@@ -1440,7 +1440,7 @@ def _encryption_type_mapper(cls) -> dict[str, 'EncryptionType']:
14401440
}
14411441

14421442
@classmethod
1443-
def text_to_type(cls, text: str) -> 'EncryptionType':
1443+
def text_to_type(cls, text: str) -> Self:
14441444
mapping = cls._encryption_type_mapper()
14451445
return mapping[text]
14461446

@@ -1518,7 +1518,7 @@ def parse_arg(
15181518
disk_config: DiskLayoutConfiguration,
15191519
disk_encryption: _DiskEncryptionSerialization,
15201520
password: Password | None = None,
1521-
) -> 'DiskEncryption | None':
1521+
) -> Self | None:
15221522
if not cls.validate_enc(disk_config.device_modifications, disk_config.lvm_config):
15231523
return None
15241524

@@ -1580,7 +1580,7 @@ def table_data(self) -> dict[str, str]:
15801580
}
15811581

15821582
@classmethod
1583-
def parse_arg(cls, arg: _Fido2DeviceSerialization) -> 'Fido2Device':
1583+
def parse_arg(cls, arg: _Fido2DeviceSerialization) -> Self:
15841584
return cls(
15851585
Path(arg['path']),
15861586
arg['manufacturer'],

archinstall/lib/models/locale.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def _load_config(self, args: dict[str, str]) -> None:
4141
self.kb_layout = args['kb_layout']
4242

4343
@classmethod
44-
def parse_arg(cls, args: dict[str, Any]) -> 'LocaleConfiguration':
44+
def parse_arg(cls, args: dict[str, Any]) -> Self:
4545
default = cls.default()
4646

4747
if 'locale_config' in args:

archinstall/lib/models/mirrors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import urllib.request
66
from dataclasses import dataclass, field
77
from enum import Enum
8-
from typing import Any, TypedDict, override
8+
from typing import Any, Self, TypedDict, override
99

1010
from pydantic import BaseModel, field_validator, model_validator
1111

@@ -191,7 +191,7 @@ def json(self) -> _CustomRepositorySerialization:
191191
}
192192

193193
@classmethod
194-
def parse_args(cls, args: list[dict[str, str]]) -> list['CustomRepository']:
194+
def parse_args(cls, args: list[dict[str, str]]) -> list[Self]:
195195
configs = []
196196
for arg in args:
197197
configs.append(
@@ -217,7 +217,7 @@ def json(self) -> dict[str, str]:
217217
return {'url': self.url}
218218

219219
@classmethod
220-
def parse_args(cls, args: list[dict[str, str]]) -> list['CustomServer']:
220+
def parse_args(cls, args: list[dict[str, str]]) -> list[Self]:
221221
configs = []
222222
for arg in args:
223223
configs.append(
@@ -304,7 +304,7 @@ def parse_args(
304304
cls,
305305
args: dict[str, Any],
306306
backwards_compatible_repo: list[Repository] = [],
307-
) -> 'MirrorConfiguration':
307+
) -> Self:
308308
config = cls()
309309

310310
mirror_regions = args.get('mirror_regions', [])

archinstall/lib/models/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class WifiConfiguredNetwork:
226226
flags: list[str]
227227

228228
@classmethod
229-
def from_wpa_cli_output(cls, list_networks: str) -> list[WifiConfiguredNetwork]:
229+
def from_wpa_cli_output(cls, list_networks: str) -> list[Self]:
230230
"""
231231
Example output from 'wpa_cli list_networks'
232232

archinstall/lib/models/packages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ class PackageGroup:
152152
def from_available_packages(
153153
cls,
154154
packages: dict[str, AvailablePackage],
155-
) -> dict[str, 'PackageGroup']:
156-
pkg_groups: dict[str, 'PackageGroup'] = {}
155+
) -> dict[str, Self]:
156+
pkg_groups: dict[str, Self] = {}
157157

158158
for pkg in packages.values():
159159
if 'None' in pkg.groups:

archinstall/lib/models/profile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, TypedDict
4+
from typing import TYPE_CHECKING, Self, TypedDict
55

66
from archinstall.default_profiles.profile import GreeterType, Profile
77

@@ -33,7 +33,7 @@ def json(self) -> _ProfileConfigurationSerialization:
3333
}
3434

3535
@classmethod
36-
def parse_arg(cls, arg: _ProfileConfigurationSerialization) -> 'ProfileConfiguration':
36+
def parse_arg(cls, arg: _ProfileConfigurationSerialization) -> Self:
3737
from ..profile.profiles_handler import profile_handler
3838

3939
profile = profile_handler.parse_profile_config(arg['profile'])

archinstall/lib/models/users.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass, field
22
from enum import Enum
3-
from typing import NotRequired, TypedDict, override
3+
from typing import NotRequired, Self, TypedDict, override
44

55
from archinstall.lib.translationhandler import tr
66

@@ -38,7 +38,7 @@ def color(self) -> str:
3838
return 'green'
3939

4040
@classmethod
41-
def strength(cls, password: str) -> 'PasswordStrength':
41+
def strength(cls, password: str) -> Self:
4242
digit = any(character.isdigit() for character in password)
4343
upper = any(character.isupper() for character in password)
4444
lower = any(character.islower() for character in password)
@@ -53,7 +53,7 @@ def _check_password_strength(
5353
lower: bool,
5454
symbol: bool,
5555
length: int,
56-
) -> 'PasswordStrength':
56+
) -> Self:
5757
# suggested evaluation
5858
# https://github.com/archlinux/archinstall/issues/1304#issuecomment-1146768163
5959
if digit and upper and lower and symbol:
@@ -185,8 +185,8 @@ def json(self) -> UserSerialization:
185185
def parse_arguments(
186186
cls,
187187
args: list[UserSerialization],
188-
) -> list['User']:
189-
users: list[User] = []
188+
) -> list[Self]:
189+
users = []
190190

191191
for entry in args:
192192
username = entry.get('username')

0 commit comments

Comments
 (0)