Skip to content

Commit 1b0a30b

Browse files
committed
fix pre 3.8 compatible of list[Node]
1 parent 280d110 commit 1b0a30b

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

treelib/tree.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@
4242
from six import python_2_unicode_compatible, iteritems
4343
from typing import cast, List, Any, Callable, Optional, Union
4444

45-
if sys.version_info >= (3, 9):
46-
StrList = list[str]
47-
StrLList = list[list[str]]
48-
else:
49-
StrList = List[str] # Python 3.8 and earlier
50-
StrLList = List[List[str]]
51-
5245
try:
5346
from StringIO import StringIO # type: ignore
5447
except ImportError:
@@ -64,6 +57,16 @@
6457
)
6558
from .node import Node
6659

60+
if sys.version_info >= (3, 9):
61+
StrList = list[str]
62+
StrLList = list[list[str]]
63+
NodeList = list[Node]
64+
else:
65+
StrList = List[str] # Python 3.8 and earlier
66+
StrLList = List[List[str]]
67+
NodeList = List[Node]
68+
69+
6770
__author__ = "chenxm"
6871

6972

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

368-
def all_nodes(self) -> list[Node]:
371+
def all_nodes(self) -> NodeList:
369372
"""Return all nodes in a list"""
370373
return list(self._nodes.values())
371374

@@ -408,7 +411,7 @@ def ancestor(self, nid, level=None):
408411
ascendant_level = self.level(ascendant)
409412
return None
410413

411-
def children(self, nid: str) -> list[Node]:
414+
def children(self, nid: str) -> NodeList:
412415
"""
413416
Return the children (Node) list of nid.
414417
Empty list is returned if nid does not exist
@@ -522,7 +525,7 @@ def expand_tree(
522525

523526
elif mode is self.ZIGZAG:
524527
# Suggested by Ilya Kuprik ([email protected]).
525-
stack_fw: list[Node] = []
528+
stack_fw: NodeList = []
526529
queue.reverse()
527530
stack = stack_bw = queue
528531
direction = False
@@ -583,7 +586,7 @@ def is_branch(self, nid):
583586
fpointer = []
584587
return fpointer
585588

586-
def leaves(self, nid: Optional[str] = None) -> list[Node]:
589+
def leaves(self, nid: Optional[str] = None) -> NodeList:
587590
"""Get leaves of the whole tree or a subtree."""
588591
leaves = []
589592
if nid is None:
@@ -988,7 +991,7 @@ def write(line):
988991
else:
989992
return self._reader
990993

991-
def siblings(self, nid: str) -> list[Node]:
994+
def siblings(self, nid: str) -> NodeList:
992995
"""
993996
Return the siblings of given @nid.
994997

0 commit comments

Comments
 (0)