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

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ktns opened this issue Jun 22, 2023 · 4 comments · Fixed by #25837
Closed

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

ktns opened this issue Jun 22, 2023 · 4 comments · Fixed by #25837
Assignees
Labels
Milestone

Comments

@ktns
Copy link

ktns commented Jun 22, 2023

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.

@ktns ktns added the 00 - Bug label Jun 22, 2023
@seberg seberg added this to the 1.25.1 release milestone Jun 22, 2023
@seberg
Copy link
Member

seberg commented Jun 22, 2023

The stacklevel= is set to 3 rather than 2 on the deprecation warnings so the location of the cause is incorrect (it can't always be correct easily, but here it should be). It seems incorrect in all functions deprecated in the original PR.

This may also hide the warning from end-users at least in some cases.

@charris
Copy link
Member

charris commented Nov 12, 2023

@seberg I think this can be closed as fixed in 2.0.

@charris
Copy link
Member

charris commented Feb 5, 2024

Pushed off to 2.0.0. @seberg Can this be closed?

@seberg
Copy link
Member

seberg commented Feb 5, 2024

Technically, still there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants