Skip to content

Commit

Permalink
refactor(*): replace Dict, List with ABCs
Browse files Browse the repository at this point in the history
Adopt PEP 585
(`foo: tuple[Bar]` instead of `foo: Tuple[Bar]`).
  • Loading branch information
dbohdan committed Jan 12, 2024
1 parent c70b72a commit 17ba771
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
33 changes: 16 additions & 17 deletions src/remarshal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Mapping,
Sequence,
Tuple,
Union,
cast,
)
Expand Down Expand Up @@ -110,7 +107,7 @@ def _timestamp_constructor(loader: Any, node: Any) -> datetime.datetime:
# === CLI ===


def _argv0_to_format(argv0: str) -> Tuple[str, str]:
def _argv0_to_format(argv0: str) -> tuple[str, str]:
possible_format = "(" + "|".join(FORMATS) + ")"
match = re.search("^" + possible_format + "2" + possible_format, argv0)
from_, to = match.groups() if match else ("", "")
Expand All @@ -126,8 +123,8 @@ def _extension_to_format(path: str) -> str:
return ext if ext in FORMATS else ""


def _parse_command_line(argv: List[str]) -> argparse.Namespace: # noqa: C901.
defaults: Dict[str, Any] = {
def _parse_command_line(argv: Sequence[str]) -> argparse.Namespace: # noqa: C901.
defaults: dict[str, Any] = {
"json_indent": None,
"ordered": True,
"stringify": False,
Expand Down Expand Up @@ -371,10 +368,10 @@ def identity(x: Any) -> Any:

def traverse(
col: Any,
dict_callback: Callable[[List[Tuple[Any, Any]]], Any] = dict,
list_callback: Callable[[List[Tuple[Any, Any]]], Any] = identity,
dict_callback: Callable[[Sequence[tuple[Any, Any]]], Any] = dict,
list_callback: Callable[[Sequence[tuple[Any, Any]]], Any] = identity,
key_callback: Callable[[Any], Any] = identity,
instance_callbacks: Sequence[Tuple[type, Any]] = (),
instance_callbacks: Sequence[tuple[type, Any]] = (),
default_callback: Callable[[Any], Any] = identity,
) -> Any:
if isinstance(col, dict):
Expand Down Expand Up @@ -554,7 +551,7 @@ def _encode_json(
data: Document,
*,
ordered: bool,
indent: Union[bool, int, None],
indent: bool | int | None,
stringify: bool,
) -> str:
if indent is True:
Expand Down Expand Up @@ -637,7 +634,9 @@ def stringify_null(x: Any) -> Any:
raise ValueError(msg)


def _encode_yaml(data: Document, *, ordered: bool, yaml_options: Dict[Any, Any]) -> str:
def _encode_yaml(
data: Document, *, ordered: bool, yaml_options: Mapping[Any, Any]
) -> str:
dumper = _OrderedDumper if ordered else yaml.SafeDumper
try:
return yaml.dump(
Expand All @@ -658,10 +657,10 @@ def encode(
output_format: str,
data: Document,
*,
json_indent: Union[int, None],
json_indent: int | None,
ordered: bool,
stringify: bool,
yaml_options: Dict[Any, Any],
yaml_options: Mapping[Any, Any],
) -> bytes:
if output_format == "json":
encoded = _encode_json(
Expand Down Expand Up @@ -706,10 +705,10 @@ def remarshal(
max_values: int = DEFAULT_MAX_VALUES,
ordered: bool = True,
stringify: bool = False,
transform: Union[Callable[[Document], Document], None] = None,
unwrap: Union[str, None] = None,
wrap: Union[str, None] = None,
yaml_options: Dict[Any, Any] | None = None,
transform: Callable[[Document], Document] | None = None,
unwrap: str | None = None,
wrap: str | None = None,
yaml_options: Mapping[Any, Any] | None = None,
) -> None:
input_file = None
output_file = None
Expand Down
21 changes: 12 additions & 9 deletions tests/test_remarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
import secrets
import sys
from pathlib import Path
from typing import Any, Callable, Dict, List, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable

import cbor2 # type: ignore
import pytest

import remarshal
from remarshal.main import _argv0_to_format, _parse_command_line

if TYPE_CHECKING:
from collections.abc import Mapping, Sequence

TEST_PATH = Path(__file__).resolve().parent


Expand Down Expand Up @@ -54,17 +57,17 @@ def assert_cbor_same(output: bytes, reference: bytes) -> None:
assert output_dec == reference_dec


def sorted_dict(pairs: List[Tuple[Any, Any]]) -> Dict[Any, Any]:
def sorted_dict(pairs: Sequence[tuple[Any, Any]]) -> Mapping[Any, Any]:
return dict(sorted(pairs))


def toml_signature(data: bytes | str) -> List[str]:
def toml_signature(data: bytes | str) -> list[str]:
"""Return a lossy representation of TOML example data for comparison."""

def strip_more(line: str) -> str:
return re.sub(r" *#.*$", "", line.strip()).replace(" ", "")

def sig_lines(lst: List[str]) -> List[str]:
def sig_lines(lst: Sequence[str]) -> list[str]:
def should_drop(line: str) -> bool:
return (
line.startswith("#")
Expand All @@ -91,13 +94,13 @@ def _convert_and_read(
output_format: str,
*,
output_filename: str,
json_indent: Union[int, None] = 2,
json_indent: int | None = 2,
ordered: bool = False,
stringify: bool = False,
transform: Union[Callable[[remarshal.Document], remarshal.Document], None] = None,
unwrap: Union[str, None] = None,
wrap: Union[str, None] = None,
yaml_options: Dict[Any, Any] | None = None,
transform: Callable[[remarshal.Document], remarshal.Document] | None = None,
unwrap: str | None = None,
wrap: str | None = None,
yaml_options: Mapping[Any, Any] | None = None,
) -> bytes:
remarshal.remarshal(
input_format,
Expand Down

0 comments on commit 17ba771

Please sign in to comment.