Skip to content

Commit

Permalink
Use newstyle type annotations across the board
Browse files Browse the repository at this point in the history
We do not need them at runtime, and the import from __future__ allows us
to not parse them. As such, we can use newer syntax
  • Loading branch information
BenjaminSchubert committed Dec 31, 2023
1 parent 914ddc7 commit 9494f0a
Show file tree
Hide file tree
Showing 34 changed files with 273 additions and 366 deletions.
12 changes: 6 additions & 6 deletions .github/scripts/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import argparse
import logging
from dataclasses import InitVar, dataclass, field
from typing import Any, List, Literal, Optional
from typing import Any, Literal
from xml.etree import ElementTree

from tabulate import tabulate
Expand All @@ -18,10 +18,10 @@ class TestCase:
name: str
time: float
state: Literal["success", "failure", "error", "skipped"]
summary: Optional[str]
summary: str | None

@classmethod
def from_junit(cls, tree: ElementTree.Element) -> "TestCase":
def from_junit(cls, tree: ElementTree.Element) -> TestCase:
children = tree.getchildren()
assert len(children) <= 1

Expand Down Expand Up @@ -69,13 +69,13 @@ class TestSuite:
failures: int
skipped: int
time: float
tests: List[TestCase]
tests: list[TestCase]

def __post_init__(self, total: int) -> None:
self.successes = total - self.errors - self.failures - self.skipped

@classmethod
def from_junit(cls, tree: ElementTree.Element) -> "TestSuite":
def from_junit(cls, tree: ElementTree.Element) -> TestSuite:
attrs = tree.attrib

tests = [
Expand All @@ -99,7 +99,7 @@ def __lt__(self, other: Any) -> bool:
return (self.time, self.name) < (other.time, other.name)


def get_failures_and_errors(testsuites: List[TestSuite]) -> str:
def get_failures_and_errors(testsuites: list[TestSuite]) -> str:
reports = []

for testsuite in testsuites:
Expand Down
10 changes: 5 additions & 5 deletions docs/_extensions/cleanup_signatures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from sphinx.application import Sphinx
Expand All @@ -14,17 +14,17 @@ def cleanup_signatures( # pylint: disable=unused-argument
name: str,
obj: Any,
options: Options,
signature: Optional[str],
return_annotation: Optional[str],
) -> Optional[Tuple[str, Optional[str]]]:
signature: str | None,
return_annotation: str | None,
) -> tuple[str, str | None] | None:
if name == "dwas.StepRunner":
# Hide the __init__ signature for dwas.StepRunner, it's meant to be
# private
return ("()", return_annotation)
return None


def setup(app: Sphinx) -> Dict[str, Any]:
def setup(app: Sphinx) -> dict[str, Any]:
app.connect("autodoc-process-signature", cleanup_signatures)

return {
Expand Down
6 changes: 3 additions & 3 deletions docs/_extensions/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import shlex
import subprocess
from typing import TYPE_CHECKING, Any, Dict, List
from typing import TYPE_CHECKING, Any

from docutils import nodes
from docutils.parsers import rst
Expand All @@ -29,7 +29,7 @@ class ExecuteDirective(rst.Directive):
"cwd": unchanged,
}

def run(self) -> List[nodes.Element]:
def run(self) -> list[nodes.Element]:
env = self.state.document.settings.env

node = execute()
Expand Down Expand Up @@ -81,7 +81,7 @@ def run_programs(
node.replace_self(new_node)


def setup(app: Sphinx) -> Dict[str, Any]:
def setup(app: Sphinx) -> dict[str, Any]:
app.add_directive("command-output", ExecuteDirective)
app.connect("doctree-read", run_programs)
return {"parallel_read_safe": True}
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ ignore = [
"S603",
# We want to be able tor rely on PATH and not hardcode binaries
"S607",

# FIXME: re-enable upgrade rules
"UP",
]

flake8-pytest-style.parametrize-values-type = "tuple"
Expand Down
18 changes: 9 additions & 9 deletions src/dwas/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from contextvars import copy_context
from importlib.metadata import version
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any

from . import _io, _pipeline
from ._config import Config
Expand All @@ -35,7 +35,7 @@ def __call__(
parser: ArgumentParser, # noqa:ARG002
namespace: Namespace,
values: Any,
option_string: Optional[str] = None, # noqa:ARG002
option_string: str | None = None, # noqa:ARG002
) -> None:
items = getattr(namespace, self.dest, None)
if items is None:
Expand All @@ -56,7 +56,7 @@ def __call__(
parser: ArgumentParser, # noqa:ARG002
namespace: Namespace,
values: Any, # noqa:ARG002
option_string: Optional[str] = None,
option_string: str | None = None,
) -> None:
assert option_string is not None

Expand All @@ -69,7 +69,7 @@ def format_usage(self) -> str:
return " | ".join(self.option_strings)


def _parse_args(args: Optional[List[str]] = None) -> Namespace:
def _parse_args(args: list[str] | None = None) -> Namespace:
parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
epilog="""\
Expand Down Expand Up @@ -199,8 +199,8 @@ def _parse_args(args: Optional[List[str]] = None) -> Namespace:


def _parse_steps(
args: List[str], known_steps: Dict[str, BaseStepHandler]
) -> Optional[List[str]]:
args: list[str], known_steps: dict[str, BaseStepHandler]
) -> list[str] | None:
if not args:
return None

Expand Down Expand Up @@ -279,8 +279,8 @@ def _load_user_config(
def _execute_pipeline(
config: Config,
pipeline_config: str,
steps_parameters: List[str],
except_steps: Optional[List[str]],
steps_parameters: list[str],
except_steps: list[str] | None,
*,
only_selected_step: bool,
clean: bool,
Expand Down Expand Up @@ -318,7 +318,7 @@ def _execute_pipeline(


@_io.instrument_streams()
def main(sys_args: Optional[List[str]] = None) -> None:
def main(sys_args: list[str] | None = None) -> None:
if sys_args is None:
sys_args = sys.argv[1:]
if env_args := os.environ.get("DWAS_ADDOPTS"):
Expand Down
9 changes: 4 additions & 5 deletions src/dwas/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import shutil
import sys
from pathlib import Path
from typing import Dict, Optional

from ._exceptions import BaseDwasException

Expand Down Expand Up @@ -58,7 +57,7 @@ class Config:
- Finally, it will look if this is attached to a tty and enable colors if so.
"""

environ: Dict[str, str]
environ: dict[str, str]
"""
The environment to use when running commands.
Expand Down Expand Up @@ -125,9 +124,9 @@ class Config:
def __init__(
self,
cache_path: str,
log_path: Optional[str],
log_path: str | None,
verbosity: int,
colors: Optional[bool],
colors: bool | None,
n_jobs: int,
*,
skip_missing_interpreters: bool,
Expand Down Expand Up @@ -203,7 +202,7 @@ def __init__(
self.environ["PY_COLORS"] = "0"
self.environ["NO_COLOR"] = "0"

def _get_color_setting(self, colors: Optional[bool]) -> bool:
def _get_color_setting(self, colors: bool | None) -> bool:
# pylint: disable=too-many-return-statements
if colors is not None:
return colors
Expand Down
4 changes: 2 additions & 2 deletions src/dwas/_dependency_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import inspect
import logging
from typing import Any, Callable, Dict, TypeVar
from typing import Any, Callable, TypeVar

from ._exceptions import BaseDwasException
from ._inspect import get_location, get_name
Expand All @@ -12,7 +12,7 @@


def call_with_parameters(
func: Callable[..., T], parameters: Dict[str, Any]
func: Callable[..., T], parameters: dict[str, Any]
) -> T:
signature = inspect.signature(func)
kwargs = {}
Expand Down
6 changes: 2 additions & 4 deletions src/dwas/_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from typing import List


class BaseDwasException(Exception):
"""
Expand Down Expand Up @@ -48,13 +46,13 @@ def _pluralize(self, n_jobs: int) -> str:


class UnknownStepsException(BaseDwasException):
def __init__(self, steps: List[str]) -> None:
def __init__(self, steps: list[str]) -> None:
message = f"Unknown steps: {', '.join(steps)}"
super().__init__(message)


class CyclicStepDependenciesException(BaseDwasException):
def __init__(self, cycle: List[str]) -> None:
def __init__(self, cycle: list[str]) -> None:
message = f"Cyclic dependencies between steps: {' --> '.join(cycle)}"
super().__init__(message)
self.cycle = cycle
Expand Down
6 changes: 3 additions & 3 deletions src/dwas/_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from contextvars import copy_context
from datetime import timedelta
from threading import Event, Thread
from typing import TYPE_CHECKING, Iterator, List
from typing import TYPE_CHECKING, Iterator

from colorama import Cursor, Fore, Style, ansi

Expand All @@ -29,7 +29,7 @@ def __init__(self, scheduler: Scheduler, start_time: float) -> None:
def _counter(self, value: int, color: str) -> str:
return f"{color}{Style.BRIGHT}{value}{Style.NORMAL}{Fore.YELLOW}"

def lines(self) -> List[str]:
def lines(self) -> list[str]:
update_at = time.monotonic()

term_width = shutil.get_terminal_size().columns
Expand Down Expand Up @@ -59,7 +59,7 @@ def lines(self) -> List[str]:
"~",
)

