Skip to content

Commit

Permalink
Fix some mypy warnings in archinstall/lib/ (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
correctmost authored Jan 11, 2025
1 parent 22b410d commit e7f2a8c
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion archinstall/lib/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions archinstall/lib/disk/device_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions archinstall/lib/models/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions archinstall/lib/models/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions archinstall/lib/models/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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 = []

Expand Down
10 changes: 5 additions & 5 deletions archinstall/lib/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,15 +40,15 @@ 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)

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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions archinstall/lib/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion archinstall/lib/pacman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e7f2a8c

Please sign in to comment.