Repository navigation
Support generator expressions, evaluated lazily (#564) - #917
Merged
craigbarratt merged 2 commits intoNov 21, 2026
Merged
craigbarratt merged 2 commits into
craigbarratt merged 2 commits into
Conversation
craigbarratt
approved these changes
Nov 21, 2026
craigbarratt
left a comment
Member
There was a problem hiding this comment.
Thanks @HoneyBearTech, this is a nice, self-contained way to get lazy generator expressions. Two small things:
- It needs a rebase.
tests/test_unit_eval.pyconflicts with master only because both append at the end. - In the docs, "a service with
blocking=Truewhose handler has to wait" undersells it. Anyblocking=Truecall to a pyscript service fails, even when its handler doesn't wait, since pyscript runs the handler in its own task (and most HA services do I/O too). I'd just say an element can't make a service call withblocking=True.
Generator expressions such as any(x > 3 for x in values) failed with "not implemented ast ast_generatorexp" (custom-components#564, also custom-components#834). They now return a real generator that evaluates each element only when it's asked for, in the scope where it was created; an element that has to wait (eg, for task.sleep()) raises a RuntimeError that suggests a list comprehension instead. Signed-off-by: Sean Stoube <[email protected]>
A service call with blocking=True fails in an element even when the handler doesn't wait for anything: a pyscript service's handler runs in its own task, and most other services do I/O. Signed-off-by: Sean Stoube <[email protected]>
honeybeartech-homelab-automation
Bot
force-pushed
the
fix-generator-expressions-564
branch
from
November 21, 2026 19:00
e7be2f5 to
382ee3d
Compare
Contributor
Author
|
Thanks @craigbarratt! Rebased onto master. Only tests/test_unit_eval.py needed it: our tests now come after the new ones, and nothing else changed. The docs now just say an element can't make a service call with blocking=True. That's in its own commit so it's easy to see. I checked it: a blocking call to a pyscript service whose handler never waits does raise the error. (A native service with a plain @callback handler still works, but I agree the simple rule is better for the docs.) |
Member
|
Thanks @HoneyBearTech, looks great. Merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #564
What was broken
Generator expressions weren't implemented, so common idioms failed with
not implemented ast ast_generatorexp(also reported in #834):What I changed
ast_generatorexpreturns a real generator that evaluates each element only when it's asked for:sum,any,"".join,sorted,zipandnextwork, andany()stops at the first match. An endless source such asitertools.count()is fine withnext().RuntimeErrorsays to use a list comprehension instead.task.executor()gives a clear error.yieldand generator functions are still not supported.Testing
test_generator_expressions_match_python(tests/test_unit_eval.py): 30 sources, each compared with CPython. They cover:sum/any/all,min/max/sorted,zip/enumerate,dict/set/tuple,join,nextanditertools.count();for/ifclauses, closures and late binding, consumption by loops and comprehensions;StopIteration;test_generator_expression_errors: elements that wait give the clear error, scope is restored afterwards, and thetask.executorguard works.test_generator_expressions_in_triggers_and_services(tests/test_function.py): genexps in a@state_triggerexpression, in trigger and service functions, and an element that makes a blocking service call.sphinx-build -Wpass on Python 3.14. The new tests run cleanly with-W error::RuntimeWarning.Docs
Language Limitations now says generator expressions work, and lists their limits:
"generators" in the unsupported list now says "generator functions and the
yieldstatement".