Skip to content

Indentation characters of the original code are not preserved #379

Description

@kelly-eclipse

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    freshThis is a new issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions