|
9 | 9 | from collections.abc import MutableMapping
|
10 | 10 | from collections.abc import MutableSet
|
11 | 11 | from typing import Any
|
| 12 | +from typing import Generic |
12 | 13 | from typing import Protocol
|
13 | 14 | from typing import TypeAlias
|
14 | 15 | from typing import TypeVar
|
@@ -269,38 +270,43 @@ def is_active_endpoint(endpoint: str, url_kwargs: Mapping[str, Any], ignore_quer
|
269 | 270 | return url == request.path
|
270 | 271 |
|
271 | 272 |
|
| 273 | +H = TypeVar("H", bound=tags.html_tag) |
| 274 | + |
| 275 | + |
272 | 276 | @attrs.define
|
273 |
| -class Tag: |
| 277 | +class Tag(Generic[H]): |
274 | 278 | """A helper for creating tags.
|
275 | 279 |
|
276 | 280 | Holds the tag type as well as attributes for the tag. This can be used
|
277 | 281 | by calling the instance as a function to create a tag, or by calling the
|
278 | 282 | :meth:`update` method to apply the attributes to an existing tag."""
|
279 | 283 |
|
280 | 284 | #: The tag type
|
281 |
| - tag: type[tags.html_tag] = attrs.field() |
| 285 | + tag: type[H] = attrs.field() |
282 | 286 |
|
283 | 287 | #: The classes to apply to the tag
|
284 | 288 | classes: set[str] = attrs.field(factory=set)
|
285 | 289 |
|
286 | 290 | #: The attributes to apply to the tag
|
287 | 291 | attributes: dict[str, str] = attrs.field(factory=dict)
|
288 | 292 |
|
289 |
| - def __tag__(self) -> tags.html_tag: |
| 293 | + def __tag__(self) -> H: |
290 | 294 | tag = self.tag(**self.attributes)
|
291 | 295 | tag.classes.add(*self.classes)
|
292 | 296 | return tag
|
293 | 297 |
|
294 |
| - def __call__(self, *args: Any, **kwds: Any) -> Any: |
| 298 | + def __call__(self, *args: Any, **kwds: Any) -> H: |
295 | 299 | tag = self.tag(*args, **{**self.attributes, **kwds})
|
296 | 300 | tag.classes.add(*self.classes)
|
| 301 | + return tag |
297 | 302 |
|
298 | 303 | def __setitem__(self, name: str, value: str) -> None:
|
299 | 304 | self.attributes[name] = value
|
300 | 305 |
|
301 | 306 | def __getitem__(self, name: str) -> str:
|
302 | 307 | return self.attributes[name]
|
303 | 308 |
|
304 |
| - def update(self, tag: tags.html_tag) -> None: |
| 309 | + def update(self, tag: H) -> H: |
305 | 310 | tag.classes.add(*self.classes)
|
306 | 311 | tag.attributes.update(self.attributes)
|
| 312 | + return tag |
0 commit comments