Use Self for return instances of cls (#4116)
This commit is contained in:
parent
bde3b0ed6e
commit
3374b47d50
|
|
@ -7,7 +7,7 @@ from argparse import ArgumentParser, Namespace
|
|||
from dataclasses import dataclass, field
|
||||
from importlib.metadata import version
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from typing import Any, Self
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from pydantic.dataclasses import dataclass as p_dataclass
|
||||
|
|
@ -131,7 +131,7 @@ class ArchConfig:
|
|||
return config
|
||||
|
||||
@classmethod
|
||||
def from_config(cls, args_config: dict[str, Any], args: Arguments) -> 'ArchConfig':
|
||||
def from_config(cls, args_config: dict[str, Any], args: Arguments) -> Self:
|
||||
arch_config = cls()
|
||||
|
||||
arch_config.locale_config = LocaleConfiguration.parse_arg(args_config)
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import os
|
|||
from enum import Enum
|
||||
from functools import cached_property
|
||||
from pathlib import Path
|
||||
from typing import Self
|
||||
|
||||
from .exceptions import SysCallError
|
||||
from .general import SysCommand
|
||||
|
|
@ -16,7 +17,7 @@ class CpuVendor(Enum):
|
|||
_Unknown = 'unknown'
|
||||
|
||||
@classmethod
|
||||
def get_vendor(cls, name: str) -> 'CpuVendor':
|
||||
def get_vendor(cls, name: str) -> Self:
|
||||
if vendor := getattr(cls, name, None):
|
||||
return vendor
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import sys
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
from typing import Any, Self
|
||||
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ class Bootloader(Enum):
|
|||
return self.value
|
||||
|
||||
@classmethod
|
||||
def get_default(cls) -> Bootloader:
|
||||
def get_default(cls) -> Self:
|
||||
from ..args import arch_config_handler
|
||||
|
||||
if arch_config_handler.args.skip_boot:
|
||||
|
|
@ -44,7 +44,7 @@ class Bootloader(Enum):
|
|||
return cls.Grub
|
||||
|
||||
@classmethod
|
||||
def from_arg(cls, bootloader: str, skip_boot: bool) -> Bootloader:
|
||||
def from_arg(cls, bootloader: str, skip_boot: bool) -> Self:
|
||||
# to support old configuration files
|
||||
bootloader = bootloader.capitalize()
|
||||
|
||||
|
|
@ -68,14 +68,14 @@ class BootloaderConfiguration:
|
|||
return {'bootloader': self.bootloader.json(), 'uki': self.uki, 'removable': self.removable}
|
||||
|
||||
@classmethod
|
||||
def parse_arg(cls, config: dict[str, Any], skip_boot: bool) -> BootloaderConfiguration:
|
||||
def parse_arg(cls, config: dict[str, Any], skip_boot: bool) -> Self:
|
||||
bootloader = Bootloader.from_arg(config.get('bootloader', ''), skip_boot)
|
||||
uki = config.get('uki', False)
|
||||
removable = config.get('removable', True)
|
||||
return cls(bootloader=bootloader, uki=uki, removable=removable)
|
||||
|
||||
@classmethod
|
||||
def get_default(cls) -> BootloaderConfiguration:
|
||||
def get_default(cls) -> Self:
|
||||
bootloader = Bootloader.get_default()
|
||||
removable = SysInfo.has_uefi() and bootloader.has_removable_support()
|
||||
uki = SysInfo.has_uefi() and bootloader.has_uki_support()
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@ class DiskLayoutConfiguration:
|
|||
cls,
|
||||
disk_config: _DiskLayoutConfigurationSerialization,
|
||||
enc_password: Password | None = None,
|
||||
) -> DiskLayoutConfiguration | None:
|
||||
) -> Self | None:
|
||||
from archinstall.lib.disk.device_handler import device_handler
|
||||
|
||||
device_modifications: list[DeviceModification] = []
|
||||
|
|
@ -223,7 +223,7 @@ class PartitionTable(Enum):
|
|||
return self == PartitionTable.MBR
|
||||
|
||||
@classmethod
|
||||
def default(cls) -> PartitionTable:
|
||||
def default(cls) -> Self:
|
||||
return cls.GPT if SysInfo.has_uefi() else cls.MBR
|
||||
|
||||
|
||||
|
|
@ -293,7 +293,7 @@ class SectorSize:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def parse_args(cls, arg: _SectorSizeSerialization) -> SectorSize:
|
||||
def parse_args(cls, arg: _SectorSizeSerialization) -> Self:
|
||||
return cls(
|
||||
arg['value'],
|
||||
Unit[arg['unit']],
|
||||
|
|
@ -330,7 +330,7 @@ class Size:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def parse_args(cls, size_arg: _SizeSerialization) -> Size:
|
||||
def parse_args(cls, size_arg: _SizeSerialization) -> Self:
|
||||
sector_size = size_arg['sector_size']
|
||||
|
||||
return cls(
|
||||
|
|
@ -537,7 +537,7 @@ class _PartitionInfo:
|
|||
lsblk_info: LsblkInfo,
|
||||
fs_type: FilesystemType | None,
|
||||
btrfs_subvol_infos: list[_BtrfsSubvolumeInfo] = [],
|
||||
) -> _PartitionInfo:
|
||||
) -> Self:
|
||||
partition_type = PartitionType.get_type_from_code(partition.type)
|
||||
flags = [f for f in PartitionFlag if partition.getFlag(f.flag_id)]
|
||||
|
||||
|
|
@ -595,7 +595,7 @@ class _DeviceInfo:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def from_disk(cls, disk: Disk) -> _DeviceInfo:
|
||||
def from_disk(cls, disk: Disk) -> Self:
|
||||
device = disk.device
|
||||
if device.type == 18:
|
||||
device_type = 'loop'
|
||||
|
|
@ -631,11 +631,11 @@ class SubvolumeModification:
|
|||
mountpoint: Path | None = None
|
||||
|
||||
@classmethod
|
||||
def from_existing_subvol_info(cls, info: _BtrfsSubvolumeInfo) -> SubvolumeModification:
|
||||
def from_existing_subvol_info(cls, info: _BtrfsSubvolumeInfo) -> Self:
|
||||
return cls(info.name, mountpoint=info.mountpoint)
|
||||
|
||||
@classmethod
|
||||
def parse_args(cls, subvol_args: list[_SubvolumeModificationSerialization]) -> list[SubvolumeModification]:
|
||||
def parse_args(cls, subvol_args: list[_SubvolumeModificationSerialization]) -> list[Self]:
|
||||
mods = []
|
||||
for entry in subvol_args:
|
||||
if not entry.get('name', None) or not entry.get('mountpoint', None):
|
||||
|
|
@ -721,7 +721,7 @@ class PartitionType(Enum):
|
|||
_Unknown = 'unknown'
|
||||
|
||||
@classmethod
|
||||
def get_type_from_code(cls, code: int) -> PartitionType:
|
||||
def get_type_from_code(cls, code: int) -> Self:
|
||||
if code == parted.PARTITION_NORMAL:
|
||||
return cls.Primary
|
||||
else:
|
||||
|
|
@ -754,7 +754,7 @@ class PartitionFlag(PartitionFlagDataMixin, Enum):
|
|||
return self.alias or self.name.lower()
|
||||
|
||||
@classmethod
|
||||
def from_string(cls, s: str) -> PartitionFlag | None:
|
||||
def from_string(cls, s: str) -> Self | None:
|
||||
s = s.lower()
|
||||
|
||||
for partition_flag in cls:
|
||||
|
|
@ -911,7 +911,7 @@ class PartitionModification:
|
|||
return self.fs_type
|
||||
|
||||
@classmethod
|
||||
def from_existing_partition(cls, partition_info: _PartitionInfo) -> PartitionModification:
|
||||
def from_existing_partition(cls, partition_info: _PartitionInfo) -> Self:
|
||||
if partition_info.btrfs_subvol_infos:
|
||||
mountpoint = None
|
||||
subvol_mods = []
|
||||
|
|
@ -1431,7 +1431,7 @@ class EncryptionType(Enum):
|
|||
LuksOnLvm = 'luks_on_lvm'
|
||||
|
||||
@classmethod
|
||||
def _encryption_type_mapper(cls) -> dict[str, 'EncryptionType']:
|
||||
def _encryption_type_mapper(cls) -> dict[str, Self]:
|
||||
return {
|
||||
tr('No Encryption'): cls.NoEncryption,
|
||||
tr('LUKS'): cls.Luks,
|
||||
|
|
@ -1440,7 +1440,7 @@ class EncryptionType(Enum):
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def text_to_type(cls, text: str) -> 'EncryptionType':
|
||||
def text_to_type(cls, text: str) -> Self:
|
||||
mapping = cls._encryption_type_mapper()
|
||||
return mapping[text]
|
||||
|
||||
|
|
@ -1518,7 +1518,7 @@ class DiskEncryption:
|
|||
disk_config: DiskLayoutConfiguration,
|
||||
disk_encryption: _DiskEncryptionSerialization,
|
||||
password: Password | None = None,
|
||||
) -> 'DiskEncryption | None':
|
||||
) -> Self | None:
|
||||
if not cls.validate_enc(disk_config.device_modifications, disk_config.lvm_config):
|
||||
return None
|
||||
|
||||
|
|
@ -1580,7 +1580,7 @@ class Fido2Device:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _Fido2DeviceSerialization) -> 'Fido2Device':
|
||||
def parse_arg(cls, arg: _Fido2DeviceSerialization) -> Self:
|
||||
return cls(
|
||||
Path(arg['path']),
|
||||
arg['manufacturer'],
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class LocaleConfiguration:
|
|||
self.kb_layout = args['kb_layout']
|
||||
|
||||
@classmethod
|
||||
def parse_arg(cls, args: dict[str, Any]) -> 'LocaleConfiguration':
|
||||
def parse_arg(cls, args: dict[str, Any]) -> Self:
|
||||
default = cls.default()
|
||||
|
||||
if 'locale_config' in args:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import urllib.parse
|
|||
import urllib.request
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, TypedDict, override
|
||||
from typing import Any, Self, TypedDict, override
|
||||
|
||||
from pydantic import BaseModel, field_validator, model_validator
|
||||
|
||||
|
|
@ -191,7 +191,7 @@ class CustomRepository:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def parse_args(cls, args: list[dict[str, str]]) -> list['CustomRepository']:
|
||||
def parse_args(cls, args: list[dict[str, str]]) -> list[Self]:
|
||||
configs = []
|
||||
for arg in args:
|
||||
configs.append(
|
||||
|
|
@ -217,7 +217,7 @@ class CustomServer:
|
|||
return {'url': self.url}
|
||||
|
||||
@classmethod
|
||||
def parse_args(cls, args: list[dict[str, str]]) -> list['CustomServer']:
|
||||
def parse_args(cls, args: list[dict[str, str]]) -> list[Self]:
|
||||
configs = []
|
||||
for arg in args:
|
||||
configs.append(
|
||||
|
|
@ -304,7 +304,7 @@ class MirrorConfiguration:
|
|||
cls,
|
||||
args: dict[str, Any],
|
||||
backwards_compatible_repo: list[Repository] = [],
|
||||
) -> 'MirrorConfiguration':
|
||||
) -> Self:
|
||||
config = cls()
|
||||
|
||||
mirror_regions = args.get('mirror_regions', [])
|
||||
|
|
|
|||
|
|
@ -226,7 +226,7 @@ class WifiConfiguredNetwork:
|
|||
flags: list[str]
|
||||
|
||||
@classmethod
|
||||
def from_wpa_cli_output(cls, list_networks: str) -> list[WifiConfiguredNetwork]:
|
||||
def from_wpa_cli_output(cls, list_networks: str) -> list[Self]:
|
||||
"""
|
||||
Example output from 'wpa_cli list_networks'
|
||||
|
||||
|
|
|
|||
|
|
@ -152,8 +152,8 @@ class PackageGroup:
|
|||
def from_available_packages(
|
||||
cls,
|
||||
packages: dict[str, AvailablePackage],
|
||||
) -> dict[str, 'PackageGroup']:
|
||||
pkg_groups: dict[str, 'PackageGroup'] = {}
|
||||
) -> dict[str, Self]:
|
||||
pkg_groups: dict[str, Self] = {}
|
||||
|
||||
for pkg in packages.values():
|
||||
if 'None' in pkg.groups:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, TypedDict
|
||||
from typing import TYPE_CHECKING, Self, TypedDict
|
||||
|
||||
from archinstall.default_profiles.profile import GreeterType, Profile
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ class ProfileConfiguration:
|
|||
}
|
||||
|
||||
@classmethod
|
||||
def parse_arg(cls, arg: _ProfileConfigurationSerialization) -> 'ProfileConfiguration':
|
||||
def parse_arg(cls, arg: _ProfileConfigurationSerialization) -> Self:
|
||||
from ..profile.profiles_handler import profile_handler
|
||||
|
||||
profile = profile_handler.parse_profile_config(arg['profile'])
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import NotRequired, TypedDict, override
|
||||
from typing import NotRequired, Self, TypedDict, override
|
||||
|
||||
from archinstall.lib.translationhandler import tr
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ class PasswordStrength(Enum):
|
|||
return 'green'
|
||||
|
||||
@classmethod
|
||||
def strength(cls, password: str) -> 'PasswordStrength':
|
||||
def strength(cls, password: str) -> Self:
|
||||
digit = any(character.isdigit() for character in password)
|
||||
upper = any(character.isupper() for character in password)
|
||||
lower = any(character.islower() for character in password)
|
||||
|
|
@ -53,7 +53,7 @@ class PasswordStrength(Enum):
|
|||
lower: bool,
|
||||
symbol: bool,
|
||||
length: int,
|
||||
) -> 'PasswordStrength':
|
||||
) -> Self:
|
||||
# suggested evaluation
|
||||
# https://github.com/archlinux/archinstall/issues/1304#issuecomment-1146768163
|
||||
if digit and upper and lower and symbol:
|
||||
|
|
@ -185,8 +185,8 @@ class User:
|
|||
def parse_arguments(
|
||||
cls,
|
||||
args: list[UserSerialization],
|
||||
) -> list['User']:
|
||||
users: list[User] = []
|
||||
) -> list[Self]:
|
||||
users = []
|
||||
|
||||
for entry in args:
|
||||
username = entry.get('username')
|
||||
|
|
|
|||
Loading…
Reference in New Issue