Fix issue #174: Enable line wrapping in docstrings with argument lists while preserving section formatting #324
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR resolves issue #174 by implementing intelligent handling of mixed-style docstrings, allowing docformatter to wrap long description lines while preserving the formatting of argument lists (Args/Returns sections). Additionally, it includes critical bug fixes for URL handling and adds definition list detection.
Related Issues
Fixes #174
Problem
Previously, docformatter would not wrap long lines in docstrings when argument lists were present, unless
--force-wrap
was used. However,--force-wrap
would incorrectly wrap the argument lists themselves, making them unreadable:Before (no wrapping):
Before (with
--force-wrap
- incorrect):Solution
After (with this fix):
Changes Made
1. Enhanced Field List Detection (
src/docformatter/patterns/fields.py
)Args:
,Returns:
, etc.) even when using Sphinx/Epytext styles_wrap_parameters = False
for section-based styles to preserve their formatting2. Improved List Pattern Recognition (
src/docformatter/patterns/lists.py
)`term`
) to avoid false positives3. Enhanced Description Wrapping (
src/docformatter/strings.py
)do_split_description()
to handle mixed-style scenarios4. Force Wrap Enhancement (
src/docformatter/wrappers/description.py
)force_wrap
logic that bypasses field list detection entirely--force-wrap
always wraps everything as regular text5. Critical URL Regex Fix (
src/docformatter/constants.py
)URL_REGEX
pattern that was causing incorrect URL splitting[.|\.\. _?[\w. :]+
→|\.\. _[\w. :]+
6. Code Quality Improvements
7. Test Updates
list_patterns.toml
expectation fromtrue
tofalse
for NumPy sections in Sphinx docstest_docformatter.py
to reflect corrected URL handlingdescription_wrappers.toml
to show new wrapping behavior for mixed-content docstringsTechnical Details
The solution works by:
_wrap_parameters = False
)Testing
--force-wrap
still works as expected for complete wrappingBehavioral Impact
This change modifies the default behavior for mixed-style docstrings:
--force-wrap
Backward Compatibility
This change maintains full backward compatibility:
--force-wrap
option behavior is unchanged (but improved)Files Modified
src/docformatter/constants.py
- Fixed URL regex patternsrc/docformatter/patterns/fields.py
- Enhanced field list detection, code cleanupsrc/docformatter/patterns/lists.py
- Added definition list detection, improved style handlingsrc/docformatter/strings.py
- Enhanced description splitting logicsrc/docformatter/wrappers/description.py
- Added force wrap shortcuttests/test_docformatter.py
- Updated URL handling test expectationstests/_data/string_files/description_wrappers.toml
- Updated wrapping expectationstests/_data/string_files/list_patterns.toml
- Updated list detection expectationsType of Change