You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just stumbled over an issue with contextvars and async generators in my code. That led me to https://peps.python.org/pep-0568/ and further research brought me here.
I'm not 100% sure this is the actual problem, though - it's about 1 am now and after reading a lot my brain is a bit fried.
Let me describe my issue in code:
fromunittest.mockimportMockimportcontextvarsasyncdeftest_():
m=Mock()
cv=contextvars.ContextVar("cv", default={})
asyncdefdummy_async_method(i: int):
# wrapped.set_mdc({f"coro{i}": f"coro{i}"})cv.set({f"coro{i}": f"coro{i}"})
m.info(cv.get())
yieldf"c{i}.1"# cv({f"coro{i}": f"coro{i}"}) # by setting the var again the test passesm.info(cv.get())
yieldf"c{i}.2"gen1=dummy_async_method(1)
gen2=dummy_async_method(2)
# assert expected execution orderassertawaitanext(gen1) =="c1.1"assertawaitanext(gen2) =="c2.1"assertawaitanext(gen1) =="c1.2"assertawaitanext(gen2) =="c2.2"# assert logger invocations with correct contextassertm.info.call_args_list[0].args[0] == {"coro1": "coro1"}
assertm.info.call_args_list[1].args[0] == {"coro2": "coro2"}
assertm.info.call_args_list[2].args[0] == {"coro1": "coro1"} # <-- fails here, is coro2: coro2assertm.info.call_args_list[3].args[0] == {"coro2": "coro2"} # this, too, is coro2: coro2
I am like .93 confident I'm in the right place and I'll give extracontent a shot somewhere next week. But perhaps someone's faster.
Was there ever a follow-up to PEP 568? I can't find any plans on fixing this issue.
P. S.:
There's a print in PyContextLocal's constructor - is that intended? ;)
Also, the indentation in the first example here is off. I may file a PR once I caught some sleep.
The text was updated successfully, but these errors were encountered:
I just stumbled over an issue with
contextvars
and async generators in my code. That led me to https://peps.python.org/pep-0568/ and further research brought me here.I'm not 100% sure this is the actual problem, though - it's about 1 am now and after reading a lot my brain is a bit fried.
Let me describe my issue in code:
I am like .93 confident I'm in the right place and I'll give
extracontent
a shot somewhere next week. But perhaps someone's faster.Was there ever a follow-up to PEP 568? I can't find any plans on fixing this issue.
P. S.:
There's a
print
inPyContextLocal
's constructor - is that intended? ;)Also, the indentation in the first example here is off. I may file a PR once I caught some sleep.
The text was updated successfully, but these errors were encountered: