Skip to content

Commit

Permalink
Type changes for back-compat to 3.6, linting pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ingrinder committed Apr 13, 2024
1 parent d17dfc7 commit b7c9f87
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
6 changes: 4 additions & 2 deletions archey/entries/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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).
Expand Down
6 changes: 4 additions & 2 deletions archey/entries/lan_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ipaddress
from itertools import islice
from typing import Iterator, Self
from typing import Iterator, TypeVar

try:
import netifaces
Expand All @@ -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"""
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions archey/entries/wan_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 10 additions & 6 deletions archey/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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...
Expand All @@ -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])
Expand All @@ -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.
"""
Expand All @@ -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)
24 changes: 20 additions & 4 deletions archey/test/entries/test_archey_load_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"):
Expand All @@ -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"):
Expand All @@ -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}"
),
)


Expand Down
2 changes: 1 addition & 1 deletion archey/test/test_archey_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down

0 comments on commit b7c9f87

Please sign in to comment.