Skip to content

Python 3.13: Account for dedented docstrings #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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