gen: accept any _Yieldable in WaitIterator constructor (issue #3001)#3670
Open
HrachShah wants to merge 1 commit into
Open
gen: accept any _Yieldable in WaitIterator constructor (issue #3001)#3670HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
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.
Fixes #3001
What
WaitIterator's constructor is documented to take any awaitable(
tornado.gen.WaitIteratordocstring: 'Provides an iterator to yieldthe results of awaitables as they finish'), but its type annotation
pinned the parameters to
Future. mypy rejected the documented usagegen.WaitIterator(tornado.locks.Event().wait())even though itworked at runtime (the
future_add_done_callbackcall inside theconstructor only worked when the input happened to already be a
Future—asyncio.Futurewould crash withAttributeErroronadd_done_callback, and coroutines wouldn't be started at all).The type now matches the documented behaviour:
*argsand**kwargsare_Yieldable, and each input is normalized throughconvert_yieldedso the rest of the class can keep treating_unfinishedasdict[Future, int | str]. When the user passesa
Future,convert_yieldedreturns the same object(
is_futureshort-circuits), so the documented contract thatcurrent_futureis the input that produced the result is preserved.Test
New
WaitIteratorTest.test_iterator_accepts_coroutineintornado/test/gen_test.pypasses threeasync defcoroutines withdifferent
gen.sleepdelays toWaitIteratorand asserts allthree values come back via
current_index. On the pre-fix codethis test crashes inside the constructor with
AttributeError: 'coroutine' object has no attribute 'add_done_callback'for the first coroutine input.
Verified with
python3 -m pytest tornado/test/gen_test.py-> 73 passed(72 existing + 1 new). The five pre-existing
WaitIteratorTestcases still pass without modification, includingtest_already_doneandtest_iteratorwhich assertcurrent_future is f1and friends — those inputs areFutureobjects, and
convert_yieldedreturns them unchanged.mypy on
tornado/gen.pyreports no new errors (the onlydiagnostic is the pre-existing missing-stubs note for
pycurlintornado/curl_httpclient.py).