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

List comprehension failing with an empty list #236

Closed
rccoleman opened this issue Aug 28, 2021 · 0 comments · Fixed by #535
Closed

List comprehension failing with an empty list #236

rccoleman opened this issue Aug 28, 2021 · 0 comments · Fixed by #535

Comments

@rccoleman
Copy link
Contributor

rccoleman commented Aug 28, 2021

The following very simple list comprehension works fine in the interactive Python interpreter, but fails if used in PyScript:

[x for x in []]

I would expect it to return an empty list ([]), as it does in the interactive interpreter:

>>> [x for x in []]
[]

but PyScript gives an error:

        [x for x in []]
                    ^
KeyError: 'x'

It's not a very interesting example, but I've had situations where an incoming iterable may be empty and it's easier to just execute the list comprehension without having to add an if xxx to the end to filter empty lists. I've worked around it with a for loop, but I didn't see a limitation explicitly called out in the docs and wanted to file an issue for it.

Weirdly, I have seen this construct work as expected sometimes, but I can get to to reliably fail if I write an app like this:

@time_trigger('startup')
def startup():
    [x for x in []]
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