Thinking about this more, getting the watcher config/state from the context can be problematic in a couple of ways:
- The config/state is not guaranteed to be available in the context. For example, the code can do
ctx = contextvars.Context()
instead ofctx = contextvars.copy_context()
. Or the code can docopy_context()
, but early during initialization and squirrel the context away for later use. The latter seems plausible for 3rd party libraries (where the user might not have much control). - The same watcher callback cannot be registered multiple times simultaneously, each with its own config/state. This came up while I was refactoring
TestContextObjectWatchers
inLib/test/test_capi/test_watchers.py
to add some more tests for a change I’m working on.
I’m not sure how likely either is to come up in normal use, but it might be worth changing the API before the 3.14 release cements the design.