Skip to content

Commit

Permalink
fix pre 3.8 compatible of list[Node]
Browse files Browse the repository at this point in the history
  • Loading branch information
caesar0301 committed Mar 1, 2025
1 parent 280d110 commit 1b0a30b
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@
from six import python_2_unicode_compatible, iteritems
from typing import cast, List, Any, Callable, Optional, Union

if sys.version_info >= (3, 9):
StrList = list[str]
StrLList = list[list[str]]
else:
StrList = List[str] # Python 3.8 and earlier
StrLList = List[List[str]]

try:
from StringIO import StringIO # type: ignore
except ImportError:
Expand All @@ -64,6 +57,16 @@
)
from .node import Node

if sys.version_info >= (3, 9):
StrList = list[str]
StrLList = list[list[str]]
NodeList = list[Node]
else:
StrList = List[str] # Python 3.8 and earlier
StrLList = List[List[str]]
NodeList = List[Node]


__author__ = "chenxm"


Expand Down Expand Up @@ -365,7 +368,7 @@ def add_node(self, node: Node, parent: Optional[Union[Node, str]] = None) -> Non
self.__update_bpointer(node.identifier, pid)
node.set_initial_tree_id(cast(str, self._identifier))

def all_nodes(self) -> list[Node]:
def all_nodes(self) -> NodeList:
"""Return all nodes in a list"""
return list(self._nodes.values())

Expand Down Expand Up @@ -408,7 +411,7 @@ def ancestor(self, nid, level=None):
ascendant_level = self.level(ascendant)
return None

def children(self, nid: str) -> list[Node]:
def children(self, nid: str) -> NodeList:
"""
Return the children (Node) list of nid.
Empty list is returned if nid does not exist
Expand Down Expand Up @@ -522,7 +525,7 @@ def expand_tree(

elif mode is self.ZIGZAG:
# Suggested by Ilya Kuprik (ilya-spy@ynadex.ru).
stack_fw: list[Node] = []
stack_fw: NodeList = []
queue.reverse()
stack = stack_bw = queue
direction = False
Expand Down Expand Up @@ -583,7 +586,7 @@ def is_branch(self, nid):
fpointer = []
return fpointer

def leaves(self, nid: Optional[str] = None) -> list[Node]:
def leaves(self, nid: Optional[str] = None) -> NodeList:
"""Get leaves of the whole tree or a subtree."""
leaves = []
if nid is None:
Expand Down Expand Up @@ -988,7 +991,7 @@ def write(line):
else:
return self._reader

def siblings(self, nid: str) -> list[Node]:
def siblings(self, nid: str) -> NodeList:
"""
Return the siblings of given @nid.
Expand Down

0 comments on commit 1b0a30b

Please sign in to comment.