Skip to content

Commit

Permalink
Python 3.13: Account for dedented docstrings
Browse files Browse the repository at this point in the history
- Dedent docstrings in Python 3.13+
- Fix nipy#1311
- Ref: python/cpython#81283
  • Loading branch information
penguinpee committed Apr 1, 2024
1 parent e5cd0e8 commit 9fa116b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 15 additions & 0 deletions nibabel/deprecator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@

import functools
import re
import sys
import typing as ty
import warnings
from textwrap import dedent

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

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


def _dedent_docstring(docstring):
"""Compatibility with Python 3.13+.
xref: https://github.com/python/cpython/issues/81283
"""
return '\n'.join([dedent(line) for line in docstring.split('\n')])


TESTSETUP = """
.. testsetup::
Expand All @@ -32,6 +43,10 @@
"""

if sys.version_info >= (3, 13):
TESTSETUP = _dedent_docstring(TESTSETUP)
TESTCLEANUP = _dedent_docstring(TESTCLEANUP)


class ExpiredDeprecationError(RuntimeError):
"""Error for expired deprecation
Expand Down
15 changes: 10 additions & 5 deletions nibabel/tests/test_deprecator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@
Deprecator,
ExpiredDeprecationError,
_add_dep_doc,
_dedent_docstring,
_ensure_cr,
)

from ..testing import clear_and_catch_warnings

_OWN_MODULE = sys.modules[__name__]

func_docstring = (
f'A docstring\n \n foo\n \n{indent(TESTSETUP, " ", lambda x: True)}'
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}'
)

if sys.version_info >= (3, 13):
func_docstring = _dedent_docstring(func_docstring)


def test__ensure_cr():
# Make sure text ends with carriage return
Expand Down Expand Up @@ -92,11 +101,7 @@ def test_dep_func(self):
with pytest.deprecated_call() as w:
assert func(1, 2) is None
assert len(w) == 1
assert (
func.__doc__
== f'A docstring\n \n foo\n \n{indent(TESTSETUP, " ", lambda x: True)}'
f' Some text\n{indent(TESTCLEANUP, " ", lambda x: True)}'
)
assert func.__doc__ == func_docstring

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

0 comments on commit 9fa116b

Please sign in to comment.