Skip to content

Instantly share code, notes, and snippets.

@nascheme
Last active January 8, 2026 21:57
Show Gist options
  • Select an option

  • Save nascheme/68525f4ebb5e084f27f79cfdde2f7812 to your computer and use it in GitHub Desktop.

Select an option

Save nascheme/68525f4ebb5e084f27f79cfdde2f7812 to your computer and use it in GitHub Desktop.
Example of context_aware_warnings behaviour
# See: https://github.com/pytest-dev/pytest/issues/14077
from concurrent.futures import ThreadPoolExecutor
import numpy as np
import pytest
@pytest.fixture(scope="module")
def pool():
yield ThreadPoolExecutor(max_workers=1)
def test_1(pool):
# warm the thread
pool.submit(lambda: None).result()
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
def test_2(pool):
# In the gil-enabled build (or with context_aware_warnings=0),
# the filters established by the decorator also apply to the
# lambda being executed by the pool thread. With
# context_aware_warnings=1, the warning is not filtered.
pool.submit(lambda: np.array(0.0) / np.array(0.0)).result()
###################################################################
def test_3(pool):
# This works as expected since catch_warnings() runs in the
# worker thread.
def work():
with warnings.catch_warnings(category=RuntimeWarning, action='ignore'):
return np.array(0.0) / np.array(0.0)
pool.submit(work).result()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment