Skip to content

Commit 9fa116b

Browse files
committed
Python 3.13: Account for dedented docstrings
- Dedent docstrings in Python 3.13+ - Fix #1311 - Ref: python/cpython#81283
1 parent e5cd0e8 commit 9fa116b

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

nibabel/deprecator.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33

44
import functools
55
import re
6+
import sys
67
import typing as ty
78
import warnings
9+
from textwrap import dedent
810

911
if ty.TYPE_CHECKING: # pragma: no cover
1012
T = ty.TypeVar('T')
1113
P = ty.ParamSpec('P')
1214

1315
_LEADING_WHITE = re.compile(r'^(\s*)')
1416

17+
18+
def _dedent_docstring(docstring):
19+
"""Compatibility with Python 3.13+.
20+
21+
xref: https://github.com/python/cpython/issues/81283
22+
"""
23+
return '\n'.join([dedent(line) for line in docstring.split('\n')])
24+
25+
1526
TESTSETUP = """
1627
1728
.. testsetup::
@@ -32,6 +43,10 @@
3243
3344
"""
3445

46+
if sys.version_info >= (3, 13):
47+
TESTSETUP = _dedent_docstring(TESTSETUP)
48+
TESTCLEANUP = _dedent_docstring(TESTCLEANUP)
49+
3550

3651
class ExpiredDeprecationError(RuntimeError):
3752
"""Error for expired deprecation

nibabel/tests/test_deprecator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@
1414
Deprecator,
1515
ExpiredDeprecationError,
1616
_add_dep_doc,
17+
_dedent_docstring,
1718
_ensure_cr,
1819
)
1920

2021
from ..testing import clear_and_catch_warnings
2122

2223
_OWN_MODULE = sys.modules[__name__]
2324

25+
func_docstring = (
26+
f'A docstring\n \n foo\n \n{indent(TESTSETUP, " ", lambda x: True)}'
27+
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}'
28+
)
29+
30+
if sys.version_info >= (3, 13):
31+
func_docstring = _dedent_docstring(func_docstring)
32+
2433

2534
def test__ensure_cr():
2635
# Make sure text ends with carriage return
@@ -92,11 +101,7 @@ def test_dep_func(self):
92101
with pytest.deprecated_call() as w:
93102
assert func(1, 2) is None
94103
assert len(w) == 1
95-
assert (
96-
func.__doc__
97-
== f'A docstring\n \n foo\n \n{indent(TESTSETUP, " ", lambda x: True)}'
98-
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}'
99-
)
104+
assert func.__doc__ == func_docstring
100105

101106
# Try some since and until versions
102107
func = dec('foo', '1.1')(func_no_doc)

0 commit comments

Comments
 (0)