Commit 8a5b3bf
authored
fix: bug#15089 - Removing leading indentation (#15110)
# 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:
```
>>> print(1)
```
IPython now strips the `>>>` prompt and also removes the extra
indentation, so the result is:
```
print(1)
```
Previously, the result would have been:
```
print(1)
```
which can cause an `IndentationError` at top-level.
## What changed
- `inputtransformer2.py`
- Enhanced PromptStripper to:
- Detect doctest-style pastes when any line matches `^\s*>>>`.
- In doctest-mode, strip `^\s*>>>\s?` and `^\s*\.\.\.\s?`
(continuation).
- After stripping, apply `textwrap.dedent()` to the joined block, then
return dedented lines.
- Only apply dedent when doctest prompts were actually stripped (to
avoid changing non-doctest pastes).
- Only treat `...` as a continuation prompt when a `>>>` is present in
the same block (avoids confusing Python Ellipsis with doctest
continuation).
- `test_inputtransformer2_line.py`
- Added/updated tests covering single-line doctest, indented `>>>`,
multi-line `>>>` + `...` doctest, and standalone ... (unchanged when no
`>>>`).
## Description of changes
- Update `IPython/core/inputtransformer2.py` to enhance the
`PromptStripper`:
- Detect doctest-mode for a pasted block when any line matches
`^\s*>>>`.
- In doctest-mode, strip indented doctest prompts:
- `^\s*>>>\s?` (primary prompt)
- `^\s*\.\.\.\s?` (continuation prompt)
- After stripping doctest prompts, apply `textwrap.dedent()` to the
joined block and return the dedented lines.
- Only apply `dedent()` when doctest prompts were actually stripped to
avoid changing normal paste semantics.
- Update/add tests in `tests/test_inputtransformer2_line.py` for 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 raise `IndentationError`. 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:
```py
>>> print(1)
```
Before:
```py
print(1)
```
After:
```py
print(1)
```
### Indented xdoctest-style prompt
Input:
```py
>>> print(1)
```
After:
```py
print(1)
```
### Multi-line doctest with continuation
Input:
```py
>>> for i in range(2):
... print(i)
```
After:
```py
for i in range(2):
print(i)
```
### Standalone continuation prompt (unchanged)
Input:
```py
... print(1)
```
After (unchanged because no `>>>` present):
```py
... print(1)
```
## 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's `Ellipsis`
literal with doctest continuations.
- Regexes used are tolerant of leading whitespace to support indented
`>>>` prompts (xdoctest-style).
## Related issues
- Fixes #15089
## Testing performed
- Added and updated tests in `tests/test_inputtransformer2_line.py` to
cover:
- Single-line doctest with extra indentation: `">>> print(1)\n" ->
"print(1)\n"`
- Leading whitespace before `>>>`: `" >>> print(1)\n" -> "print(1)\n"`
- Multiline doctest using `>>>` and `...` continuation lines.
- Standalone `...` only lines remain unchanged.
## Doc
Contribution by Gittensor, see my contribution statistics at
https://gittensor.io/miners/details?githubId=1913729632 files changed
Lines changed: 149 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
73 | 111 | | |
74 | 112 | | |
75 | 113 | | |
76 | 114 | | |
77 | 115 | | |
78 | 116 | | |
79 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
80 | 169 | | |
81 | 170 | | |
82 | 171 | | |
83 | 172 | | |
84 | 173 | | |
85 | 174 | | |
86 | 175 | | |
87 | | - | |
| 176 | + | |
| 177 | + | |
88 | 178 | | |
89 | 179 | | |
90 | 180 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
48 | 70 | | |
49 | 71 | | |
50 | | - | |
51 | 72 | | |
52 | 73 | | |
53 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
54 | 101 | | |
55 | 102 | | |
56 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
57 | 112 | | |
58 | 113 | | |
59 | 114 | | |
| |||
0 commit comments