@@ -29,29 +29,39 @@ def pytest_namespace():
29
29
30
30
31
31
def deprecated_call (func , * args , ** kwargs ):
32
- """ assert that calling ``func(*args, **kwargs)``
33
- triggers a DeprecationWarning.
32
+ """ assert that calling ``func(*args, **kwargs)`` triggers a
33
+ DeprecationWarning.
34
+
35
+ Note: we cannot use WarningsRecorder here because it is still subject
36
+ to the mechanism that prevents warnings of the same type from being
37
+ triggered twice for the same module. See #1190.
34
38
"""
35
- l = []
36
- oldwarn_explicit = getattr (warnings , 'warn_explicit' )
37
- def warn_explicit (* args , ** kwargs ):
38
- l .append (args )
39
- oldwarn_explicit (* args , ** kwargs )
40
- oldwarn = getattr (warnings , 'warn' )
41
- def warn (* args , ** kwargs ):
42
- l .append (args )
43
- oldwarn (* args , ** kwargs )
39
+ categories = []
40
+
41
+ def warn_explicit (message , category , * args , ** kwargs ):
42
+ categories .append (category )
43
+ old_warn_explicit (message , category , * args , ** kwargs )
44
+
45
+ def warn (message , category = None , ** kwargs ):
46
+ if isinstance (message , Warning ):
47
+ categories .append (message .__class__ )
48
+ else :
49
+ categories .append (category )
50
+ old_warn (message , category , * args , ** kwargs )
44
51
52
+ old_warn = warnings .warn
53
+ old_warn_explicit = warnings .warn_explicit
45
54
warnings .warn_explicit = warn_explicit
46
55
warnings .warn = warn
47
56
try :
48
57
ret = func (* args , ** kwargs )
49
58
finally :
50
- warnings .warn_explicit = oldwarn_explicit
51
- warnings .warn = oldwarn
52
- if not l :
59
+ warnings .warn_explicit = old_warn_explicit
60
+ warnings .warn = old_warn
61
+ deprecation_categories = (DeprecationWarning , PendingDeprecationWarning )
62
+ if not any (issubclass (c , deprecation_categories ) for c in categories ):
53
63
__tracebackhide__ = True
54
- raise AssertionError ("%r did not produce DeprecationWarning" % (func ,))
64
+ raise AssertionError ("%r did not produce DeprecationWarning" % (func ,))
55
65
return ret
56
66
57
67
0 commit comments