13
13
import numpy as np
14
14
from numpy .testing import assert_raises ,temppath
15
15
16
- try :
17
- import pytz # noqa: F401
18
- _has_pytz = True
19
- except ImportError :
20
- _has_pytz = False
21
-
22
16
23
17
class _DeprecationTestCase :
24
18
# Just as warning: warnings uses re.match, so the start of this message
@@ -43,8 +37,7 @@ def setup_method(self):
43
37
def teardown_method (self ):
44
38
self .warn_ctx .__exit__ ()
45
39
46
- def assert_deprecated (self ,function ,num = 1 ,msg_patterns = None ,
47
- ignore_others = False ,
40
+ def assert_deprecated (self ,function ,num = 1 ,ignore_others = False ,
48
41
function_fails = False ,
49
42
exceptions = np ._NoValue ,
50
43
args = (),kwargs = {}):
@@ -62,11 +55,6 @@ def assert_deprecated(self, function, num=1, msg_patterns=None,
62
55
The function to test
63
56
num : int
64
57
Number of DeprecationWarnings to expect. This should normally be 1.
65
- msg_patterns : str or tuple of str
66
- Patterns for which warning messages should match. For `str` each
67
- warning should match to the same pattern. For a tuple of `str`
68
- each warning should match against the corresponding pattern.
69
- For `None` this check is skipped.
70
58
ignore_others : bool
71
59
Whether warnings of the wrong type should be ignored (note that
72
60
the message is not checked)
@@ -100,14 +88,6 @@ def assert_deprecated(self, function, num=1, msg_patterns=None,
100
88
# just in case, clear the registry
101
89
num_found = 0
102
90
for warning in self .log :
103
- if msg_patterns is not None :
104
- pattern = (msg_patterns if isinstance (msg_patterns ,str )else
105
- msg_patterns [num_found ])
106
- msg = warning .message .args [0 ]
107
- if re .match (pattern ,msg )is None :
108
- raise AssertionError (
109
- "expected %s warning message pattern but got: %s" %
110
- (pattern ,msg ))
111
91
if warning .category is self .warning_cls :
112
92
num_found += 1
113
93
elif not ignore_others :
@@ -157,17 +137,9 @@ def test_assert_deprecated(self):
157
137
lambda :None )
158
138
159
139
def foo ():
160
- warnings .warn ("foo bar" ,category = DeprecationWarning ,
161
- stacklevel = 2 )
162
-
163
- def foo_many ():
164
140
warnings .warn ("foo" ,category = DeprecationWarning ,stacklevel = 2 )
165
- warnings .warn ("bar" ,category = DeprecationWarning ,stacklevel = 2 )
166
141
167
142
test_case_instance .assert_deprecated (foo )
168
- test_case_instance .assert_deprecated (foo ,msg_patterns = "foo" )
169
- test_case_instance .assert_deprecated (foo_many ,num = 2 ,
170
- msg_patterns = ("foo" ,"^bar$" ))
171
143
test_case_instance .teardown_method ()
172
144
173
145
@@ -476,18 +448,20 @@ def test_deprecated(self):
476
448
)
477
449
478
450
479
- class TestDeprecatedTNon2Dim (_DeprecationTestCase ):
480
- # Deprecated in Numpy 2.3, 2025-04
451
+ class TestDeprecatedTPropScalar (_DeprecationTestCase ):
452
+ # Deprecated in Numpy 2.3, 2025-05
453
+ message = ("In the future, the `.T` property for array scalars will "
454
+ "raise an error." )
455
+
456
+ def test_deprecated (self ):
457
+ self .assert_deprecated (lambda :np .int64 (1 ).T )
458
+
459
+
460
+ class TestDeprecatedTPropNon2Dim (_DeprecationTestCase ):
461
+ # Deprecated in Numpy 2.3, 2025-05
462
+ message = ("In the future, the `.T` property will be supported for "
463
+ r"2-dimensional arrays only. Received \d+-dimensional array." )
464
+
481
465
def test_deprecated (self ):
482
- self .assert_deprecated (
483
- lambda :np .int64 (1 ).T ,
484
- msg_patterns = "In the future `.T` property for "
485
- "array scalars will raise an error."
486
- )
487
466
for shape in [(5 ,), (2 ,3 ,4 )]:
488
- self .assert_deprecated (
489
- lambda :np .ones (shape ).T ,
490
- msg_patterns = "In the future `.T` property will be "
491
- "supported for 2-dim arrays only. "
492
- f"Received{ len (shape )} -dim array."
493
- )
467
+ self .assert_deprecated (lambda :np .ones (shape ).T )