Skip to content

Commit 1aaaf1a

Browse files
authored
Merge pull request #24 from leonardbinet/mypy
mypy - use alias for readibility
2 parents be8b832 + 9d9202a commit 1aaaf1a

File tree

2 files changed

+23
-30
lines changed

2 files changed

+23
-30
lines changed

lighttree/tree.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
from .utils import STYLES
2323

2424

25+
Key = Union[None, str, int]
26+
KeyedNode = Tuple[Key, Node]
27+
KeyedTree = Tuple[Key, "Tree"]
28+
29+
2530
class Tree(object):
2631

2732
"""Principles:
@@ -57,9 +62,7 @@ def __init__(self, path_separator: str = ".") -> None:
5762
def __contains__(self, identifier: str) -> bool:
5863
return identifier in self._nodes_map
5964

60-
def get(
61-
self, nid: str, by_path: bool = False
62-
) -> Tuple[Union[None, str, int], Node]:
65+
def get(self, nid: str, by_path: bool = False) -> KeyedNode:
6366
"""Get a node by its id.
6467
:param nid: str, identifier of node to fetch
6568
:param by_path: bool, if True nid is the path to the node
@@ -86,9 +89,7 @@ def child_id(self, nid: str, key: Union[str, int], by_path: bool = False) -> str
8689
raise ValueError("Expected integer key, got %s" % key)
8790
return self._nodes_children_list[nid][int(key)]
8891

89-
def child(
90-
self, nid: str, key: Union[int, str], by_path: bool = False
91-
) -> Tuple[Union[None, str, int], Node]:
92+
def child(self, nid: str, key: Union[int, str], by_path: bool = False) -> KeyedNode:
9293
return self.get(self.child_id(nid, key, by_path=by_path))
9394

9495
def get_node_id_by_path(self, path: str) -> str:
@@ -114,7 +115,7 @@ def get_path(self, nid: str) -> str:
114115
]
115116
)
116117

