From 72dce11206304a51f46209f68d2dd003756da5b9 Mon Sep 17 00:00:00 2001 From: teutoburg Date: Sat, 7 Sep 2024 18:07:54 +0200 Subject: [PATCH] Use Python 3.10 typing feature, black styling indentations --- astar_utils/nested_mapping.py | 32 ++++++++++++++++++++++---------- astar_utils/unique_list.py | 4 ++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/astar_utils/nested_mapping.py b/astar_utils/nested_mapping.py index 6b87723..e4e8ce7 100644 --- a/astar_utils/nested_mapping.py +++ b/astar_utils/nested_mapping.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Contains NestedMapping class.""" -from typing import TextIO, Optional, Union, Any +from typing import TextIO, Any from io import StringIO from collections import abc, ChainMap @@ -16,8 +16,11 @@ class NestedMapping(abc.MutableMapping): # TODO: improve docstring """Dictionary-like structure that supports nested !-bang string keys.""" - def __init__(self, new_dict: Optional[abc.Iterable] = None, - title: Optional[str] = None): + def __init__( + self, + new_dict: abc.Iterable | None = None, + title: str | None = None, + ): self.dic: abc.MutableMapping[str, Any] = {} self._title = title if isinstance(new_dict, abc.MutableMapping): @@ -131,8 +134,10 @@ def _guard_submapping(entry, key_chunks, kind: str = "get") -> None: f"self['!{'.'.join(key_chunks)}']`` first and then optionally " "re-assign a new sub-mapping to the key.") - def _staggered_items(self, key: Union[str, None], - value: abc.Mapping) -> abc.Iterator[tuple[str, Any]]: + def _staggered_items( + self, key: str | None, + value: abc.Mapping, + ) -> abc.Iterator[tuple[str, Any]]: simple = [] for subkey, subvalue in value.items(): new_key = self._join_subkey(key, subkey) @@ -157,9 +162,12 @@ def _write_subkey(key: str, pre: str, final: bool, stream: TextIO) -> str: stream.write(f"{newpre}{key}: ") return newpre - def _write_subitems(self, items: abc.Collection[tuple[str, Any]], pre: str, - stream: TextIO, nested: bool = False - ) -> list[tuple[str, Any]]: + def _write_subitems( + self, items: abc.Collection[tuple[str, Any]], + pre: str, + stream: TextIO, + nested: bool = False, + ) -> list[tuple[str, Any]]: # TODO: could this (and _write_subdict) use _staggered_items instead?? n_items = len(items) simple: list[tuple[str, Any]] = [] @@ -180,8 +188,12 @@ def _write_subitems(self, items: abc.Collection[tuple[str, Any]], pre: str, return simple - def _write_subdict(self, subdict: abc.Mapping, stream: TextIO, - pad: str = "") -> None: + def _write_subdict( + self, + subdict: abc.Mapping, + stream: TextIO, + pad: str = "", + ) -> None: pre = pad.replace("├─", "│ ").replace("└─", " ") simple = self._write_subitems(subdict.items(), pre, stream, True) self._write_subitems(simple, pre, stream) diff --git a/astar_utils/unique_list.py b/astar_utils/unique_list.py index e042dec..36b323f 100644 --- a/astar_utils/unique_list.py +++ b/astar_utils/unique_list.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Contains UniqueList class.""" -from typing import Optional, Any +from typing import Any from collections.abc import Iterable, MutableSequence @@ -19,7 +19,7 @@ class UniqueList(MutableSequence): items. """ - def __init__(self, initial: Optional[Iterable[Any]] = None): + def __init__(self, initial: Iterable[Any] | None = None): self._set: set[Any] = set() # For uniqueness self._list: list[Any] = [] # For order