Skip to content

Commit

Permalink
Drop Python 3.8 support (typing)
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Nov 10, 2024
1 parent 88fba9d commit 4f8ed12
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 38 deletions.
21 changes: 9 additions & 12 deletions nutree/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,11 +22,7 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterator,
List,
Literal,
Type,
Union,
)

Expand Down Expand Up @@ -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]]
Expand All @@ -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]]
]

#:
Expand All @@ -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=...)`
Expand Down
3 changes: 2 additions & 1 deletion nutree/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion nutree/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions nutree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
from __future__ import annotations

import re
from collections.abc import Iterable, Iterator
from operator import attrgetter
from pathlib import Path
from typing import (
IO,
TYPE_CHECKING,
Any,
Generic,
Iterable,
Iterator,
cast,
)

Expand Down
6 changes: 2 additions & 4 deletions nutree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion nutree/tree_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions nutree/typed_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# --- Ruff Settings ------------------------------------------------------------
[tool.ruff]
target-version = "py38"
target-version = "py39"
src = ["nutree", "tests"]

[tool.ruff.lint]
Expand Down
4 changes: 2 additions & 2 deletions tests/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_typing_concept.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 4f8ed12

Please sign in to comment.