| 1 |
From: Julian Gilbey <[email protected]> |
| 2 |
Date: Thu, 26 Mar 2026 20:42:51 +0100 |
| 3 |
Subject: Fix handling of docstring dedenting in Python 3.13 |
| 4 |
|
| 5 |
Forwarded: https://github.com/jupyter/jupyter_client/pull/1052 |
| 6 |
Bug-Debian: https://bugs.debian.org/1092533 |
| 7 |
Last-Update: 2025-01-13 |
| 8 |
--- |
| 9 |
jupyter_client/client.py | 53 ++++++++++++++++++++++++++++++++++-------------- |
| 10 |
1 file changed, 38 insertions(+), 15 deletions(-) |
| 11 |
|
| 12 |
diff --git a/jupyter_client/client.py b/jupyter_client/client.py |
| 13 |
index 1a0c8b0..ebf531b 100644 |
| 14 |
--- a/jupyter_client/client.py |
| 15 |
+++ b/jupyter_client/client.py |
| 16 |
@@ -4,7 +4,9 @@ |
| 17 |
# Distributed under the terms of the Modified BSD License. |
| 18 |
import asyncio |
| 19 |
import inspect |
| 20 |
+import re |
| 21 |
import sys |
| 22 |
+import textwrap |
| 23 |
import time |
| 24 |
import typing as t |
| 25 |
from functools import partial |
| 26 |
@@ -36,6 +38,24 @@ def validate_string_dict(dct: t.Dict[str, str]) -> None: |
| 27 |
raise ValueError("value %r in dict must be a string" % v) |
| 28 |
|
| 29 |
|
| 30 |
+def get_docstring_indent(doc: str) -> str: |
| 31 |
+ # Python 3.13 dedents docstrings automatically. |
| 32 |
+ # In this module, the docstring indent is the indent of the |
| 33 |
+ # first non-blank line after the initial line, which is returned |
| 34 |
+ # as a whitespace string. |
| 35 |
+ # This is not a general method for determining the indent of a docstring! |
| 36 |
+ # See the source code of textwrap.dedent() for that. |
| 37 |
+ doclines = doc.split("\n") |
| 38 |
+ for line in doclines[1:]: |
| 39 |
+ if re.match(r"\s*$", line): |
| 40 |
+ continue |
| 41 |
+ linematch = re.match(r"(\s*)\S", line) |
| 42 |
+ assert linematch is not None |
| 43 |
+ return linematch.group(1) |
| 44 |
+ # If there was no content in the docstring beyond the initial line |
| 45 |
+ return "" |
| 46 |
+ |
| 47 |
+ |
| 48 |
def reqrep(wrapped: t.Callable, meth: t.Callable, channel: str = "shell") -> t.Callable: |
| 49 |
wrapped = wrapped(meth, channel) |
| 50 |
if not meth.__doc__: |
| 51 |
@@ -43,31 +63,34 @@ def reqrep(wrapped: t.Callable, meth: t.Callable, channel: str = "shell") -> t.C |
| 52 |
# so don't bother building the wrapped docstring |
| 53 |
return wrapped |
| 54 |
|
| 55 |
- basedoc, _ = meth.__doc__.split("Returns\n", 1) |
| 56 |
- parts = [basedoc.strip()] |
| 57 |
- if "Parameters" not in basedoc: |
| 58 |
- parts.append( |
| 59 |
- """ |
| 60 |
+ params_header = """\ |
| 61 |
Parameters |
| 62 |
---------- |
| 63 |
""" |
| 64 |
- ) |
| 65 |
- parts.append( |
| 66 |
- """ |
| 67 |
- reply: bool (default: False) |
| 68 |
+ returns_doc = """\ |
| 69 |
+ reply : bool (default: False) |
| 70 |
Whether to wait for and return reply |
| 71 |
- timeout: float or None (default: None) |
| 72 |
+ |
| 73 |
+ timeout : float or None (default: None) |
| 74 |
Timeout to use when waiting for a reply |
| 75 |
|
| 76 |
Returns |
| 77 |
------- |
| 78 |
- msg_id: str |
| 79 |
+ msg_id : str |
| 80 |
The msg_id of the request sent, if reply=False (default) |
| 81 |
- reply: dict |
| 82 |
+ |
| 83 |
+ reply : dict |
| 84 |
The reply message for this request, if reply=True |
| 85 |
- """ |
| 86 |
- ) |
| 87 |
- wrapped.__doc__ = "\n".join(parts) |
| 88 |
+ """ |
| 89 |
+ |
| 90 |
+ basedoc, _ = meth.__doc__.split("Returns\n", 1) |
| 91 |
+ parts = [basedoc.strip()] |
| 92 |
+ indent = get_docstring_indent(basedoc) |
| 93 |
+ if "Parameters" not in basedoc: |
| 94 |
+ parts.append(textwrap.indent(textwrap.dedent(params_header), indent)) |
| 95 |
+ parts.append(textwrap.indent(textwrap.dedent(returns_doc), indent)) |
| 96 |
+ |
| 97 |
+ wrapped.__doc__ = "\n\n".join(parts) |
| 98 |
return wrapped |
| 99 |
|
| 100 |
|