Author Jeff.Laughlin
Recipients Jeff.Laughlin, asvetlov, yselivanov
Date 2020-04-18.15:28:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
As a test engineer I want to be able to run async test fixtures and test cases in different async tasks with the same Context. Not a copy; the same specific instance of contextvars.Context().

I do NOT want the task to run in a COPY of the context because I want mutations to the context to be preserved so that I can pass the mutated context into another async task.

I do NOT want the task to inherit the potentially polluted global context.

class Task currently unconditionally copies the current global context and has no facility for the user to override the context.

Therefor I propose adding a context argument to the Task constructor and to create_task()

It should be noted that this argument should not be used for "normal" development and only for "weird" stuff like test frameworks.

I should also note that Context().run() is not useful here because it is not async and there is no obvious existing async equivalent. This proposal would be roughly equivalent.

I should also note that a hack like copying the items from one context to another will not work because it breaks ContextVar set/reset. I tried this. It was a heroic failure. It must be possible to run a task with an exist instance of context and not a copy.

Here is a real-world use case:

Here is the hacked Task constructor I cooked up:

class Task(asyncio.tasks._PyTask):
    def __init__(self, coro, *, loop=None, name=None, context=None):
        self._context = context if context is not None else copy_context()

        self._loop.call_soon(self.__step, context=self._context)

if folks are on board I can do a PR
Date User Action Args
2020-04-18 15:28:07Jeff.Laughlinsetrecipients: + Jeff.Laughlin, asvetlov, yselivanov
2020-04-18 15:28:07Jeff.Laughlinsetmessageid: <>
2020-04-18 15:28:07Jeff.Laughlinlinkissue40320 messages
2020-04-18 15:28:07Jeff.Laughlincreate