Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Cannot filter DeprecationWarning from numpy.product() by caller module #24015

Closed
@ktns

Description

@ktns

Describe the issue:

It seems that DeprecationWarning from numpy.product cannot be filtered by caller module. I set filterwarnings option for pytest as ignore:<msg>:DeprecationWarning,error:<msg>:DeprecationWarning:mylib but calls from numpy.product in mylib module is not treated as errors.

After changing stacklevel argument of warnings.warn called in numpy.core.fromnumeric._product_dispatcher from 3 to 2, the filter started to work as I expected.

Is this intended behavior?

Reproduce the code example:

import numpy
import warnings

warnings.filterwarnings(
    action="always", # For demonstration purpose. I wanted to ignore warnings from other modules.
    message="`product` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `prod` instead.",
    category=DeprecationWarning,
)
warnings.filterwarnings(
    action="error",
    message="`product` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `prod` instead.",
    category=DeprecationWarning,
    module="__main__",
)

numpy.product([42])

raise RuntimeError("I expected this line should not be reached")

Error message:

sys:1: DeprecationWarning: `product` is deprecated as of NumPy 1.25.0, and will be removed in NumPy 2.0. Please use `prod` instead.
Traceback (most recent call last):
  File "/tmp/tmp.c0VF4KSqKu/hoge.py", line 18, in <module>
    raise RuntimeError("I expected this line should not be reached")
RuntimeError: I expected this line should not be reached

Runtime information:

1.25.0
3.11.2 (main, May 30 2023, 17:45:26) [GCC 12.2.0]
WARNING: `threadpoolctl` not found in system! Install it by `pip install threadpoolctl`. Once installed, try `np.show_runtime` again for more detailed build information
[{'numpy_version': '1.25.0',
  'python': '3.11.2 (main, May 30 2023, 17:45:26) [GCC 12.2.0]',
  'uname': uname_result(system='Linux', node='chesed', release='6.2.0-20-generic', version='#20-Ubuntu SMP PREEMPT_DYNAMIC Thu Apr  6 07:48:48 UTC 2023', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2',
                                'AVX512F',
                                'AVX512CD',
                                'AVX512_SKX',
                                'AVX512_CLX',
                                'AVX512_CNL',
                                'AVX512_ICL'],
                      'not_found': ['AVX512_KNL', 'AVX512_KNM']}}]
None

Context for the issue:

Since numpy.product has begun emitting DeprecationWarning recently, I want pytest to ignore DeprecationWarnings if they are from 3rd party library and treat them as errors if they are from my library.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions