diff --git a/nutree/common.py b/nutree/common.py index cc65229..42ac785 100644 --- a/nutree/common.py +++ b/nutree/common.py @@ -13,6 +13,7 @@ import sys import warnings import zipfile +from collections.abc import Iterator from contextlib import contextmanager from enum import Enum from pathlib import Path @@ -21,11 +22,7 @@ TYPE_CHECKING, Any, Callable, - Dict, - Iterator, - List, Literal, - Type, Union, ) @@ -129,17 +126,17 @@ def __init__(self, value=None): ReprArgType = Union[str, Callable[["Node"], str]] #: A dict of scalar values -FlatJsonDictType = Dict[str, Union[str, int, float, bool, None]] +FlatJsonDictType = dict[str, Union[str, int, float, bool, None]] #: Type of ``tree.save(..., key_map)`` -KeyMapType = Dict[str, str] +KeyMapType = dict[str, str] #: Type of ``tree.save(..., value_map)`` #: E.g. `{'t': ['person', 'dept']}` -ValueMapType = Dict[str, List[str]] +ValueMapType = dict[str, list[str]] #: E.g. `{'t': {'person': 0, 'dept': 1}}` -ValueDictMapType = Dict[str, Dict[str, int]] +ValueDictMapType = dict[str, dict[str, int]] #: Generic callback for `tree.to_dot()`, ... MapperCallbackType = Callable[["Node", dict], Union[None, Any]] @@ -152,7 +149,7 @@ def __init__(self, value=None): #: Generic callback for `tree.filter()`, `tree.copy()`, ... PredicateCallbackType = Callable[ - ["Node"], Union[None, bool, IterationControl, Type[IterationControl]] + ["Node"], Union[None, bool, IterationControl, type[IterationControl]] ] #: @@ -166,9 +163,9 @@ def __init__(self, value=None): bool, SkipBranch, StopTraversal, - Type[SkipBranch], - Type[StopTraversal], - Type[StopIteration], + type[SkipBranch], + type[StopTraversal], + type[StopIteration], ], ] #: Callback for `tree.sort(key=...)` diff --git a/nutree/dot.py b/nutree/dot.py index 7c0dc9a..c2b678d 100644 --- a/nutree/dot.py +++ b/nutree/dot.py @@ -7,8 +7,9 @@ from __future__ import annotations +from collections.abc import Iterator from pathlib import Path -from typing import IO, TYPE_CHECKING, Any, Iterator +from typing import IO, TYPE_CHECKING, Any from nutree.common import MapperCallbackType, call_mapper diff --git a/nutree/mermaid.py b/nutree/mermaid.py index 5d8d78b..b53695a 100644 --- a/nutree/mermaid.py +++ b/nutree/mermaid.py @@ -9,9 +9,10 @@ from __future__ import annotations import io +from collections.abc import Iterable, Iterator from pathlib import Path from subprocess import CalledProcessError, check_output -from typing import IO, TYPE_CHECKING, Callable, Iterable, Iterator, Literal +from typing import IO, TYPE_CHECKING, Callable, Literal from nutree.common import DataIdType diff --git a/nutree/node.py b/nutree/node.py index c83681c..07fbfd5 100644 --- a/nutree/node.py +++ b/nutree/node.py @@ -13,6 +13,7 @@ from __future__ import annotations import re +from collections.abc import Iterable, Iterator from operator import attrgetter from pathlib import Path from typing import ( @@ -20,8 +21,6 @@ TYPE_CHECKING, Any, Generic, - Iterable, - Iterator, cast, ) diff --git a/nutree/tree.py b/nutree/tree.py index 0eef9d8..2945fae 100644 --- a/nutree/tree.py +++ b/nutree/tree.py @@ -15,15 +15,13 @@ import json import random import threading +from collections.abc import Iterable, Iterator from pathlib import Path from typing import ( IO, Any, Generic, - Iterable, - Iterator, Literal, - Type, Union, cast, ) @@ -111,7 +109,7 @@ class Tree(Generic[TData, TNode]): **Note:** Use with care, see also :ref:`forward-attributes`. """ - node_factory: Type[TNode] = cast(Type[TNode], Node) + node_factory: type[TNode] = cast(type[TNode], Node) root_node_factory = _SystemRootNode #: Default connector prefixes ``format(style=...)`` argument. diff --git a/nutree/tree_generator.py b/nutree/tree_generator.py index 9aece65..73748c0 100644 --- a/nutree/tree_generator.py +++ b/nutree/tree_generator.py @@ -9,8 +9,9 @@ import random import sys from abc import ABC, abstractmethod +from collections.abc import Sequence from datetime import date, datetime, timedelta, timezone -from typing import Any, Sequence, Union +from typing import Any, Union from nutree.common import DictWrapper from nutree.node import Node diff --git a/nutree/typed_tree.py b/nutree/typed_tree.py index 6581f56..8925bf9 100644 --- a/nutree/typed_tree.py +++ b/nutree/typed_tree.py @@ -15,8 +15,9 @@ from __future__ import annotations from collections import Counter +from collections.abc import Iterator from pathlib import Path -from typing import IO, Iterator, Type, cast, final +from typing import IO, cast, final # typing.Self requires Python 3.11 from typing_extensions import Any, Self @@ -100,7 +101,7 @@ def __repr__(self) -> str: def kind(self) -> str: return self._kind - def get_children(self, kind: str | Type[ANY_KIND]) -> list[Self]: + def get_children(self, kind: str | type[ANY_KIND]) -> list[Self]: """Return list of direct child nodes of a given type (list may be empty).""" all_children = self._children if not all_children: @@ -109,7 +110,7 @@ def get_children(self, kind: str | Type[ANY_KIND]) -> list[Self]: return all_children return list(filter(lambda n: n._kind == kind, all_children)) - def first_child(self, kind: str | Type[ANY_KIND]) -> Self | None: + def first_child(self, kind: str | type[ANY_KIND]) -> Self | None: """First direct child node or None if no children exist.""" all_children = self._children if not all_children: @@ -122,7 +123,7 @@ def first_child(self, kind: str | Type[ANY_KIND]) -> Self | None: return n return None - def last_child(self, kind: str | Type[ANY_KIND]) -> Self | None: + def last_child(self, kind: str | type[ANY_KIND]) -> Self | None: """Last direct child node or None if no children exist.""" all_children = self._children if not all_children: @@ -136,7 +137,7 @@ def last_child(self, kind: str | Type[ANY_KIND]) -> Self | None: return n return None - def has_children(self, kind: str | Type[ANY_KIND]) -> bool: + def has_children(self, kind: str | type[ANY_KIND]) -> bool: """Return true if this node has one or more children.""" if kind is ANY_KIND: return bool(self._children) @@ -283,7 +284,7 @@ def add_child( source_node: Self = None # type: ignore new_node: Self = None # type: ignore - factory: Type[Self] = self._tree.node_factory # type: ignore + factory: type[Self] = self._tree.node_factory # type: ignore if isinstance(child, TypedNode): if deep is None: @@ -620,15 +621,15 @@ def add( node_id=node_id, ) - def first_child(self, kind: str | Type[ANY_KIND]) -> TypedNode[TData] | None: + def first_child(self, kind: str | type[ANY_KIND]) -> TypedNode[TData] | None: """Return the first toplevel node.""" return self.system_root.first_child(kind=kind) - def last_child(self, kind: str | Type[ANY_KIND]) -> TypedNode[TData] | None: + def last_child(self, kind: str | type[ANY_KIND]) -> TypedNode[TData] | None: """Return the last toplevel node.""" return self.system_root.last_child(kind=kind) - def iter_by_type(self, kind: str | Type[ANY_KIND]) -> Iterator[TypedNode[TData]]: + def iter_by_type(self, kind: str | type[ANY_KIND]) -> Iterator[TypedNode[TData]]: if kind == ANY_KIND: yield from self.iterator() for n in self.iterator(): diff --git a/pyproject.toml b/pyproject.toml index 6a02e3a..9aadbbd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ # --- Ruff Settings ------------------------------------------------------------ [tool.ruff] -target-version = "py38" +target-version = "py39" src = ["nutree", "tests"] [tool.ruff.lint] diff --git a/tests/fixture.py b/tests/fixture.py index 3ef055c..d347df9 100644 --- a/tests/fixture.py +++ b/tests/fixture.py @@ -15,7 +15,7 @@ import uuid from random import randint from textwrap import dedent, indent -from typing import IO, Any, List +from typing import IO, Any from nutree.common import ReprArgType from nutree.tree import Node, Tree @@ -233,7 +233,7 @@ def create_typed_tree_simple( return tree -def generate_tree(level_defs: List[int]) -> Tree: +def generate_tree(level_defs: list[int]) -> Tree: """Generate a tree. Example: diff --git a/tests/test_serialize.py b/tests/test_serialize.py index 53aec5c..e919281 100644 --- a/tests/test_serialize.py +++ b/tests/test_serialize.py @@ -11,7 +11,7 @@ import pprint import tempfile import zipfile -from typing import Any, Tuple +from typing import Any import pytest from nutree import Node, Tree @@ -21,7 +21,7 @@ from . import fixture -def _get_fp_result(fp, *, do_print=True, assert_len: int) -> Tuple[str, dict]: +def _get_fp_result(fp, *, do_print=True, assert_len: int) -> tuple[str, dict]: fp.seek(0) text = fp.read() data = json.loads(text) diff --git a/tests/test_typing_concept.py b/tests/test_typing_concept.py index b8c0812..908e9b8 100644 --- a/tests/test_typing_concept.py +++ b/tests/test_typing_concept.py @@ -6,7 +6,7 @@ from __future__ import annotations -from typing import Generic, List, Type, cast +from typing import Generic, cast from uuid import UUID, uuid4 from typing_extensions import Any, Self, TypeVar, reveal_type @@ -19,7 +19,7 @@ class Node(Generic[TData]): def __init__(self, data: TData, parent: Self): self.data: TData = data self.parent: Self = parent - self.children: List[Self] = [] + self.children: list[Self] = [] def add(self, data: TData) -> Self: node = self.__class__(data, self) @@ -28,7 +28,7 @@ def add(self, data: TData) -> Self: class Tree(Generic[TData, TNode]): - node_factory: Type[TNode] = cast(Type[TNode], Node) + node_factory: type[TNode] = cast(type[TNode], Node) def __init__(self): self._root: Node = self.node_factory("__root__", None) # type: ignore