Skip to content

Commit 56487c6

Browse files
committed
Convert TagList to inherit collections.UserList, instead of typing.List
1 parent 81e0749 commit 56487c6

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

htmltools/_core.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
import tempfile
1212
import urllib.parse
1313
import webbrowser
14-
from collections import UserString
14+
from collections import UserList, UserString
1515
from copy import copy, deepcopy
1616
from pathlib import Path
1717
from typing import (
1818
Any,
1919
Callable,
2020
Dict,
2121
Iterable,
22-
List,
2322
Mapping,
2423
Optional,
2524
Sequence,
@@ -255,7 +254,7 @@ def _repr_html_(self) -> str: ...
255254
# =============================================================================
256255
# TagList class
257256
# =============================================================================
258-
class TagList(List[TagNode]):
257+
class TagList(UserList[TagNode]):
259258
"""
260259
Create an HTML tag list (i.e., a fragment of HTML)
261260
@@ -275,26 +274,26 @@ class TagList(List[TagNode]):
275274
def __init__(self, *args: TagChild) -> None:
276275
super().__init__(_tagchilds_to_tagnodes(args))
277276

278-
def extend(self, x: Iterable[TagChild]) -> None:
277+
def extend(self, other: Iterable[TagChild]) -> None:
279278
"""
280279
Extend the children by appending an iterable of children.
281280
"""
282281

283-
super().extend(_tagchilds_to_tagnodes(x))
282+
super().extend(_tagchilds_to_tagnodes(other))
284283

285-
def append(self, *args: TagChild) -> None:
284+
def append(self, item: TagChild, *args: TagChild) -> None:
286285
"""
287286
Append tag children to the end of the list.
288287
"""
289288

290-
self.extend(args)
289+
self.extend([item, *args])
291290

292-
def insert(self, index: SupportsIndex, x: TagChild) -> None:
291+
def insert(self, i: SupportsIndex, item: TagChild) -> None:
293292
"""
294293
Insert tag children before a given index.
295294
"""
296295

297-
self[index:index] = _tagchilds_to_tagnodes([x])
296+
self.data[i:i] = _tagchilds_to_tagnodes([item])
298297

299298
def tagify(self) -> "TagList":
300299
"""
@@ -306,16 +305,16 @@ def tagify(self) -> "TagList":
306305
# Iterate backwards because if we hit a Tagifiable object, it may be replaced
307306
# with 0, 1, or more items (if it returns TagList).
308307
for i in reversed(range(len(cp))):
309-
child = cp[i]
308+
child = cp.data[i]
310309

311310
if isinstance(child, Tagifiable):
312311
tagified_child = child.tagify()
313312
if isinstance(tagified_child, TagList):
314313
# If the Tagifiable object returned a TagList, flatten it into this
315314
# one.
316-
cp[i : i + 1] = _tagchilds_to_tagnodes(tagified_child)
315+
cp.data[i : i + 1] = _tagchilds_to_tagnodes(tagified_child)
317316
else:
318-
cp[i] = tagified_child
317+
cp.data[i] = tagified_child
319318

320319
elif isinstance(child, MetadataNode):
321320
cp[i] = copy(child)
@@ -342,7 +341,7 @@ def save_html(
342341
The path to the generated HTML file.
343342
"""
344343

345-
return HTMLDocument(self).save_html(
344+
return HTMLDocument(self.data).save_html(
346345
file, libdir=libdir, include_version=include_version
347346
)
348347

@@ -382,7 +381,7 @@ def get_html_string(
382381
first_child = True
383382
prev_was_add_ws = add_ws
384383

385-
for child in self:
384+
for child in self.data:
386385
if isinstance(child, MetadataNode):
387386
continue
388387

@@ -447,7 +446,7 @@ def get_dependencies(self, *, dedup: bool = True) -> list["HTMLDependency"]:
447446
"""
448447

449448
deps: list[HTMLDependency] = []
450-
for x in self:
449+
for x in self.data:
451450
if isinstance(x, HTMLDependency):
452451
deps.append(x)
453452
elif isinstance(x, Tag):

htmltools/_util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ def flatten(x: Iterable[Union[T, None]]) -> list[T]:
8686
# Having this separate function and passing along `result` is faster than defining
8787
# a closure inside of `flatten()` (and not passing `result`).
8888
def _flatten_recurse(x: Iterable[T | None], result: list[T]) -> None:
89+
from ._core import TagList
90+
8991
for item in x:
90-
if isinstance(item, (list, tuple)):
92+
if isinstance(item, TagList):
93+
_flatten_recurse(item, result)
94+
elif isinstance(item, (list, tuple)):
9195
# Don't yet know how to specify recursive generic types, so we'll tell
9296
# the type checker to ignore this line.
9397
_flatten_recurse(item, result) # pyright: ignore[reportUnknownArgumentType]

0 commit comments

Comments
 (0)