fix: bug#15089 - Removing leading indentation #15110
Conversation
|
Hi @krassowski, All required checks are passing. Thank you very much for your time and for maintaining IPython. |
|
What happens when pasting into a docstring? Does it distinguish these cases? |
|
Good point — preserving doctest prompts inside docstrings makes sense. I’ll update this so that when the paste occurs inside a triple-quoted string (docstring), we skip doctest stripping and dedenting, and only apply the transformation in normal code contexts. I’ll add a targeted test case covering pasting doctest snippets inside a docstring to ensure the prompts remain intact. |
|
@krassowski I’ve pushed an update that preserves doctest prompts when pasting inside triple-quoted strings and added a targeted test covering this case. |
|
Thanks, let's try it. |
Fix doctest paste: strip prompts and dedent (#15089)
Summary
This PR improves how IPython handles pasting doctest and xdoctest code snippets. When pasting code like:
IPython now strips the
>>>prompt and also removes the extra indentation, so the result is:Previously, the result would have been:
which can cause an
IndentationErrorat top-level.What changed
inputtransformer2.py^\s*>>>.^\s*>>>\s?and^\s*\.\.\.\s?(continuation).textwrap.dedent()to the joined block, then return dedented lines....as a continuation prompt when a>>>is present in the same block (avoids confusing Python Ellipsis with doctest continuation).test_inputtransformer2_line.py>>>, multi-line>>>+...doctest, and standalone ... (unchanged when no>>>).Description of changes
IPython/core/inputtransformer2.pyto enhance thePromptStripper:^\s*>>>.^\s*>>>\s?(primary prompt)^\s*\.\.\.\s?(continuation prompt)textwrap.dedent()to the joined block and return the dedented lines.dedent()when doctest prompts were actually stripped to avoid changing normal paste semantics.tests/test_inputtransformer2_line.pyfor the new behavior.Motivation and context
Users often copy doctest examples from documentation or tools that show doctest prompts and indentation (for example, xdoctest-style indented examples). Previously IPython removed the
>>>prompt but preserved the remaining indentation, which could turn a pasted, single-line doctest into an indented top-level statement and raiseIndentationError. This PR makes doctest pastes behave as users expect: prompts are removed and the code block is normalized by dedenting.Examples (Before → After)
Single-line doctest
Input:
Before:
After:
Indented xdoctest-style prompt
Input:
After:
Multi-line doctest with continuation
Input:
After:
Standalone continuation prompt (unchanged)
Input:
After (unchanged because no
>>>present):Implementation notes
dedent()is applied only after doctest prompts were detected and stripped. This avoids affecting normal paste behavior for non-doctest code, and prevents accidental removal of meaningful indentation in user-pasted code....is considered a continuation prompt only when the pasted block contains at least one>>>, to avoid confusing Python'sEllipsisliteral with doctest continuations.>>>prompts (xdoctest-style).Related issues
Testing performed
tests/test_inputtransformer2_line.pyto cover:">>> print(1)\n" -> "print(1)\n">>>:" >>> print(1)\n" -> "print(1)\n">>>and...continuation lines....only lines remain unchanged.Doc
Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=191372963