117-
def get_key(self, nid: str) -> Union[int, str, None]:
118+
def get_key(self, nid: str) -> Key:
118119
"""Get a node's key.
119120
:param nid: str, identifier of node
120121
@@ -135,7 +136,7 @@ def list(
135136
id_in: Optional[Sequence[str]] = None,
136137
depth_in: Optional[Sequence[int]] = None,
137138
filter_: Optional[Callable[[Node], bool]] = None,
138-
) -> List[Tuple[Union[None, int, str], Node]]:
139+
) -> List[KeyedNode]:
139140
"""List nodes.
140141
:param id_in: list of str, optional, filter nodes among provided identifiers
141142
:param depth_in: list of int, optional, filter nodes whose depth in tree is among provided values
@@ -234,7 +235,7 @@ def clone(
234235
new_tree.insert_node(node, parent_id=pid, key=key)
235236
return new_tree
236237

237-
def parent(self, nid: str) -> Tuple[Union[None, str, int], Node]:
238+
def parent(self, nid: str) -> KeyedNode:
238239
"""Return parent node.
239240
Return None if given node id is root.
240241
"""
@@ -255,9 +256,7 @@ def parent_id(self, nid: str, by_path: bool = False) -> str:
255256
raise NotFoundNodeError()
256257
return parent_id
257258

258-
def children(
259-
self, nid: str, by_path: bool = False
260-
) -> List[Tuple[Union[None, str, int], Node]]:
259+
def children(self, nid: str, by_path: bool = False) -> List[KeyedNode]:
261260
"""Return set of given node children node ids."""
262261
return [self.get(id_) for id_ in self.children_ids(nid, by_path=by_path)]
263262

@@ -266,9 +265,7 @@ def children_ids(self, nid: str, by_path: bool = False) -> List[str]:
266265
return list(self._nodes_children_map[nid].keys())
267266
return list(self._nodes_children_list[nid])
268267

269-
def siblings(
270-
self, nid: str, by_path: bool = False
271-
) -> List[Tuple[Union[None, str, int], Node]]:
268+
def siblings(self, nid: str, by_path: bool = False) -> List[KeyedNode]:
272269
"""Return set of ids of nodes that share the provided node's parent."""
273270
return [self.get(id_) for id_ in self.siblings_ids(nid, by_path=by_path)]
274271

@@ -297,7 +294,7 @@ def ancestors(
297294
from_root: bool = False,
298295
include_current: bool = False,
299296
by_path: bool = False,
300-
) -> List[Tuple[Union[None, str, int], Node]]:
297+
) -> List[KeyedNode]:
301298
"""From element to root.
302299
:param nid:
303300
:param from_root:
@@ -332,9 +329,7 @@ def ancestors_ids(
332329
ancestor_ids = list(reversed(ancestor_ids))
333330
return ancestor_ids
334331

335-
def subtree(
336-
self, nid: str, deep: bool = False, by_path: bool = False
337-
) -> Tuple[Union[None, str, int], "Tree"]:
332+
def subtree(self, nid: str, deep: bool = False, by_path: bool = False) -> KeyedTree:
338333
if by_path:
339334
nid = self.get_node_id_by_path(nid)
340335
t = self.clone(with_nodes=True, new_root=nid, deep=deep)
@@ -344,7 +339,7 @@ def subtree(
344339

345340
def leaves(
346341
self, nid: Optional[str] = None, by_path: bool = False
347-
) -> List[Tuple[Union[None, str, int], Node]]:
342+
) -> List[KeyedNode]:
348343
"""Return leaves under a node subtree."""
349344
return [self.get(id_) for id_ in self.leaves_ids(nid, by_path=by_path)]
350345

@@ -395,7 +390,7 @@ def insert_node(
395390
child_id: Optional[str] = None,
396391
key: Optional[Union[int, str]] = None,
397392
by_path: bool = False,
398-
) -> Union[None, int, str]:
393+
) -> Key:
399394
"""Insert node, return key
400395
:param node:
401396
:param parent_id:
@@ -498,7 +493,7 @@ def insert_tree(
498493
child_id_below: Optional[str] = None,
499494
key: Optional[Union[str, int]] = None,
500495
by_path: bool = False,
501-
) -> Union[int, str, None]:
496+
) -> Key:
502497
"""Return new key"""
503498
self._validate_tree_insertion(new_tree)
504499
if new_tree.root is None:
@@ -580,7 +575,7 @@ def _insert_tree_above(
580575
self._insert_tree_below(new_tree, parent_id, key=subtree_key, by_path=False)
581576
self._insert_tree_below(child_subtree, child_id_below, key=key, by_path=False)
582577

583-
def _drop_node(self, nid: str) -> Tuple[Union[None, str, int], Node]:
578+
def _drop_node(self, nid: str) -> KeyedNode:
584579
"""Return key, node"""
585580
if self.children_ids(nid):
586581
raise ValueError("Cannot drop node having children.")
@@ -611,7 +606,7 @@ def drop_node(
611606
nid: str,
612607
with_children: bool = True,
613608
by_path: bool = False,
614-
) -> Tuple[Union[None, str, int], Node]:
609+
) -> KeyedNode:
615610
"""If with_children is False, children of this node will take as new parent the dropped node parent.
616611
Possible only if node type is same as parent node type.
617612
@@ -645,9 +640,7 @@ def drop_node(
645640
self._insert_tree_below(new_tree=st, parent_id=pid, key=k, by_path=False)
646641
return removed_key, node
647642

648-
def drop_subtree(
649-
self, nid: str, by_path: bool = False
650-
) -> Tuple[Union[None, str, int], "Tree"]:
643+
def drop_subtree(self, nid: str, by_path: bool = False) -> KeyedTree:
651644
if by_path:
652645
nid = self.get_node_id_by_path(nid)
653646
self._ensure_present(nid)
@@ -663,7 +656,7 @@ def expand_tree(
663656
filter_: Optional[Callable[[Union[None, str, int], Node], bool]] = None,
664657
filter_through: bool = False,
665658
reverse: bool = False,
666-
) -> Iterable[Tuple[Union[None, int, str], Node]]:
659+
) -> Iterable[KeyedNode]:
667660
"""Python generator traversing the tree (or a subtree) with optional node filtering.
668661
669662
Inspired by treelib implementation https://github.com/caesar0301/treelib/blob/master/treelib/tree.py#L374
@@ -781,7 +774,7 @@ def _iter_nodes_with_location(
781774
filter_: Optional[Callable[[Node], bool]],
782775
reverse: bool,
783776
is_last_list: Optional[List[bool]] = None,
784-
) -> Iterable[Tuple[Tuple[bool, ...], Union[None, int, str], Node]]:
777+
) -> Iterable[Tuple[Tuple[bool, ...], Key, Node]]:
785778
"""Yield nodes with information on how they are placed.
786779
:param nid: starting node identifier
787780
:param filter_: filter function applied on nodes

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from setuptools import setup
55

6-
__version__ = "1.1.0"
6+
__version__ = "1.1.1"
77

88
develop_requires = [
99
"pre-commit",

0 commit comments

Comments
 (0)