Skip to content

Commit 9c4af89

Browse files
committed
Make Tag item generic over tag kind
1 parent caa2ad1 commit 9c4af89

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/bootlace/util.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections.abc import MutableMapping
1010
from collections.abc import MutableSet
1111
from typing import Any
12+
from typing import Generic
1213
from typing import Protocol
1314
from typing import TypeAlias
1415
from typing import TypeVar
@@ -269,38 +270,43 @@ def is_active_endpoint(endpoint: str, url_kwargs: Mapping[str, Any], ignore_quer
269270
return url == request.path
270271

271272

273+
H = TypeVar("H", bound=tags.html_tag)
274+
275+
272276
@attrs.define
273-
class Tag:
277+
class Tag(Generic[H]):
274278
"""A helper for creating tags.
275279
276280
Holds the tag type as well as attributes for the tag. This can be used
277281
by calling the instance as a function to create a tag, or by calling the
278282
:meth:`update` method to apply the attributes to an existing tag."""
279283

280284
#: The tag type
281-
tag: type[tags.html_tag] = attrs.field()
285+
tag: type[H] = attrs.field()
282286

283287
#: The classes to apply to the tag
284288
classes: set[str] = attrs.field(factory=set)
285289

286290
#: The attributes to apply to the tag
287291
attributes: dict[str, str] = attrs.field(factory=dict)
288292

289-
def __tag__(self) -> tags.html_tag:
293+
def __tag__(self) -> H:
290294
tag = self.tag(**self.attributes)
291295
tag.classes.add(*self.classes)
292296
return tag
293297

294-
def __call__(self, *args: Any, **kwds: Any) -> Any:
298+
def __call__(self, *args: Any, **kwds: Any) -> H:
295299
tag = self.tag(*args, **{**self.attributes, **kwds})
296300
tag.classes.add(*self.classes)
301+
return tag
297302

298303
def __setitem__(self, name: str, value: str) -> None:
299304
self.attributes[name] = value
300305

301306
def __getitem__(self, name: str) -> str:
302307
return self.attributes[name]
303308

304-
def update(self, tag: tags.html_tag) -> None:
309+
def update(self, tag: H) -> H:
305310
tag.classes.add(*self.classes)
306311
tag.attributes.update(self.attributes)
312+
return tag

0 commit comments

Comments
 (0)