additional_info: List[str] = []
additional_info: list[str] = []
if self._scheduler.ready:
ready_line = (
f"[-:--:--] {Fore.YELLOW}{Style.BRIGHT}ready: "
Expand Down
24 changes: 8 additions & 16 deletions src/dwas/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@
suppress,
)
from contextvars import ContextVar
from typing import (
TYPE_CHECKING,
Deque,
Generator,
Iterator,
Optional,
TextIO,
Tuple,
)
from typing import TYPE_CHECKING, Generator, Iterator, TextIO

if TYPE_CHECKING:
from pathlib import Path
Expand All @@ -36,7 +28,7 @@ class NoOpWriter(io.TextIOWrapper):
def __init__(self) -> None:
pass

def read(self, size: Optional[int] = None) -> str: # noqa: ARG002
def read(self, size: int | None = None) -> str: # noqa: ARG002
raise io.UnsupportedOperation("Can't read from a noopwriter")

def write(self, data: str) -> int:
Expand All @@ -49,11 +41,11 @@ def flush(self) -> None:
class MemoryPipe(io.TextIOWrapper):
def __init__(
self,
writer: "PipePlexer",
writer: PipePlexer,
) -> None:
self._writer = writer

def read(self, size: Optional[int] = None) -> str: # noqa: ARG002
def read(self, size: int | None = None) -> str: # noqa: ARG002
raise io.UnsupportedOperation("can't read from a memorypipe")

def write(self, data: str) -> int:
Expand All @@ -68,7 +60,7 @@ def __init__(self, *, write_on_flush: bool = True) -> None:
self.stderr = MemoryPipe(self)
self.stdout = MemoryPipe(self)

self._buffer: Deque[Tuple[MemoryPipe, str]] = deque()
self._buffer: deque[tuple[MemoryPipe, str]] = deque()
self._write_on_flush = write_on_flush

def write(self, stream: MemoryPipe, data: str) -> int:
Expand All @@ -77,7 +69,7 @@ def write(self, stream: MemoryPipe, data: str) -> int:

def flush(
self, force_write: bool = False # noqa:FBT001,FBT002
) -> Optional[int]:
) -> int | None:
line = None

if self._write_on_flush or force_write:
Expand Down Expand Up @@ -108,7 +100,7 @@ def __init__(
self._var = var
self._log_var = log_var

def read(self, size: Optional[int] = None) -> str: # noqa: ARG002
def read(self, size: int | None = None) -> str: # noqa: ARG002
raise io.UnsupportedOperation("can't read from a memorypipe")

def write(self, data: str) -> int:
Expand Down Expand Up @@ -151,7 +143,7 @@ def redirect_streams(stdout: TextIO, stderr: TextIO) -> Iterator[None]:


@contextmanager
def log_file(path: Optional[Path]) -> Iterator[None]:
def log_file(path: Path | None) -> Iterator[None]:
with ExitStack() as stack:
if path is None:
fd: TextIO = NoOpWriter()
Expand Down
17 changes: 3 additions & 14 deletions src/dwas/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@

import logging
from types import MappingProxyType, TracebackType
from typing import (
TYPE_CHECKING,
Any,
Optional,
TextIO,
Tuple,
Type,
Union,
cast,
)
from typing import TYPE_CHECKING, Any, TextIO, cast

from colorama import Back, Fore, Style, init

Expand Down Expand Up @@ -41,10 +32,8 @@ def formatMessage(self, record: logging.LogRecord) -> str:

def formatException(
self,
ei: Union[
Tuple[Type[BaseException], BaseException, Optional[TracebackType]],
Tuple[None, None, None],
],
ei: tuple[type[BaseException], BaseException, TracebackType | None]
| tuple[None, None, None],
) -> str:
output = super().formatException(ei)
return f"{Fore.CYAN}\ndwas > " + "\ndwas > ".join(output.splitlines())
Expand Down
Loading

0 comments on commit 9494f0a

Please sign in to comment.