Skip to content

Commit

Permalink
Separate handling of default values to prevent buggy behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyGuthridge committed Oct 3, 2023
1 parent 6f74db5 commit 719ad20
Show file tree
Hide file tree
Showing 8 changed files with 1,170 additions and 785 deletions.
18 changes: 15 additions & 3 deletions meta/generate_tag_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
"""
text = get_template_class(tag.base)

# Generate default attributes dictionary
default_attrs = repr({
attr.name: attr.default
for attr in tag.attributes
})

# Generate attribute arguments, unions and documentation
# To get a better idea of these, look inside the template files to see
# what would be replaced
Expand All @@ -41,10 +47,15 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
# Yucky hard-coded spaces, I can't be bothered to fix this
# Also making everything optional for the sake of users always
# being able to remove an attribute
f" {attr.name}: Optional[{attr.type}] = {attr.default!r},"
f" {attr.name}: Optional[{attr.type}] = None,"
)
attr_unions_gen.append(f" '{attr.name}': {attr.name},")
attr_docs_gen.append(f"* {attr.name}: {attr.doc}")
# Also mention default value if applicable
if attr.default is not None:
attr_docs_gen.append(
f"* {attr.name}: {attr.doc} (defaults to {attr.default})")
else:
attr_docs_gen.append(f"* {attr.name}: {attr.doc}")

attr_args = '\n'.join(attr_args_gen).strip()
attr_unions = '\n'.join(attr_unions_gen).strip()
Expand All @@ -70,7 +81,8 @@ def generate_tag_class(output: TextIO, tag: TagInfo):
.replace("{attr_unions}", attr_unions)\
.replace("{attr_docs_outer}", attr_docs_outer)\
.replace("{attr_docs_inner}", attr_docs_inner)\
.replace("{kw_only}", kw_only)
.replace("{kw_only}", kw_only)\
.replace("{default_attrs}", default_attrs)

print(text, file=output)
# And a nice trailing newline to make flake8 happy
Expand Down
3 changes: 3 additions & 0 deletions meta/templates/class_attrs_SelfClosingTag.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def __call__(
{attr_unions}
}
return super().__call__(**attributes)

def _get_default_attributes(self) -> dict[str, Any]:
return {default_attrs}
3 changes: 3 additions & 0 deletions meta/templates/class_attrs_StylableTag.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ def __call__(
{attr_unions}
}
return super().__call__(*children, **attributes)

def _get_default_attributes(self) -> dict[str, Any]:
return {default_attrs}
3 changes: 3 additions & 0 deletions meta/templates/class_attrs_Tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def __call__(
{attr_unions}
}
return super().__call__(*children, **attributes)

def _get_default_attributes(self) -> dict[str, Any]:
return {default_attrs}
18 changes: 16 additions & 2 deletions pyhtml/__tag_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,29 @@ def _get_tag_name(self) -> str:
"""
return type(self).__name__.removesuffix('_')

def _get_default_attributes(self) -> dict[str, Any]:
"""
Returns the default attributes for the tag
This is overridden by child classes to return a dictionary of default
attributes that are applied to the class.
"""
return {}

def _render(self) -> list[str]:
"""
Renders tag and its children to a list of strings where each string is
a single line of output
"""
attributes = util.filter_attributes(util.dict_union(
self._get_default_attributes(),
self.attributes,
))

# Tag and attributes
opening = f"<{self._get_tag_name()}"
if len(self.attributes):
opening += f" {util.render_tag_attributes(self.attributes)}>"
if len(attributes):
opening += f" {util.render_tag_attributes(attributes)}>"
else:
opening += ">"

Expand Down
Loading

0 comments on commit 719ad20

Please sign in to comment.