Skip to content

Commit

Permalink
fix typing for 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
skullydazed committed Jan 29, 2024
1 parent ebdf695 commit c81a92e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
20 changes: 10 additions & 10 deletions milc/_sparkline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"""
from decimal import Decimal
from math import inf
from typing import Any, List, Optional, TypeGuard
from typing import Any, List, Optional

from milc import cli

spark_chars = '▁▂▃▄▅▆▇█'


def is_number(i: Any) -> TypeGuard[bool]:
def is_number(i: Any) -> bool:
"""Returns true if i is a number. Used to filter non-numbers from a list.
"""
return isinstance(i, (int, float, Decimal))
Expand Down Expand Up @@ -81,12 +81,12 @@ def sparkline(
A MILC or ANSI color code to reset the color code applied in `positive_color`. This is usually `{fg_reset}`, `{bg_reset}`, or `{style_reset_all}`.
"""
if min_value is None:
min_value = min(filter(is_number, number_list))
min_value = min(filter(is_number, number_list)) # type: ignore[type-var]

if max_value is None:
max_value = max(filter(is_number, number_list))
max_value = max(filter(is_number, number_list)) # type: ignore[type-var]

int_range = max_value - min_value
int_range = max_value - min_value # type: ignore[operator]
sparks = []

for i in number_list:
Expand All @@ -95,12 +95,12 @@ def sparkline(
sparks.append(' ')
continue

if i < min_value or i > max_value:
if i < min_value or i > max_value: # type: ignore[operator]
cli.log.debug('Skipping out of bounds value %s', i)
continue

# Determine the bucket for this value
spark_int = (i-min_value) / int_range * 8
spark_int = (i-min_value) / int_range * 8 # type: ignore[operator]

if spark_int > 7:
spark_int = 7
Expand All @@ -109,15 +109,15 @@ def sparkline(
color = positive_color
reset = positive_reset

if i < 0:
if i < 0: # type: ignore[operator]
color = negative_color
reset = negative_reset

if i < highlight_low:
if i < highlight_low: # type: ignore[operator]
color = highlight_low_color
reset = highlight_low_reset

if i > highlight_high:
if i > highlight_high: # type: ignore[operator]
color = highlight_high_color
reset = highlight_high_reset

Expand Down
16 changes: 8 additions & 8 deletions milc/milc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from platform import platform
from tempfile import NamedTemporaryFile
from types import TracebackType
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union

try:
import threading
Expand All @@ -21,9 +21,9 @@

import argcomplete
import colorama
from appdirs import user_config_dir # type: ignore[import-untyped]
from halo import Halo # type: ignore[import-untyped]
from spinners.spinners import Spinners # type: ignore[import-untyped]
from appdirs import user_config_dir # type: ignore
from halo import Halo # type: ignore
from spinners.spinners import Spinners # type: ignore

from .ansi import MILCFormatter, ansi_colors, ansi_config, ansi_escape, format_ansi
from .attrdict import AttrDict
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(
self._config_store_true: Sequence[str] = []
self._config_store_false: Sequence[str] = []
self._entrypoint: Callable[[Any], Any] = lambda _: None
self._spinners: Dict[str, Dict[str, int | Sequence[str]]] = {}
self._spinners: Dict[str, Dict[str, Union[int, Sequence[str]]]] = {}
self._subcommand = None
self._inside_context_manager = False
self.ansi = ansi_colors
Expand Down Expand Up @@ -132,7 +132,7 @@ def run(
combined_output: bool = False,
text: bool = True,
**kwargs: Any,
) -> subprocess.CompletedProcess[bytes | str]:
) -> Any: # FIXME: In python 3.10 we can use subprocess.CompletedProcess[bytes | str] instead
"""Run a command using `subprocess.run`, but using some different defaults.
Unlike subprocess.run you must supply a sequence of arguments. You can use `shlex.split()` to build this from a string.
Expand Down Expand Up @@ -757,7 +757,7 @@ def __enter__(self) -> Any:

def __exit__(
self,
exc_type: Optional[type[BaseException]],
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
Expand All @@ -775,7 +775,7 @@ def is_spinner(self, name: str) -> bool:
"""
return name in Spinners.__members__ or name in self._spinners

def add_spinner(self, name: str, spinner: Dict[str, int | Sequence[str]]) -> None:
def add_spinner(self, name: str, spinner: Dict[str, Union[int, Sequence[str]]]) -> None:
"""Adds a new spinner to the list of spinners.
A spinner is a dictionary with two keys:
Expand Down
4 changes: 2 additions & 2 deletions milc/questions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Sometimes you need to ask the user a question. MILC provides basic functions for collecting and validating user input. You can find these in the `milc.questions` module.
"""
from getpass import getpass
from typing import Any, Callable, Optional, Sequence
from typing import Any, Callable, Optional, Sequence, Union

from milc import cli
from .ansi import format_ansi
Expand Down Expand Up @@ -140,7 +140,7 @@ def question(
answer_type: Callable[[str], str] = str,
validate: Optional[Callable[..., bool]] = None,
**kwargs: Any,
) -> str | Any:
) -> Union[str, Any]:
"""Allow the user to type in a free-form string to answer.
| Argument | Description |
Expand Down
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ flake8
hjson
mike
mkdocs-git-revision-date-plugin
mkdocs-material
mkdocs-material<9
mypy
nose2
pygments
types-appdirs
semver
yapf

0 comments on commit c81a92e

Please sign in to comment.