99import textwrap
1010from typing import Any , NamedTuple , TypeVar , overload
1111
12+ from typing_extensions import Self
13+
1214from .token import Token
1315
1416
@@ -84,13 +86,13 @@ def __getitem__(self: _NodeType, item: int) -> _NodeType: ...
8486 @overload
8587 def __getitem__ (self : _NodeType , item : slice ) -> list [_NodeType ]: ...
8688
87- def __getitem__ (self : _NodeType , item : int | slice ) -> _NodeType | list [_NodeType ]:
89+ def __getitem__ (self , item : int | slice ) -> Self | list [Self ]:
8890 return self .children [item ]
8991
90- def to_tokens (self : _NodeType ) -> list [Token ]:
92+ def to_tokens (self ) -> list [Token ]:
9193 """Recover the linear token stream."""
9294
93- def recursive_collect_tokens (node : _NodeType , token_list : list [Token ]) -> None :
95+ def recursive_collect_tokens (node : Self , token_list : list [Token ]) -> None :
9496 if node .type == "root" :
9597 for child in node .children :
9698 recursive_collect_tokens (child , token_list )
@@ -108,19 +110,19 @@ def recursive_collect_tokens(node: _NodeType, token_list: list[Token]) -> None:
108110 return tokens
109111
110112 @property
111- def children (self : _NodeType ) -> list [_NodeType ]:
113+ def children (self ) -> list [Self ]:
112114 return self ._children
113115
114116 @children .setter
115- def children (self : _NodeType , value : list [_NodeType ]) -> None :
117+ def children (self , value : list [Self ]) -> None :
116118 self ._children = value
117119
118120 @property
119- def parent (self : _NodeType ) -> _NodeType | None :
121+ def parent (self ) -> Self | None :
120122 return self ._parent # type: ignore
121123
122124 @parent .setter
123- def parent (self : _NodeType , value : _NodeType | None ) -> None :
125+ def parent (self , value : Self | None ) -> None :
124126 self ._parent = value
125127
126128 @property
@@ -139,7 +141,7 @@ def is_nested(self) -> bool:
139141 return bool (self .nester_tokens )
140142
141143 @property
142- def siblings (self : _NodeType ) -> Sequence [_NodeType ]:
144+ def siblings (self ) -> Sequence [Self ]:
143145 """Get siblings of the node.
144146
145147 Gets the whole group of siblings, including self.
@@ -165,7 +167,7 @@ def type(self) -> str:
165167 return self .nester_tokens .opening .type .removesuffix ("_open" )
166168
167169 @property
168- def next_sibling (self : _NodeType ) -> _NodeType | None :
170+ def next_sibling (self ) -> Self | None :
169171 """Get the next node in the sequence of siblings.
170172
171173 Returns `None` if this is the last sibling.
@@ -176,7 +178,7 @@ def next_sibling(self: _NodeType) -> _NodeType | None:
176178 return None
177179
178180 @property
179- def previous_sibling (self : _NodeType ) -> _NodeType | None :
181+ def previous_sibling (self ) -> Self | None :
180182 """Get the previous node in the sequence of siblings.
181183
182184 Returns `None` if this is the first sibling.
@@ -241,9 +243,7 @@ def pretty(
241243 )
242244 return text
243245
244- def walk (
245- self : _NodeType , * , include_self : bool = True
246- ) -> Generator [_NodeType , None , None ]:
246+ def walk (self , * , include_self : bool = True ) -> Generator [Self , None , None ]:
247247 """Recursively yield all descendant nodes in the tree starting at self.
248248
249249 The order mimics the order of the underlying linear token
@@ -282,7 +282,7 @@ def attrs(self) -> dict[str, str | int | float]:
282282 """Html attributes."""
283283 return self ._attribute_token ().attrs
284284
285- def attrGet (self , name : str ) -> None | str | int | float :
285+ def attrGet (self , name : str ) -> str | int | float | None :
286286 """Get the value of attribute `name`, or null if it does not exist."""
287287 return self ._attribute_token ().attrGet (name )
288288
0 commit comments