You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PythonCodeSplitter with strip_docstrings=True deletes code on the same line as a docstring #13308
Describe the bug PythonCodeSplitter with strip_docstrings=True removes a docstring by dropping whole source lines. If other code sits on the same line as the docstring, that code gets dropped too. For example """Docs."""; return 5 loses the return 5, and the function is left with an empty body.
Nothing is raised, and the docstring still shows up in meta["docstrings"], so the chunk looks fine unless you compare it to the source.
The cause is in PythonCodeSplitter._strip_docstring, where after = source_lines[ds_end:unit_end] skips the last docstring line completely. The fix for #12330 adds a pass when the docstring is the only statement, but it doesn't cover this case.
Error message
No error from the splitter. The chunk from the example below fails ast.parse with IndentationError: expected an indented block after function definition on line 1.
Expected behavior
Only the docstring is removed. If that can't be done safely, the docstring should stay in the chunk, like the splitter already does when the docstring is on the same line as the def.
Additional context
I found this by reading the code, not in a real project. The style is rare, but the code loss is silent. It also happens with code after the closing quotes of a multi-line docstring ("""Docs\n more."""; x = 1) and with class docstrings ("""Docs."""; y = 2).
Activity