Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error on invalid attributes directly #49

Closed
pelme opened this issue Aug 24, 2024 · 1 comment · Fixed by #55
Closed

Raise error on invalid attributes directly #49

pelme opened this issue Aug 24, 2024 · 1 comment · Fixed by #55

Comments

@pelme
Copy link
Owner

pelme commented Aug 24, 2024

Currently, invalid attributes (such as integers) gets raised during rendering, not when constructing the element, leading to cryptic stack traces.

The error should be raised directly when constructing the invalid element, not during render:

>>> el = div(a=1)
>>> print(el)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 272, in __str__
    return _Markup("".join(str(x) for x in self))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 272, in <genexpr>
    return _Markup("".join(str(x) for x in self))
                          ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 331, in _iter_context
    yield f"<{self._name}{_attrs_string(self._attrs)}>"
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 112, in _attrs_string
    result = " ".join(k if v is True else f'{k}="{v}"' for k, v in _generate_attrs(attrs))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 112, in <genexpr>
    result = " ".join(k if v is True else f'{k}="{v}"' for k, v in _generate_attrs(attrs))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/andreas/code/htpy/htpy/__init__.py", line 106, in _generate_attrs
    raise ValueError(f"Attribute value must be a string , got {value!r}")
ValueError: Attribute value must be a string , got 1
pelme added a commit that referenced this issue Sep 8, 2024
This commit transforms attributes to strings directly when being called
rather at rendering time. This makes exceptions for invalid attributes
appear directly leading to clearer stack traces.

Fixes #49.
pelme added a commit that referenced this issue Sep 8, 2024
This commit transforms attributes to strings directly when being called
rather at rendering time. This makes exceptions for invalid attributes
appear directly leading to clearer stack traces.

Fixes #49.
@pelme pelme closed this as completed in #55 Sep 8, 2024
@pelme pelme closed this as completed in b457d12 Sep 8, 2024
@pelme
Copy link
Owner Author

pelme commented Sep 9, 2024

This is fixed in 24.9.1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant