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