I’ve stumbled upon this while writing a custom auth scheme for HTTPX today. Parentheses can be omitted around a yield expression if on the right side of an assignment expression (as mentioned here, last paragraph), but not within a function call.
I did a quick read of PEP 255, and search this forum, but did not find a straight answer.
# Ok
def printgen():
while True:
x = yield
print(x)
# Also ok
def printgen():
while True:
print((yield))
# SyntaxError
def printgen():
while True:
print(yield)
In contrast, await expressions can be used in function calls within coroutine functions.
# Ok
print((await asyncio.sleep(1)))
# Also ok
print(await asyncio.sleep(1))
I suspect this has something to do with await expressions requiring a right hand expression unlike yield expressions.
I’ll be glad if someone can give me a clear explainer!