Indentation characters of the original code are not preserved
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 598
- Forks
- 93
- PR merge metrics
- No merged PRs in 30d
Description
docformatter unnecessarily replaces tab characters with spaces. When a file
uses tabs, running docformatter on it changes indentation characters both
inside docstrings and in code outside the docstring.
Reproducer
def some_function(
a_parameter
):
"""Important documentation.
with an indented item
"""
...
$ docformatter file.py
--- before/file.py
+++ after/file.py
@@ -1,8 +1,8 @@
def some_function(
- a_parameter
+ a_parameter
):
"""Important documentation.
- with an indented item
- """
+ with an indented item
+ """
...
Cause
Two independent code paths substitute spaces for the original indentation
characters:
-
_do_add_formatted_docstringand_do_add_unformatted_docstringbuild
the indent for the docstring's internal lines as" " * token.start[1],
based on the docstring's opening line. When the opening line is indented with
a tab, each tab inside the docstring is replaced by a single space. -
_do_format_coderuns the whole file throughtokenize.untokenize,
which reconstructs inter-token whitespace as" " * col_offset. This means
that tabs on continuation lines are also replaced by a single space.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with _do_add_formatted_docstring and _do_add_unformatted_docstring, then inspect _do_format_code and its use of tokenize.untokenize. Run the provided tab-indented reproducer with docformatter and verify that tabs inside docstrings and on code continuation lines remain tabs while formatting still works.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100