From e7f2a8c2037f494e73841998a1621d360af9bdb2 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Sat, 11 Jan 2025 04:44:43 +0000 Subject: [PATCH] Fix some mypy warnings in archinstall/lib/ (#3103) --- archinstall/lib/args.py | 2 +- archinstall/lib/configuration.py | 2 +- archinstall/lib/disk/device_model.py | 10 ++++++++-- archinstall/lib/general.py | 2 +- archinstall/lib/installer.py | 2 +- archinstall/lib/models/gen.py | 10 ++++++++-- archinstall/lib/models/mirrors.py | 1 + archinstall/lib/models/users.py | 6 +++--- archinstall/lib/networking.py | 10 +++++----- archinstall/lib/output.py | 4 ++-- archinstall/lib/pacman/__init__.py | 2 +- pyproject.toml | 7 +++++-- 12 files changed, 37 insertions(+), 21 deletions(-) diff --git a/archinstall/lib/args.py b/archinstall/lib/args.py index d1e8011f94..0bfb320857 100644 --- a/archinstall/lib/args.py +++ b/archinstall/lib/args.py @@ -326,7 +326,7 @@ def _read_file(self, path: Path) -> str: return path.read_text() - def _cleanup_config(self, config: Namespace | dict) -> dict[str, Any]: + def _cleanup_config(self, config: Namespace | dict[str, Any]) -> dict[str, Any]: clean_args = {} for key, val in config.items(): if isinstance(val, dict): diff --git a/archinstall/lib/configuration.py b/archinstall/lib/configuration.py index aa04a53369..1552f1f58a 100644 --- a/archinstall/lib/configuration.py +++ b/archinstall/lib/configuration.py @@ -20,7 +20,7 @@ class ConfigurationOutput: - def __init__(self, config: dict): + def __init__(self, config: dict[str, Any]): """ Configuration output handler to parse the existing configuration data structure and prepare for output on the console and for saving it to configuration files diff --git a/archinstall/lib/disk/device_model.py b/archinstall/lib/disk/device_model.py index d9ce75d34f..485d6595ab 100644 --- a/archinstall/lib/disk/device_model.py +++ b/archinstall/lib/disk/device_model.py @@ -439,11 +439,17 @@ def __le__(self, other: Size) -> bool: return self._normalize() <= other._normalize() @override - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: + if not isinstance(other, Size): + return NotImplemented + return self._normalize() == other._normalize() @override - def __ne__(self, other) -> bool: + def __ne__(self, other: object) -> bool: + if not isinstance(other, Size): + return NotImplemented + return self._normalize() != other._normalize() def __gt__(self, other: Size) -> bool: diff --git a/archinstall/lib/general.py b/archinstall/lib/general.py index 1a96ef7ef4..181588b6d4 100644 --- a/archinstall/lib/general.py +++ b/archinstall/lib/general.py @@ -477,7 +477,7 @@ def run_custom_user_commands(commands: list[str], installation: Installer) -> No os.unlink(chroot_path) -def json_stream_to_structure(configuration_identifier: str, stream: str, target: dict) -> bool: +def json_stream_to_structure(configuration_identifier: str, stream: str, target: dict[str, Any]) -> bool: """ Load a JSON encoded dictionary from a stream and merge it into an existing dictionary. A stream can be a filepath, a URL or a raw JSON string. diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 19a8667b17..f8ea21141c 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -72,7 +72,7 @@ def __init__( if accessibility_tools_in_use(): self._base_packages.extend(__accessibility_packages__) - self.post_base_install: list[Callable] = [] + self.post_base_install: list[Callable] = [] # type: ignore[type-arg] # TODO: Figure out which one of these two we'll use.. But currently we're mixing them.. storage['session'] = self diff --git a/archinstall/lib/models/gen.py b/archinstall/lib/models/gen.py index 257fbd1f2f..6d626f32fd 100644 --- a/archinstall/lib/models/gen.py +++ b/archinstall/lib/models/gen.py @@ -40,7 +40,10 @@ def pkg_version(self) -> str: return self.pkgver @override - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: + if not isinstance(other, PackageSearchResult): + return NotImplemented + return self.pkg_version == other.pkg_version def __lt__(self, other: 'PackageSearchResult') -> bool: @@ -99,7 +102,10 @@ def pkg_version(self) -> str: return self.version @override - def __eq__(self, other) -> bool: + def __eq__(self, other: object) -> bool: + if not isinstance(other, LocalPackage): + return NotImplemented + return self.pkg_version == other.pkg_version def __lt__(self, other: 'LocalPackage') -> bool: diff --git a/archinstall/lib/models/mirrors.py b/archinstall/lib/models/mirrors.py index 2eb3fc6338..09aec8f392 100644 --- a/archinstall/lib/models/mirrors.py +++ b/archinstall/lib/models/mirrors.py @@ -57,6 +57,7 @@ def speed(self) -> float: with urllib.request.urlopen(req, None, 5) as handle, DownloadTimer(timeout=5) as timer: size = len(handle.read()) + assert timer.time is not None self._speed = size / timer.time debug(f" speed: {self._speed} ({int(self._speed / 1024 / 1024 * 100) / 100}MiB/s)") # Do not retry error diff --git a/archinstall/lib/models/users.py b/archinstall/lib/models/users.py index 46ff52bb0e..26497b5b44 100644 --- a/archinstall/lib/models/users.py +++ b/archinstall/lib/models/users.py @@ -140,7 +140,7 @@ def _parse(cls, config_users: list[dict[str, Any]]) -> list['User']: return users @classmethod - def _parse_backwards_compatible(cls, config_users: dict, sudo: bool) -> list['User']: + def _parse_backwards_compatible(cls, config_users: dict[str, dict[str, str]], sudo: bool) -> list['User']: if len(config_users.keys()) > 0: username = list(config_users.keys())[0] password = config_users[username]['!password'] @@ -153,8 +153,8 @@ def _parse_backwards_compatible(cls, config_users: dict, sudo: bool) -> list['Us @classmethod def parse_arguments( cls, - config_users: list[dict[str, str]] | dict[str, str], - config_superusers: list[dict[str, str]] | dict[str, str] + config_users: list[dict[str, str]] | dict[str, dict[str, str]], + config_superusers: list[dict[str, str]] | dict[str, dict[str, str]] ) -> list['User']: users = [] diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py index 9de7313809..b50c0f79f8 100644 --- a/archinstall/lib/networking.py +++ b/archinstall/lib/networking.py @@ -6,8 +6,8 @@ import ssl import struct import time -from types import FrameType -from typing import Any +from types import FrameType, TracebackType +from typing import Any, Self from urllib.error import URLError from urllib.parse import urlencode from urllib.request import urlopen @@ -40,7 +40,7 @@ def raise_timeout(self, signl: int, frame: FrameType | None) -> None: ''' raise DownloadTimeout(f'Download timed out after {self.timeout} second(s).') - def __enter__(self): + def __enter__(self) -> Self: if self.timeout > 0: self.previous_handler = signal.signal(signal.SIGALRM, self.raise_timeout) # type: ignore[assignment] self.previous_timer = signal.alarm(self.timeout) @@ -48,7 +48,7 @@ def __enter__(self): self.start_time = time.time() return self - def __exit__(self, typ, value, traceback) -> None: + def __exit__(self, typ: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None) -> None: if self.start_time: time_delta = time.time() - self.start_time signal.alarm(0) @@ -164,7 +164,7 @@ def build_icmp(payload: bytes) -> bytes: return struct.pack('!BBHHH', 8, 0, checksum, 0, 1) + payload -def ping(hostname, timeout=5) -> int: +def ping(hostname, timeout: int = 5) -> int: watchdog = select.epoll() started = time.time() random_identifier = f'archinstall-{random.randint(1000, 9999)}'.encode() diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index d001b96268..bed2959c8f 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -20,7 +20,7 @@ class FormattedOutput: def _get_values( cls, o: 'DataclassInstance', - class_formatter: str | Callable | None = None, + class_formatter: str | Callable | None = None, # type: ignore[type-arg] filter_list: list[str] = [] ) -> dict[str, Any]: """ @@ -52,7 +52,7 @@ def _get_values( def as_table( cls, obj: list[Any], - class_formatter: str | Callable | None = None, + class_formatter: str | Callable | None = None, # type: ignore[type-arg] filter_list: list[str] = [], capitalize: bool = False ) -> str: diff --git a/archinstall/lib/pacman/__init__.py b/archinstall/lib/pacman/__init__.py index 025b7a3925..8ecb664da8 100644 --- a/archinstall/lib/pacman/__init__.py +++ b/archinstall/lib/pacman/__init__.py @@ -45,7 +45,7 @@ def run(args: str, default_cmd: str = 'pacman') -> SysCommand: return SysCommand(f'{default_cmd} {args}') - def ask(self, error_message: str, bail_message: str, func: Callable, *args, **kwargs) -> None: + def ask(self, error_message: str, bail_message: str, func: Callable, *args, **kwargs) -> None: # type: ignore[type-arg] while True: try: func(*args, **kwargs) diff --git a/pyproject.toml b/pyproject.toml index 253e539733..52ea5a542b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,13 +96,16 @@ disallow_any_explicit = true [[tool.mypy.overrides]] module = "archinstall.lib.*" -disallow_any_generics = false -disallow_any_unimported = false disallow_incomplete_defs = false disallow_untyped_defs = false warn_return_any = false warn_unreachable = false +[[tool.mypy.overrides]] +module = "archinstall.lib.disk.*" +# 'Any' imports are allowed because pyparted doesn't have type hints +disallow_any_unimported = false + [[tool.mypy.overrides]] module = "archinstall.lib.packages" disallow_any_explicit = true