Skip to content

Commit

Permalink
Extend the mypy checks (archlinux#2120)
Browse files Browse the repository at this point in the history
* Extend the mypy checks

* Update

* Update

* Update

---------

Co-authored-by: Daniel Girtler <[email protected]>
  • Loading branch information
svartkanin and svartkanin authored Oct 3, 2023
1 parent 5c903df commit edbc135
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 30 deletions.
4 changes: 2 additions & 2 deletions archinstall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def load_config():
if arguments.get('servers', None) is not None:
storage['_selected_servers'] = arguments.get('servers', None)

if arguments.get('network_config', None) is not None:
config = NetworkConfiguration.parse_arg(arguments.get('network_config'))
if (net_config := arguments.get('network_config', None)) is not None:
config = NetworkConfiguration.parse_arg(net_config)
arguments['network_config'] = config

if arguments.get('!users', None) is not None or arguments.get('!superusers', None) is not None:
Expand Down
12 changes: 6 additions & 6 deletions archinstall/lib/disk/device_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ class _PartitionInfo:
start: Size
length: Size
flags: List[PartitionFlag]
partn: int
partuuid: str
uuid: str
partn: Optional[int]
partuuid: Optional[str]
uuid: Optional[str]
disk: Disk
mountpoints: List[Path]
btrfs_subvol_infos: List[_BtrfsSubvolumeInfo] = field(default_factory=list)
Expand Down Expand Up @@ -344,9 +344,9 @@ def from_partition(
cls,
partition: Partition,
fs_type: Optional[FilesystemType],
partn: int,
partuuid: str,
uuid: str,
partn: Optional[int],
partuuid: Optional[str],
uuid: Optional[str],
mountpoints: List[Path],
btrfs_subvol_infos: List[_BtrfsSubvolumeInfo] = []
) -> _PartitionInfo:
Expand Down
7 changes: 4 additions & 3 deletions archinstall/lib/disk/encryption_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ..disk import (
DeviceModification,
DiskLayoutConfiguration,
PartitionModification,
DiskEncryption,
EncryptionType
Expand All @@ -26,7 +27,7 @@
class DiskEncryptionMenu(AbstractSubMenu):
def __init__(
self,
mods: List[DeviceModification],
disk_config: DiskLayoutConfiguration,
data_store: Dict[str, Any],
preset: Optional[DiskEncryption] = None
):
Expand All @@ -35,7 +36,7 @@ def __init__(
else:
self._preset = DiskEncryption()

self._modifications = mods
self._disk_config = disk_config
super().__init__(data_store=data_store)

def setup_selection_menu_options(self):
Expand All @@ -59,7 +60,7 @@ def setup_selection_menu_options(self):
self._menu_options['partitions'] = \
Selector(
_('Partitions'),
func=lambda preset: select_partitions_to_encrypt(self._modifications.device_modifications, preset),
func=lambda preset: select_partitions_to_encrypt(self._disk_config.device_modifications, preset),
display_func=lambda x: f'{len(x)} {_("Partitions")}' if x else None,
dependencies=['encryption_password'],
default=self._preset.partitions,
Expand Down
11 changes: 7 additions & 4 deletions archinstall/lib/global_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,11 @@ def setup_selection_menu_options(self):
self._menu_options['abort'] = Selector(_('Abort'), exec_func=lambda n,v:exit(1))

def _missing_configs(self) -> List[str]:
def check(s):
return self._menu_options.get(s).has_selection()
def check(s) -> bool:
obj = self._menu_options.get(s)
if obj and obj.has_selection():
return True
return False

def has_superuser() -> bool:
sel = self._menu_options['!users']
Expand Down Expand Up @@ -228,7 +231,7 @@ def _display_network_conf(self, config: Optional[NetworkConfiguration]) -> str:
return config.type.display_msg()

def _disk_encryption(self, preset: Optional[disk.DiskEncryption]) -> Optional[disk.DiskEncryption]:
mods: Optional[List[disk.DeviceModification]] = self._menu_options['disk_config'].current_selection
mods: Optional[disk.DiskLayoutConfiguration] = self._menu_options['disk_config'].current_selection

if not mods:
# this should not happen as the encryption menu has the disk_config as dependency
Expand Down Expand Up @@ -263,7 +266,7 @@ def _prev_network_config(self) -> Optional[str]:

def _prev_additional_pkgs(self):
selector = self._menu_options['packages']
if selector.has_selection():
if selector.current_selection:
packages: List[str] = selector.current_selection
return format_cols(packages, None)
return None
Expand Down
14 changes: 10 additions & 4 deletions archinstall/lib/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def _verify_service_stop(self):
We need to wait for it before we continue since we opted in to use a custom mirror/region.
"""
info('Waiting for time sync (systemd-timesyncd.service) to complete.')
while SysCommand('timedatectl show --property=NTPSynchronized --value').decode() != 'yes':

while True:
time_val = SysCommand('timedatectl show --property=NTPSynchronized --value').decode()
if time_val and time_val.strip() == 'yes':
break
time.sleep(1)

info('Waiting for automatic mirror selection (reflector) to complete.')
Expand Down Expand Up @@ -237,7 +241,7 @@ def generate_key_files(self):
gen_enc_file = self._disk_encryption.should_generate_encryption_file(part_mod)

luks_handler = Luks2(
part_mod.dev_path,
part_mod.safe_dev_path,
mapper_name=part_mod.mapper_name,
password=self._disk_encryption.encryption_password
)
Expand Down Expand Up @@ -281,8 +285,10 @@ def add_swapfile(self, size='4G', enable_resume=True, file='/swapfile'):
self._fstab_entries.append(f'{file} none swap defaults 0 0')

if enable_resume:
resume_uuid = SysCommand(f'findmnt -no UUID -T {self.target}{file}').decode('UTF-8').strip()
resume_offset = SysCommand(f'/usr/bin/filefrag -v {self.target}{file}').decode().split('0:', 1)[1].split(":", 1)[1].split("..", 1)[0].strip()
resume_uuid = SysCommand(f'findmnt -no UUID -T {self.target}{file}').decode()
resume_offset = SysCommand(
f'/usr/bin/filefrag -v {self.target}{file}'
).decode().split('0:', 1)[1].split(":", 1)[1].split("..", 1)[0].strip()

self._hooks.append('resume')
self._kernel_params.append(f'resume=UUID={resume_uuid}')
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/menu/abstract_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Selector:
def __init__(
self,
description: str,
func: Optional[Callable[[str], Any]] = None,
func: Optional[Callable[[Any], Any]] = None,
display_func: Optional[Callable] = None,
default: Optional[Any] = None,
enabled: bool = False,
Expand Down
20 changes: 17 additions & 3 deletions archinstall/lib/models/gen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional, List
from typing import Optional, List, Dict, Any


@dataclass
Expand Down Expand Up @@ -87,6 +87,10 @@ class PackageSearchResult:
makedepends: List[str]
checkdepends: List[str]

@staticmethod
def from_json(data: Dict[str, Any]) -> 'PackageSearchResult':
return PackageSearchResult(**data)

@property
def pkg_version(self) -> str:
return self.pkgver
Expand All @@ -107,8 +111,18 @@ class PackageSearch:
page: int
results: List[PackageSearchResult]

def __post_init__(self):
self.results = [PackageSearchResult(**x) for x in self.results]
@staticmethod
def from_json(data: Dict[str, Any]) -> 'PackageSearch':
results = [PackageSearchResult.from_json(r) for r in data['results']]

return PackageSearch(
version=data['version'],
limit=data['limit'],
valid=data['valid'],
num_pages=data['num_pages'],
page=data['page'],
results=results
)


@dataclass
Expand Down
4 changes: 2 additions & 2 deletions archinstall/lib/packages/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def package_search(package :str) -> PackageSearch:
raise PackageError(f"Could not locate package: [{response.code}] {response}")

data = response.read().decode('UTF-8')

return PackageSearch(**json.loads(data))
json_data = json.loads(data)
return PackageSearch.from_json(json_data)


def find_package(package :str) -> List[PackageSearchResult]:
Expand Down
6 changes: 3 additions & 3 deletions archinstall/lib/profile/profiles_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ def add_custom_profiles(self, profiles: Union[TProfile, List[TProfile]]):
profiles = [profiles]

for profile in profiles:
self._profiles.append(profile)
self.profiles.append(profile)

self._verify_unique_profile_names(self._profiles)
self._verify_unique_profile_names(self.profiles)

def remove_custom_profiles(self, profiles: Union[TProfile, List[TProfile]]):
if not isinstance(profiles, list):
profiles = [profiles]

remove_names = [p.name for p in profiles]
self._profiles = [p for p in self._profiles if p.name not in remove_names]
self._profiles = [p for p in self.profiles if p.name not in remove_names]

def get_profile_by_name(self, name: str) -> Optional[Profile]:
return next(filter(lambda x: x.name == name, self.profiles), None) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/translationhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,4 @@ def format(self, *args) -> str:
@classmethod
def install(cls):
import builtins
builtins._ = cls
builtins._ = cls # type: ignore
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ packages = ["archinstall"]
python_version = "3.11"
files = "archinstall/"
exclude = "tests"
#check_untyped_defs=true
check_untyped_defs=true

[tool.bandit]
targets = ["archinstall"]
Expand Down

0 comments on commit edbc135

Please sign in to comment.