mirror of
https://github.com/django/django.git
synced 2025-11-17 18:48:15 +00:00
Fix docstring trimming for non-empty first lines
Allow trim_docstring to handle docstrings where the text starts on the first line, preventing rendering errors in admindoc. Adds tests for single-line and non-empty first-line docstrings.
This commit is contained in:
parent
e8fcdaad5c
commit
907a493cfd
2 changed files with 20 additions and 1 deletions
|
|
@ -34,7 +34,9 @@ def trim_docstring(docstring):
|
|||
return ''
|
||||
# Convert tabs to spaces and split into lines
|
||||
lines = docstring.expandtabs().splitlines()
|
||||
indent = min(len(line) - len(line.lstrip()) for line in lines if line.lstrip())
|
||||
# Determine indentation from all lines except the first (PEP 257)
|
||||
indent_lines = [line for line in lines[1:] if line.lstrip()]
|
||||
indent = min(len(line) - len(line.lstrip()) for line in indent_lines) if indent_lines else 0
|
||||
trimmed = [lines[0].lstrip()] + [line[indent:].rstrip() for line in lines[1:]]
|
||||
return "\n".join(trimmed).strip()
|
||||
|
||||
|
|
|
|||
|
|
@ -44,6 +44,23 @@ class TestUtils(AdminDocsSimpleTestCase):
|
|||
)
|
||||
self.assertEqual(trim_docstring_output, trimmed_docstring)
|
||||
|
||||
def test_trim_docstring_first_line_not_empty(self):
|
||||
"""
|
||||
Test trim_docstring with docstrings where text starts on the first line.
|
||||
This is a common docstring style and should not cause errors.
|
||||
"""
|
||||
docstring = """test tests something.
|
||||
This is the second line.
|
||||
"""
|
||||
expected = "test tests something.\nThis is the second line."
|
||||
self.assertEqual(trim_docstring(docstring), expected)
|
||||
|
||||
def test_trim_docstring_single_line(self):
|
||||
"""Test trim_docstring with a single-line docstring."""
|
||||
docstring = """Single line docstring."""
|
||||
expected = "Single line docstring."
|
||||
self.assertEqual(trim_docstring(docstring), expected)
|
||||
|
||||
def test_parse_docstring(self):
|
||||
title, description, metadata = parse_docstring(self.docstring)
|
||||
docstring_title = (
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue