When the thread pool is cold, contextvars area automatically propagated from the caller thread to **new** threads on Python 3.14t by default, as well as on 3.14 with the opt-in flag `python -X thread_inherit_context=1`. Test that contextvars are always propagated to the tasks, even in a
(num_workers)
| 216 | |
| 217 | @pytest.mark.parametrize("num_workers", [None, 1]) |
| 218 | def test_contextvars(num_workers): |
| 219 | """When the thread pool is cold, contextvars area automatically propagated from the |
| 220 | caller thread to **new** threads on Python 3.14t by default, as well as on 3.14 with |
| 221 | the opt-in flag `python -X thread_inherit_context=1`. |
| 222 | |
| 223 | Test that contextvars are always propagated to the tasks, even in absence of the above |
| 224 | flag and even when the threads were already warm when the context was set. |
| 225 | """ |
| 226 | warm_up_thread_pool(num_workers) |
| 227 | |
| 228 | dsk = {"x": (test_ctxvar.get,)} |
| 229 | # Reuse thread pool started by warm_up_thread_pool |
| 230 | assert get(dsk, "x", num_workers=num_workers) == 42 |
| 231 | tok = test_ctxvar.set(43) |
| 232 | try: |
| 233 | assert get(dsk, "x", num_workers=num_workers) == 43 |
| 234 | finally: |
| 235 | test_ctxvar.reset(tok) |
| 236 | |
| 237 | |
| 238 | @pytest.mark.parametrize("num_workers", [None, 1]) |
nothing calls this directly
no test coverage detected