From b7c9f8733a10cfbc2252cdcd9a61652157b93268 Mon Sep 17 00:00:00 2001 From: Michael Bromilow <12384431+ingrinder@users.noreply.github.com> Date: Sat, 13 Apr 2024 21:22:03 +0100 Subject: [PATCH] Type changes for back-compat to 3.6, linting pass --- archey/entries/disk.py | 6 +++-- archey/entries/lan_ip.py | 6 +++-- archey/entries/wan_ip.py | 6 +++-- archey/entry.py | 16 ++++++++----- .../test/entries/test_archey_load_average.py | 24 +++++++++++++++---- archey/test/test_archey_entry.py | 2 +- 6 files changed, 43 insertions(+), 17 deletions(-) diff --git a/archey/entries/disk.py b/archey/entries/disk.py index ec215ffe..69674453 100644 --- a/archey/entries/disk.py +++ b/archey/entries/disk.py @@ -4,11 +4,13 @@ import plistlib import re from subprocess import DEVNULL, PIPE, check_output, run -from typing import Dict, Iterable, List, Self +from typing import Dict, Iterable, List, TypeVar from archey.colors import Colors from archey.entry import Entry +Self = TypeVar("Self", bound="Disk") + class Disk(Entry): """Uses `df` to compute disk usage across devices""" @@ -231,7 +233,7 @@ def _blocks_to_human_readable(blocks: float, suffix: str = "B") -> str: def __str__(self): return "placeholder" - def __iter__(self) -> Self: + def __iter__(self: Self) -> Self: """Sets up iterable for entry.""" # Combine all entries into one grand-total if configured to do so # (and we have a "truthy" value). diff --git a/archey/entries/lan_ip.py b/archey/entries/lan_ip.py index 3f62f3ad..ff1478a2 100644 --- a/archey/entries/lan_ip.py +++ b/archey/entries/lan_ip.py @@ -2,7 +2,7 @@ import ipaddress from itertools import islice -from typing import Iterator, Self +from typing import Iterator, TypeVar try: import netifaces @@ -11,6 +11,8 @@ from archey.entry import Entry +Self = TypeVar("Self", bound="LanIP") + class LanIP(Entry): """Relies on the `netifaces` module to detect LAN IP addresses""" @@ -75,7 +77,7 @@ def _lan_ip_addresses_generator( # Finally, yield the address compressed representation. yield ip_addr.compressed - def __iter__(self) -> Self: + def __iter__(self: Self) -> Self: """Sets up iterable over IP addresses""" if not self.value and netifaces: # If no IP addresses found, fall-back on the "No address" string. diff --git a/archey/entries/wan_ip.py b/archey/entries/wan_ip.py index 133db666..a17a0615 100644 --- a/archey/entries/wan_ip.py +++ b/archey/entries/wan_ip.py @@ -2,13 +2,15 @@ from socket import timeout as SocketTimeoutError from subprocess import DEVNULL, CalledProcessError, TimeoutExpired, check_output -from typing import Optional, Self +from typing import Optional, TypeVar from urllib.error import URLError from urllib.request import urlopen from archey.entry import Entry from archey.environment import Environment +Self = TypeVar("Self", bound="WanIP") + class WanIP(Entry): """Uses different ways to retrieve the public IPv{4,6} addresses""" @@ -97,7 +99,7 @@ def _run_http_request(server_url: str, timeout: float) -> Optional[str]: except (URLError, SocketTimeoutError): return None - def __iter__(self) -> Self: + def __iter__(self: Self) -> Self: """Sets up iterable over IP addresses""" if not self.value and not Environment.DO_NOT_TRACK: # If no IP addresses found, fall-back on the "No address" string. diff --git a/archey/entry.py b/archey/entry.py index 3c5db167..0e79ae77 100644 --- a/archey/entry.py +++ b/archey/entry.py @@ -3,15 +3,17 @@ import logging from abc import ABC as AbstractBaseClass from abc import abstractmethod -from typing import Any, Iterator, Optional, Self, TypeAlias +from typing import Any, Iterator, Optional, Tuple, TypeVar from archey.configuration import Configuration +Self = TypeVar("Self", bound="Entry") + class Entry(AbstractBaseClass): """Module base class""" - ValueType: TypeAlias = tuple[str, Optional[str]] + ValueType = Tuple[str, Optional[str]] _ICON: Optional[str] = None _PRETTY_NAME: Optional[str] = None @@ -23,7 +25,9 @@ def __new__(cls, *_, **kwargs): return super().__new__(cls) @abstractmethod - def __init__(self, name: Optional[str] = None, value=None, options: Optional[dict] = None): + def __init__( + self: Self, name: Optional[str] = None, value=None, options: Optional[dict] = None + ): configuration = Configuration() # Each entry will have always have the following attributes... @@ -49,7 +53,7 @@ def __init__(self, name: Optional[str] = None, value=None, options: Optional[dic self._iter_idx = 0 self._iter_value: Iterator[Any] = iter([]) - def __iter__(self) -> Self: + def __iter__(self: Self) -> Self: """Best-effort set up of an iterable of value for inherited entries to use.""" if isinstance(self.value, (str, int)): self._iter_value = iter([self.value]) @@ -63,7 +67,7 @@ def __iter__(self) -> Self: self._iter_value = iter([]) return self - def __next__(self) -> ValueType: + def __next__(self: Self) -> ValueType: """ Default behaviour: assume we can just use `__str__` on ourself for a single-line output. """ @@ -77,7 +81,7 @@ def __next__(self) -> ValueType: # Otherwise, just raise `StopIteration` immediately raise StopIteration - def __str__(self) -> str: + def __str__(self: Self) -> str: """Provide a sane default printable string representation of the entry""" # Assume that the `__str__` of our value is usable return str(self.value) diff --git a/archey/test/entries/test_archey_load_average.py b/archey/test/entries/test_archey_load_average.py index 38423f22..e9cacac0 100644 --- a/archey/test/entries/test_archey_load_average.py +++ b/archey/test/entries/test_archey_load_average.py @@ -25,7 +25,11 @@ def test_pretty_value_coloration(self): self.assertEqual( str(self.load_average_mock), - f"{Colors.GREEN_NORMAL}0.5{Colors.CLEAR} {Colors.YELLOW_NORMAL}1.25{Colors.CLEAR} {Colors.RED_NORMAL}2.5{Colors.CLEAR}", + ( + f"{Colors.GREEN_NORMAL}0.5{Colors.CLEAR} " + f"{Colors.YELLOW_NORMAL}1.25{Colors.CLEAR} " + f"{Colors.RED_NORMAL}2.5{Colors.CLEAR}" + ), ) @HelperMethods.patch_clean_configuration @@ -41,7 +45,11 @@ def test_pretty_value_rounding(self): } self.assertEqual( str(self.load_average_mock), - f"{Colors.GREEN_NORMAL}0.0{Colors.CLEAR} {Colors.GREEN_NORMAL}1.0{Colors.CLEAR} {Colors.GREEN_NORMAL}2.0{Colors.CLEAR}", + ( + f"{Colors.GREEN_NORMAL}0.0{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}1.0{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}" + ), ) with self.subTest("1 decimal place"): @@ -52,7 +60,11 @@ def test_pretty_value_rounding(self): } self.assertEqual( str(self.load_average_mock), - f"{Colors.GREEN_NORMAL}0.3{Colors.CLEAR} {Colors.GREEN_NORMAL}1.2{Colors.CLEAR} {Colors.GREEN_NORMAL}2.0{Colors.CLEAR}", + ( + f"{Colors.GREEN_NORMAL}0.3{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}1.2{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}" + ), ) with self.subTest("2 decimal places"): @@ -63,7 +75,11 @@ def test_pretty_value_rounding(self): } self.assertEqual( str(self.load_average_mock), - f"{Colors.GREEN_NORMAL}0.33{Colors.CLEAR} {Colors.GREEN_NORMAL}1.25{Colors.CLEAR} {Colors.GREEN_NORMAL}2.0{Colors.CLEAR}", + ( + f"{Colors.GREEN_NORMAL}0.33{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}1.25{Colors.CLEAR} " + f"{Colors.GREEN_NORMAL}2.0{Colors.CLEAR}" + ), ) diff --git a/archey/test/test_archey_entry.py b/archey/test/test_archey_entry.py index e6c2a5b8..fad2de50 100644 --- a/archey/test/test_archey_entry.py +++ b/archey/test/test_archey_entry.py @@ -14,7 +14,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @property - def pretty_value(self) -> "typing.List[tuple[str, str]]": + def pretty_value(self) -> "typing.List[typing.Tuple[str, str]]": """Reverse order!""" return [(self.value, self.name)]