Skip to content

Commit 719ad20

Browse files
Separate handling of default values to prevent buggy behaviour
1 parent 6f74db5 commit 719ad20

File tree

8 files changed

+1170
-785
lines changed

8 files changed

+1170
-785
lines changed

meta/generate_tag_defs.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
3030
"""
3131
text = get_template_class(tag.base)
3232

33+
# Generate default attributes dictionary
34+
default_attrs = repr({
35+
attr.name: attr.default
36+
for attr in tag.attributes
37+
})
38+
3339
# Generate attribute arguments, unions and documentation
3440
# To get a better idea of these, look inside the template files to see
3541
# what would be replaced
@@ -41,10 +47,15 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
4147
# Yucky hard-coded spaces, I can't be bothered to fix this
4248
# Also making everything optional for the sake of users always
4349
# being able to remove an attribute
44-
f" {attr.name}: Optional[{attr.type}] = {attr.default!r},"
50+
f" {attr.name}: Optional[{attr.type}] = None,"
4551
)
4652
attr_unions_gen.append(f" '{attr.name}': {attr.name},")
47-
attr_docs_gen.append(f"* {attr.name}: {attr.doc}")
53+
# Also mention default value if applicable
54+
if attr.default is not None:
55+
attr_docs_gen.append(
56+
f"* {attr.name}: {attr.doc} (defaults to {attr.default})")
57+
else:
58+
attr_docs_gen.append(f"* {attr.name}: {attr.doc}")
4859

4960
attr_args = '\n'.join(attr_args_gen).strip()
5061
attr_unions = '\n'.join(attr_unions_gen).strip()
@@ -70,7 +81,8 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
7081
.replace("{attr_unions}", attr_unions)\
7182
.replace("{attr_docs_outer}", attr_docs_outer)\
7283
.replace("{attr_docs_inner}", attr_docs_inner)\
73-
.replace("{kw_only}", kw_only)
84+
.replace("{kw_only}", kw_only)\
85+
.replace("{default_attrs}", default_attrs)
7486

7587
print(text, file=output)
7688
# And a nice trailing newline to make flake8 happy

meta/templates/class_attrs_SelfClosingTag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ def __call__(
4141
{attr_unions}
4242
}
4343
return super().__call__(**attributes)
44+
45+
def _get_default_attributes(self) -> dict[str, Any]:
46+
return {default_attrs}

meta/templates/class_attrs_StylableTag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ def __call__(
5353
{attr_unions}
5454
}
5555
return super().__call__(*children, **attributes)
56+
57+
def _get_default_attributes(self) -> dict[str, Any]:
58+
return {default_attrs}

meta/templates/class_attrs_Tag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ def __call__(
4141
{attr_unions}
4242
}
4343
return super().__call__(*children, **attributes)
44+
45+
def _get_default_attributes(self) -> dict[str, Any]:
46+
return {default_attrs}

pyhtml/__tag_base.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,29 @@ def _get_tag_name(self) -> str:
4545
"""
4646
return type(self).__name__.removesuffix('_')
4747

48+
def _get_default_attributes(self) -> dict[str, Any]:
49+
"""
50+
Returns the default attributes for the tag
51+
52+
This is overridden by child classes to return a dictionary of default
53+
attributes that are applied to the class.
54+
"""
55+
return {}
56+
4857
def _render(self) -> list[str]:
4958
"""
5059
Renders tag and its children to a list of strings where each string is
5160
a single line of output
5261
"""
62+
attributes = util.filter_attributes(util.dict_union(
63+
self._get_default_attributes(),
64+
self.attributes,
65+
))
66+
5367
# Tag and attributes
5468
opening = f"<{self._get_tag_name()}"
55-
if len(self.attributes):
56-
opening += f" {util.render_tag_attributes(self.attributes)}>"
69+
if len(attributes):
70+
opening += f" {util.render_tag_attributes(attributes)}>"
5771
else:
5872
opening += ">"
5973

0 commit comments

Comments
 (0)