Skip to content

Commit 280d110

Browse files
committed
fix pre 3.8 compatible of list[str]
1 parent 6b833d2 commit 280d110

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
fail-fast: false
1717
matrix:
18-
python-version: ["3.6", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
18+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
1919

2020
steps:
2121
- uses: actions/checkout@v4

treelib/node.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@
2727

2828
import copy
2929
import uuid
30+
import sys
3031
from collections import defaultdict
3132
from warnings import warn
32-
from typing import cast, Any, Optional, Union
33+
from typing import cast, List, Any, Optional, Union
3334

3435
from .exceptions import NodePropertyError
3536
from .misc import deprecated
3637

38+
if sys.version_info >= (3, 9):
39+
StrList = list[str]
40+
else:
41+
StrList = List[str] # Python 3.8 and earlier
42+
3743

3844
class Node(object):
3945
"""
@@ -143,7 +149,7 @@ def set_predecessor(self, nid: Optional[str], tree_id: Optional[str]) -> None:
143149
"""Set the value of `_predecessor`."""
144150
self._predecessor[tree_id] = nid
145151

146-
def successors(self, tree_id: Optional[str]):
152+
def successors(self, tree_id: Optional[str]) -> StrList:
147153
"""
148154
With a getting operator, a list of IDs of node's children is obtained. With
149155
a setting operator, the value can be list, set, or dict. For list or set,

treelib/tree.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@
3737
import codecs
3838
import json
3939
import uuid
40+
import sys
4041
from copy import deepcopy
4142
from six import python_2_unicode_compatible, iteritems
42-
from typing import cast, Any, Callable, Optional, Union
43+
from typing import cast, List, Any, Callable, Optional, Union
44+
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]]
4351

4452
try:
4553
from StringIO import StringIO # type: ignore
@@ -275,7 +283,7 @@ def __get_iter(
275283
filter_: Callable[[Node], bool],
276284
key: Optional[Callable[[Node], Node]],
277285
reverse: bool,
278-
dt,
286+
dt, # tuple[str, str, str]
279287
is_last: list,
280288
sorting: bool,
281289
):
@@ -742,7 +750,7 @@ def paste(self, nid: str, new_tree, deep: bool = False):
742750
self.__update_bpointer(new_tree.root, nid)
743751
self.__update_fpointer(nid, new_tree.root, self.node_class.ADD)
744752

745-
def paths_to_leaves(self):
753+
def paths_to_leaves(self) -> StrLList:
746754
"""
747755
Use this function to get the identifiers allowing to go from the root
748756
nodes to each leaf.

0 commit comments

Comments
 (0)