11from collections .abc import Iterable , Sequence
22from typing import (
3- Any ,
43 Generic ,
54 TypedDict ,
65 TypeVar ,
98T = TypeVar ("T" )
109
1110
12- # In Python 3.10: Cannot inherit from TypedDict and Generic.
13- class _Tree (TypedDict , total = False ):
14- value : Any
15- subtree : dict [str , "_Tree" ]
11+ class _Tree (TypedDict , Generic [T ], total = False ):
12+ value : T
13+ subtree : dict [str , "_Tree[T]" ]
1614
1715
1816class SuffixTree (Generic [T ]):
@@ -24,13 +22,13 @@ class SuffixTree(Generic[T]):
2422 def __init__ (self ) -> None :
2523 #: Internal structure is like... ::
2624 #:
27- #: Tree = {value?: Any ,
25+ #: Tree = {value?: T ,
2826 #: subtree?: {segmentFoo: Tree, segmentBar: Tree, ...}}
2927 #
3028 #: A Tree can have a value key, a subtree key, or both. Subtree dicts
3129 #: always have at least 1 key. Every subtree has at least one value,
3230 #: directly or indirectly. ``self._tree`` itself is a Tree.
33- self ._tree : _Tree = {}
31+ self ._tree : _Tree [ T ] = {}
3432
3533 def add (self , unambiguous_segments : Sequence [str ], value : T ) -> None :
3634 """Add an item to the tree.
@@ -48,9 +46,7 @@ def add(self, unambiguous_segments: Sequence[str], value: T) -> None:
4846 else :
4947 tree ["value" ] = value
5048
51- def add_many (
52- self , segments_and_values : Iterable [tuple [Sequence [str ], Any ]]
53- ) -> None :
49+ def add_many (self , segments_and_values : Iterable [tuple [Sequence [str ], T ]]) -> None :
5450 """Add a batch of items to the tree all at once, and collect any
5551 errors.
5652
0 commit comments