can python black formatter use tabulators instead of spaces?

  • Last Update :
  • Techknowledgy :

Apr 16, 2020 , Released: Apr 16, 2020 , Uploaded Apr 16, 2020 py36 , Uploaded Apr 16, 2020 source

A compatible `.isort.cfg`
multi_line_output = 3
include_trailing_comma = True
force_grid_wrap = 0
use_parentheses = True
line_length = 88

The equivalent command line is:

$ isort--multi - line = 3--trailing - comma--force - grid - wrap = 0--use - parentheses--line - width = 88[]
Example `pyproject.toml`
line - length = 88
target - version = ['py37']
include = '\.pyi?$'
exclude = ''

   /(\.eggs # exclude a few common directories in the |
   \.git # root of the project |
   \.hg |
   \.mypy_cache |
   \.tox |
   \.venv |
   _build |
   buck - out |
   build |
) /
| # also separately exclude a file named in
   # the root of the project

Suggestion : 2

It would be nice to have a --use-tabs or similar to tell it to output tabs instead. By manually switching the ' '*self.depth into '\t'*self.depth in the Line.str() method it seems to support this rather nicely.,Black always output 4 spaces for indentation.,at that time to tabulate was to make spaced words flat while to indent was to create two inequal sections on a sheet.,If your editor is configured to use tabs for Python indentation, that means you are unable to meaningfully contribute to any open source software, including Python itself. Some code editors and other tooling will be confused by tabs in Python indentation, leading to mixed spaces and tabs which is not supported on Python 3.

Fixes psf #47
Closes psf #47
Closes psf #47

(cherry picked from commit 304a7a5)
Closes psf #47

Fix case where tabs can exceed line length

Closes # 9
Closes psf #47, psf# 513, psf #640, psf# 2122, psf #2584, psf# 2798

Suggestion : 3

Indentation is the whitespace at the beginning of a code line. You can use one of two whitespace characters, a space or a tab, to indent your code. Although either character works, the best practice is to use spaces instead of tabs for indentation. ,If you add comments to the end of a code line, put two spaces after the end of the code and before the # character that begins the comment:,We separate the items lists and dictionaries, as well as the parameters in function def statements, using comma characters. You should place no spaces before these commas and a single space after them, as in this example:,As for the length of each indentation level, the common practice in Python code is four spaces per level of indentation. The space characters in the following example have been marked with periods to make them visible:

We can write code in many ways that result in identical behavior. For example, we can write a list with a single space after each comma and use one kind of quote character consistently:

spam = ['dog', 'cat', 'moose']

But even if we wrote the list with a varying number of spaces and quote styles, we’d still have syntactically valid Python code:

spam = ['dog', 'cat', "moose"]

The reason is that these two characters behave differently. A space character is always rendered on the screen as a string value with a single space, like this ' '. But a tab character, which is rendered as a string value containing an escape character, or '\t', is more ambiguous. Tabs often, but not always, render as a variable amount of spacing so the following text begins at the next tab stop. The tab stop are positioned every eight spaces across the width of a text file. You can see this variation in the following interactive shell example, which first separates words with space characters and then with tab characters:

>>> print('Hello there, friend!\nHow are you?')
Hello there, friend!
   How are you ?
Hello there, friend!
   How are you ?

If you don’t leave spaces between operators and identifiers, your code will appear to run together. For example, this line has spaces separating operators and variables:

YES: blanks = blanks[: i] + secretWord[i] + blanks[i + 1: ]

This line removes all spacing:

NO: blanks = blanks[: i] + secretWord[i] + blanks[i + 1: ]

Suggestion : 4

So I'd recommend to keep using this plugin and use a simple command that replaces each 4 spaces at the beginning of each line with 2. which its implementation turned out to be suspiciously simple:

import pynvim
import re

class Iindent(object):
" Iindent plugin: Black can't stop me from using 2 spaces "
def __init__(self, nvim):
   self.nvim = nvim
self.pattern = re.compile(r "^(\s\s\s\s)+")

@pynvim.command("Iindent", nargs = "*", range = "")
def iindent(self, args, range):
   buffer = self.nvim.current.buffer
new_buffer = []
for line in buffer:
   x =
if x:
   _, end = x.span()
line = re.sub(self.pattern, " " * int(end / 2), line)
new_buffer += [line]
buffer[: ] = new_buffer

Suggestion : 5

Black ignores previous formatting and applies uniform horizontal and vertical whitespace to your code. The rules for horizontal whitespace can be summarized as: do whatever makes pycodestyle happy.,Black avoids spurious vertical whitespace. This is in the spirit of PEP 8 which says that in-function vertical whitespace should only be used sparingly.,As for vertical whitespace, Black tries to render one full expression or simple statement per line. If this fits the allotted line length, great.,Please note that Black does not add or remove any additional nested parentheses that you might want to have for clarity or further code organization. For example those parentheses are not going to be removed:

# in:

   j = [1,

# out:

   j = [1, 2, 3]
# in:

   ImportantClass.important_method(exc, limit, lookup_lines, capture_locals, extra_argument)

# out:

      exc, limit, lookup_lines, capture_locals, extra_argument
# in:

   def very_important_function(template: str, * variables, file: os.PathLike, engine: str, header: bool = True, debug: bool = False):
"Applies `variables` to the `template` and writes to `file`."
with open(file, 'w') as f:

   # out:

   def very_important_function(
      template: str,
      * variables,
      file: os.PathLike,
      engine: str,
      header: bool = True,
      debug: bool = False,
"Applies `variables` to the `template` and writes to `file`."
with open(file, "w") as f:
# in:

   if some_short_rule1\
and some_short_rule2:

   # out:

   if some_short_rule1 and some_short_rule2:

   # in:

   if some_long_rule1\
and some_long_rule2:

   # out:

   if (
      some_long_rule1 and some_long_rule2
max - line - length = 88
   select = C, E, F, W, B, B950
extend - ignore = E203, E501
max - line - length = 88
extend - ignore = E203