posit-dev / py-htmltools

Tools for HTML generation and output
MIT License
19 stars 4 forks source link

Change type annotation of `_add_ws` #67

Closed wch closed 12 months ago

wch commented 12 months ago

This PR changes the type annotation of _add_ws from bool to TagAttrValue. Even though the value must still actually be a bool, this change simplifies writing functions that pass along kwargs.

For example, previously pyright would flag this as an error, because the kwargs could include _add_ws, but the former is a TagAttrValue while the latter is a bool:

def my_component(*args: TagChild, **kwargs: TagAttrValue) -> Tag:
    return div("Hello", *args, **kwargs)

In order to satisfy the type checker, you previously would have to write this:

def my_component(*args: TagChild, _add_ws: bool: True, **kwargs: TagAttrValue) -> Tag:
    return div("Hello", *args, _add_ws=_add_ws, **kwargs)

After this PR, the first version of the code is not flagged as an error by the type checker. If a non-bool is passed to _add_ws, there will be a run-time error.