5
5
"""
6
6
import warnings
7
7
import pytest
8
- import tempfile
9
8
import re
10
9
11
10
import numpy as np
12
- from numpy .testing import (
13
- assert_raises , assert_warns , assert_ , assert_array_equal , SkipTest ,
14
- KnownFailureException , break_cycles , temppath
15
- )
11
+ from numpy .testing import assert_raises , temppath
16
12
17
- from numpy ._core ._multiarray_tests import fromstring_null_term_c_api
18
13
import numpy ._core ._struct_ufunc_tests as struct_ufunc
19
14
20
- try :
21
- import pytz
22
- _has_pytz = True
23
- except ImportError :
24
- _has_pytz = False
25
-
26
15
27
16
class _DeprecationTestCase :
28
17
# Just as warning: warnings uses re.match, so the start of this message
@@ -47,7 +36,8 @@ def setup_method(self):
47
36
def teardown_method (self ):
48
37
self .warn_ctx .__exit__ ()
49
38
50
- def assert_deprecated (self , function , num = 1 , ignore_others = False ,
39
+ def assert_deprecated (self , function , num = 1 , msg_patterns = None ,
40
+ ignore_others = False ,
51
41
function_fails = False ,
52
42
exceptions = np ._NoValue ,
53
43
args = (), kwargs = {}):
@@ -65,6 +55,11 @@ def assert_deprecated(self, function, num=1, ignore_others=False,
65
55
The function to test
66
56
num : int
67
57
Number of DeprecationWarnings to expect. This should normally be 1.
58
+ msg_patterns : str or tuple of str
59
+ Patterns for which warning messages should match. For `str` each
60
+ warning should match to the same pattern. For a tuple of `str`
61
+ each warning should match against the corresponding pattern.
62
+ For `None` this check is skipped.
68
63
ignore_others : bool
69
64
Whether warnings of the wrong type should be ignored (note that
70
65
the message is not checked)
@@ -96,6 +91,14 @@ def assert_deprecated(self, function, num=1, ignore_others=False,
96
91
# just in case, clear the registry
97
92
num_found = 0
98
93
for warning in self .log :
94
+ if msg_patterns is not None :
95
+ pattern = (msg_patterns if isinstance (msg_patterns , str ) else
96
+ msg_patterns [num_found ])
97
+ msg = warning .message .args [0 ]
98
+ if re .match (pattern , msg ) is None :
99
+ raise AssertionError (
100
+ "expected %s warning message pattern but got: %s" %
101
+ (pattern , msg ))
99
102
if warning .category is self .warning_cls :
100
103
num_found += 1
101
104
elif not ignore_others :
@@ -145,9 +148,17 @@ def test_assert_deprecated(self):
145
148
lambda : None )
146
149
147
150
def foo ():
151
+ warnings .warn ("foo bar" , category = DeprecationWarning ,
152
+ stacklevel = 2 )
153
+
154
+ def foo_many ():
148
155
warnings .warn ("foo" , category = DeprecationWarning , stacklevel = 2 )
156
+ warnings .warn ("bar" , category = DeprecationWarning , stacklevel = 2 )
149
157
150
158
test_case_instance .assert_deprecated (foo )
159
+ test_case_instance .assert_deprecated (foo , msg_patterns = "foo" )
160
+ test_case_instance .assert_deprecated (foo_many , num = 2 ,
161
+ msg_patterns = ("foo" , "^bar$" ))
151
162
test_case_instance .teardown_method ()
152
163
153
164
@@ -456,15 +467,18 @@ def test_deprecated(self):
456
467
)
457
468
458
469
459
- def test_deprecated_T_non_2dim ( ):
470
+ class TestDeprecatedTNon2Dim ( _DeprecationTestCase ):
460
471
# Deprecated in Numpy 2.3, 2025-04
461
- with pytest .warns (UserWarning , match = "In the future `.T` property for "
462
- "array scalars will raise an error." ):
463
- np .int64 (1 ).T
464
- for shape in [(5 ,), (2 , 3 , 4 )]:
465
- with pytest .warns (
466
- UserWarning ,
467
- match = "In the future `.T` property will be "
468
- "supported for 2-dim arrays only. "
469
- f"Received { len (shape )} -dim array." ):
470
- np .ones (shape ).T
472
+ def test_deprecated (self ):
473
+ self .assert_deprecated (
474
+ lambda : np .int64 (1 ).T ,
475
+ msg_patterns = "In the future `.T` property for "
476
+ "array scalars will raise an error."
477
+ )
478
+ for shape in [(5 ,), (2 , 3 , 4 )]:
479
+ self .assert_deprecated (
480
+ lambda : np .ones (shape ).T ,
481
+ msg_patterns = "In the future `.T` property will be "
482
+ "supported for 2-dim arrays only. "
483
+ f"Received { len (shape )} -dim array."
484
+ )
0 commit comments