fix: validate individual values in async generator functions - #147
Open
gaoflow wants to merge 1 commit into
Open
fix: validate individual values in async generator functions#147gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
When a contract decorator (@deal.post, @deal.ensure, @deal.pre) was applied to an async generator function, the wrapper fell through to the synchronous branch because isasyncgenfunction() is not covered by either iscoroutinefunction() or isgeneratorfunction(). This caused @deal.post and @deal.ensure to validate the async_generator object itself instead of each yielded value, producing either a confusing TypeError or silently passing/failing based on the object's truthiness rather than the actual yielded data. Add a dedicated _run_async_iter path that iterates the async generator with `async for` and validates pre-conditions before iteration starts and post/ensure conditions after each yield.
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.
Problem
When
@deal.post,@deal.ensure, or@deal.prewas applied to an async generator function, the contracts did not work correctly.Python's
iscoroutinefunction()andisgeneratorfunction()both returnFalsefor async generators — onlyisasyncgenfunction()identifies them. Because_ensure_wrappedhad no branch for async generators, they fell through to the synchronous wrapper, which calledself.func(*args, **kwargs)and received an async generator object as the result.@deal.postthen validated that object instead of each yielded value.Fix
Add an
isasyncgenfunctionbranch in_ensure_wrappedand a new_run_async_itermethod that:async for.The change is 31 lines, mirrors the existing
_run_iterlogic, and adds tests for both@deal.postand@deal.ensureon async generators.This pull request was prepared with the assistance of AI, under my direction and review.