Skip to content

Commit 8903f0b

Browse files
committed
DOCTEST: Suppress deprecation warnings in doctests of deprecated methods
1 parent f879473 commit 8903f0b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

nibabel/deprecator.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@
77

88
_LEADING_WHITE = re.compile(r'^(\s*)')
99

10+
TESTSETUP = """
11+
12+
.. testsetup::
13+
14+
>>> import pytest
15+
>>> import warnings
16+
>>> _suppress_warnings = pytest.deprecated_call()
17+
>>> _ = _suppress_warnings.__enter__()
18+
19+
"""
20+
21+
TESTCLEANUP = """
22+
23+
.. testcleanup::
24+
25+
>>> warnings.warn("Avoid error if no doctests to run...", DeprecationWarning)
26+
>>> _ = _suppress_warnings.__exit__(None, None, None)
27+
28+
"""
29+
1030

1131
class ExpiredDeprecationError(RuntimeError):
1232
""" Error for expired deprecation
@@ -25,7 +45,7 @@ def _ensure_cr(text):
2545
return text.rstrip() + '\n'
2646

2747

28-
def _add_dep_doc(old_doc, dep_doc):
48+
def _add_dep_doc(old_doc, dep_doc, setup='', cleanup=''):
2949
""" Add deprecation message `dep_doc` to docstring in `old_doc`
3050
3151
Parameters
@@ -56,8 +76,11 @@ def _add_dep_doc(old_doc, dep_doc):
5676
# nothing following first paragraph, just append message
5777
return old_doc + '\n' + dep_doc
5878
indent = _LEADING_WHITE.match(old_lines[next_line]).group()
79+
setup_lines = [indent + L for L in setup.splitlines()]
5980
dep_lines = [indent + L for L in [''] + dep_doc.splitlines() + ['']]
60-
return '\n'.join(new_lines + dep_lines + old_lines[next_line:]) + '\n'
81+
cleanup_lines = [indent + L for L in cleanup.splitlines()]
82+
return '\n'.join(new_lines + dep_lines + setup_lines +
83+
old_lines[next_line:] + cleanup_lines + [''])
6184

6285

6386
class Deprecator(object):
@@ -160,7 +183,7 @@ def deprecated_func(*args, **kwargs):
160183
return func(*args, **kwargs)
161184

162185
deprecated_func.__doc__ = _add_dep_doc(deprecated_func.__doc__,
163-
message)
186+
message, TESTSETUP, TESTCLEANUP)
164187
return deprecated_func
165188

166189
return deprecator

nibabel/tests/test_deprecator.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import sys
55
import warnings
66
from functools import partial
7+
from textwrap import indent
78

89
import pytest
910

1011
from nibabel.deprecator import (_ensure_cr, _add_dep_doc,
11-
ExpiredDeprecationError, Deprecator)
12+
ExpiredDeprecationError, Deprecator,
13+
TESTSETUP, TESTCLEANUP)
1214

1315
from ..testing import clear_and_catch_warnings
1416

@@ -81,7 +83,9 @@ def test_dep_func(self):
8183
with pytest.deprecated_call() as w:
8284
assert func(1, 2) is None
8385
assert len(w) == 1
84-
assert func.__doc__ == 'A docstring\n \n foo\n \n Some text\n'
86+
assert (func.__doc__ ==
87+
f'A docstring\n \n foo\n \n{indent(TESTSETUP, " ", lambda x: True)}'
88+
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}')
8589

8690
# Try some since and until versions
8791
func = dec('foo', '1.1')(func_no_doc)
@@ -110,7 +114,8 @@ def test_dep_func(self):
110114
assert (func.__doc__ ==
111115
'A docstring\n \n foo\n \n * deprecated from version: 1.2\n '
112116
f'* Raises {ExpiredDeprecationError} as of version: 1.8\n \n'
113-
' Some text\n')
117+
f'{indent(TESTSETUP, " ", lambda x: True)}'
118+
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}')
114119
with pytest.raises(ExpiredDeprecationError):
115120
func()
116121

0 commit comments

Comments
 (0)