Skip to content

Raise exception when rendering a generator twice #102

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

Open
pelme opened this issue Apr 10, 2025 · 0 comments
Open

Raise exception when rendering a generator twice #102

pelme opened this issue Apr 10, 2025 · 0 comments

Comments

@pelme
Copy link
Owner

pelme commented Apr 10, 2025

In #101 @JakobGM pointed out that creating an element with a generator is a foot gun that can lead to unexpected results.

Instead of silently rendering empty elements, we should raise an error when trying to render a element with a generator child node the second time.

A test could be something like:

def test_raise_error_consume_generator_twice() -> None:
    def gen() -> Iterator[str]:
        yield "hi"

    element = div[gen()]
    assert list(element.iter_chunks()) == ["<div>", "hi", "</div>"]

    with pytest.raises(RuntimeError):
        list(element.iter_chunks())

the implementation could probably be setting a boolen attribute _generator_consumed = True on nodes that has a single generator child node and then checking for that existence (in _iter_chunks()).

This is slightly backwards incompatible but I would consider code that relies on this behavior to already be broken so I think we can just change this. Feel free to submit a PR for this!

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

No branches or pull requests

1 participant