Skip to content

Commit 67efedf

Browse files
Add type constraints and support generator expressions
1 parent 01383ce commit 67efedf

17 files changed

+1893
-1654
lines changed

meta/generate_tag_defs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
4747
# Yucky hard-coded spaces, I can't be bothered to fix this
4848
# Also making everything optional for the sake of users always
4949
# being able to remove an attribute
50-
f" {attr.name}: Optional[{attr.type}] = None,"
50+
f" {attr.name}: {attr.type} = None,"
5151
)
5252
attr_unions_gen.append(f" '{attr.name}': {attr.name},")
5353
# Also mention default value if applicable

meta/scrape_tags.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def attr_entries_to_object(tags: TagsYaml, tag_name: str) -> list[Attr]:
380380
if isinstance(value, str):
381381
doc: Optional[str] = value
382382
default: Optional[str] = None
383-
type = "Any"
383+
type = "AttributeType"
384384
else:
385385
doc = value.get("doc")
386386
if "default" in value:
@@ -389,7 +389,7 @@ def attr_entries_to_object(tags: TagsYaml, tag_name: str) -> list[Attr]:
389389
default = eval(value["default"])
390390
else:
391391
default = None
392-
type = value.get("type", "Any")
392+
type = value.get("type", "AttributeType")
393393
attrs.append(Attr(name, doc, type, default))
394394
return attrs
395395

meta/tags.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,43 @@ base:
1414
link:
1515
base: SelfClosingTag
1616
attributes:
17-
href: Location of the file being linked to
18-
rel: Kind of file being loaded (eg `"stylesheet"`)
17+
href:
18+
doc: Location of the file being linked to
19+
type: Optional[str]
20+
rel:
21+
doc: Kind of file being loaded (eg `"stylesheet"`)
22+
type: Optional[str]
1923

2024
a:
2125
base: StylableTag
2226
attributes:
23-
href: URL of page to link to
24-
target: Use "_blank" to open in a new tab
27+
href:
28+
doc: URL of page to link to
29+
type: Optional[str]
30+
target:
31+
doc: Use "_blank" to open in a new tab
32+
type: "Union[str, Literal['_self', '_blank', '_parent', '_top'], None]"
2533

2634
script:
2735
attributes:
2836
type:
2937
doc: Type of script to use
3038
default: "'text/javascript'"
39+
type: Optional[str]
3140

3241
style:
3342
attributes:
3443
type:
3544
doc: Type of style to use
3645
default: "'text/css'"
46+
type: Optional[str]
3747

3848
form:
3949
attributes:
4050
method:
4151
doc: The HTTP request method to use when submitting this form. In almost all cases, you'll want this to be POST.
42-
default: "'POST'"
52+
default: "'post'"
53+
type: "Optional[Literal['post', 'get']]"
4354
action: The URL to request to when submitting this form. By default, requests will be sent to the same URL as the current page.
4455

4556
input:

meta/templates/class_attrs_SelfClosingTag.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(
1010
self,
1111
{kw_only}
1212
{attr_args}
13-
**attributes: Any,
13+
**attributes: AttributeType,
1414
) -> None:
1515
"""
1616
{description}
@@ -24,11 +24,11 @@ def __init__(
2424
}
2525
super().__init__(**attributes)
2626

27-
def __call__(
27+
def __call__( # type: ignore
2828
self,
2929
{kw_only}
3030
{attr_args}
31-
**attributes: Any,
31+
**attributes: AttributeType,
3232
):
3333
"""
3434
{description}
@@ -42,5 +42,5 @@ def __call__(
4242
}
4343
return super().__call__(**attributes)
4444

45-
def _get_default_attributes(self, given: dict[str, Any]) -> dict[str, Any]:
45+
def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str, AttributeType]:
4646
return {default_attrs}

meta/templates/class_attrs_StylableTag.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class {name}({base}):
1+
class {name}(Tag):
22
"""
33
{description}
44
@@ -8,12 +8,12 @@ class {name}({base}):
88
"""
99
def __init__(
1010
self,
11-
*children,
11+
*children: ChildrenType,
1212
{attr_args}
13-
id: Any = None,
14-
_class: Any = None,
15-
style: Any = None,
16-
**attributes: Any,
13+
id: Optional[str] = None,
14+
_class: Optional[str] = None,
15+
style: Optional[str] = None,
16+
**attributes: AttributeType,
1717
) -> None:
1818
"""
1919
{description}
@@ -30,14 +30,14 @@ def __init__(
3030
}
3131
super().__init__(*children, **attributes)
3232

33-
def __call__(
33+
def __call__( # type: ignore
3434
self,
35-
*children,
35+
*children: ChildrenType,
3636
{attr_args}
37-
id: Any = None,
38-
_class: Any = None,
39-
style: Any = None,
40-
**attributes: Any,
37+
id: Optional[str] = None,
38+
_class: Optional[str] = None,
39+
style: Optional[str] = None,
40+
**attributes: AttributeType,
4141
):
4242
"""
4343
{description}
@@ -54,5 +54,5 @@ def __call__(
5454
}
5555
return super().__call__(*children, **attributes)
5656

57-
def _get_default_attributes(self, given: dict[str, Any]) -> dict[str, Any]:
57+
def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str, AttributeType]:
5858
return {default_attrs}

meta/templates/class_attrs_Tag.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ class {name}({base}):
88
"""
99
def __init__(
1010
self,
11-
*children: Any,
11+
*children: ChildrenType,
1212
{attr_args}
13-
**attributes: Any,
13+
**attributes: AttributeType,
1414
) -> None:
1515
"""
1616
{description}
@@ -24,11 +24,11 @@ def __init__(
2424
}
2525
super().__init__(*children, **attributes)
2626

27-
def __call__(
27+
def __call__( # type: ignore
2828
self,
29-
*children: Any,
29+
*children: ChildrenType,
3030
{attr_args}
31-
**attributes: Any,
31+
**attributes: AttributeType,
3232
):
3333
"""
3434
{description}
@@ -42,5 +42,5 @@ def __call__(
4242
}
4343
return super().__call__(*children, **attributes)
4444

45-
def _get_default_attributes(self, given: dict[str, Any]) -> dict[str, Any]:
45+
def _get_default_attributes(self, given: dict[str, AttributeType]) -> dict[str, AttributeType]:
4646
return {default_attrs}

meta/templates/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# type: ignore
2-
# flake8: noqa
31
"""
42
# PyHTML Enhanced / Tags / Generated
53
@@ -9,5 +7,6 @@
97
108
https://creativecommons.org/licenses/by-sa/2.5/
119
"""
12-
from typing import Any, Optional
13-
from ..__tag_base import Tag, SelfClosingTag, StylableTag
10+
from typing import Any, Optional, Union, Literal
11+
from ..__tag_base import Tag, SelfClosingTag
12+
from ..__types import AttributeType, ChildrenType

0 commit comments

Comments
 (0)