Repository: numpy/numpy
File: numpy/testing/tests/test_utils.py (Python)

Code before:
    with suppress_warnings() as sup:
        sup.filter(UserWarning)
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)
    sup = suppress_warnings()
    sup.filter(UserWarning)
    with sup:
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)
    # And test repeat works:
    sup.filter(module=my_mod)
    with sup:
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)

    # Without specified modules
    with suppress_warnings():
        warnings.simplefilter('ignore')
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)


@pytest.mark.filterwarnings(
    "ignore:.*NumPy warning suppression and assertion utilities are deprecated"
    ".*:DeprecationWarning")
@pytest.mark.thread_unsafe(reason="uses deprecated thread-unsafe warnings")
def test_suppress_warnings_decorate_no_record():
    sup = suppress_warnings()
    sup.filter(UserWarning)

    @sup
    def warn(category):
        warnings.warn('Some warning', category)

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        warn(UserWarning)  # should be suppressed
        warn(RuntimeWarning)
        assert_equal(len(w), 1)


@pytest.mark.filterwarnings(
    "ignore:.*NumPy warning suppression and assertion utilities are deprecated"
    ".*:DeprecationWarning")
@pytest.mark.thread_unsafe(reason="uses deprecated thread-unsafe warnings")
def test_suppress_warnings_record():
    sup = suppress_warnings()
    log1 = sup.record()

    with sup:
        log2 = sup.record(message='Some other warning 2')

Review comment (suggestion):
"warnings control utilities" instead of just "warnings", here and in the other identical reasons in this file.

Code after:
    with suppress_warnings() as sup:
        sup.filter(UserWarning)
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)
    sup = suppress_warnings()
    sup.filter(UserWarning)
    with sup:
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)
    # And test repeat works:
    sup.filter(module=my_mod)
    with sup:
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)

    # Without specified modules
    with suppress_warnings():
        warnings.simplefilter('ignore')
        warnings.warn('Some warning')
    assert_warn_len_equal(my_mod, 0)


@pytest.mark.filterwarnings(
    "ignore:.*NumPy warning suppression and assertion utilities are deprecated"
    ".*:DeprecationWarning")
@pytest.mark.thread_unsafe(
    reason="uses deprecated thread-unsafe warnings control utilities"
)
def test_suppress_warnings_decorate_no_record():
    sup = suppress_warnings()
    sup.filter(UserWarning)

    @sup
    def warn(category):
        warnings.warn('Some warning', category)

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        warn(UserWarning)  # should be suppressed
        warn(RuntimeWarning)
        assert_equal(len(w), 1)


@pytest.mark.filterwarnings(
    "ignore:.*NumPy warning suppression and assertion utilities are deprecated"
    ".*:DeprecationWarning")
@pytest.mark.thread_unsafe(
    reason="uses deprecated thread-unsafe warnings control utilities"
)
def test_suppress_warnings_record():
    sup = suppress_warnings()