PyCQA / PyCQA/docformatter

Indentation characters of the original code are not preserved

Open
#379 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

fresh
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:

  1. _do_add_formatted_docstring and _do_add_unformatted_docstring build
    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.

  2. _do_format_code runs the whole file through tokenize.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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.