This bit me a while back, and I spent some time reading through the PEP 533 discussion. It was determined that changing the default behavior of for loops would break too much code.

I wonder if the new PEG parser would allow us to decorate for loops with context sensitive keywords to override this default behavior without requiring code restructuring (adding a with statement).


From: Paul Bryan via Python-ideas <python-ideas@python.org>
Sent: Thursday, December 31, 2020 12:06 PM
To: python-ideas@python.org
Subject: [EXTERNAL] [Python-ideas] PEP 533 Redux?
 
I've now encountered an issue I believe that PEP 533 was intended to address:

When an asynchronous context manager is created within an asynchronous generator, if the generator is not iterated fully, the context manager will not exit until the event loop cancels the task by raising a CancelledError, long after the context manager is assumed to be out of scope. Per PEP 525, I can call aclose coroutine method to cleanup the generator, but it requires the code iterating to be aware that that closing the generator is necessary. 

Any appetite for putting PEP 533 back on the table to address this issue?