fix: support async predicates in page.expect_request/expect_response#3055
fix: support async predicates in page.expect_request/expect_response#3055Skn0tt merged 3 commits intomicrosoft:mainfrom
Conversation
Closes microsoft#1545 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
| try: | ||
| result = predicate(event_data) | ||
| except Exception as e: | ||
| self._reject(e) |
There was a problem hiding this comment.
Is this line a separate bugfix? I don't see where we would reject after a sync predicate throws before this PR. Perhaps add a test for this as well?
There was a problem hiding this comment.
Good catch — yes, this is a coupled bugfix. Previously sync predicates that threw were silently swallowed by pyee's listener machinery, leaving the expect_* call to hang until timeout. Pushed a test (test_expect_response_should_reject_when_sync_predicate_throws) covering it.
There was a problem hiding this comment.
Cross-checked against JS waiter.ts's waitForEvent (packages/playwright-core/src/client/waiter.ts:113-124) — JS already awaits predicates and rejects on either sync or async predicate exceptions (try/catch wraps await predicate(eventArg)). This PR brings Python to parity.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…roval Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Closes #1545.
Async predicates passed to
page.expect_request/page.expect_responsewere called but never awaited, so the returned coroutine evaluated truthy and the first matching event resolved the wait (with aRuntimeWarning: coroutine '...' was never awaited).Waiternow detects coroutine results from predicates and awaits them in a tracked task; exceptions from predicates reject the waiter instead of being silently swallowed by pyee.Callable[[T], Union[bool, Awaitable[bool]]]. The sync API keepsCallable[[T], bool]— codegen was extended with aSYNC_APIflag that strips theAwaitablearm from both signatures and docstrings for the sync generator.tests/async/test_page.pycovering the success path and exception propagation.