Skip to content

Latest commit

 

History

History
189 lines (140 loc) · 15.5 KB

python_black.md

File metadata and controls

189 lines (140 loc) · 15.5 KB

black documentation

black - GitHub{target=_blank}

Configuration in Mega-Linter

Variable Description Default value
PYTHON_BLACK_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
PYTHON_BLACK_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src|lib)
Include every file
PYTHON_BLACK_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test|examples)
Exclude no file
PYTHON_BLACK_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".py"]
PYTHON_BLACK_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
PYTHON_BLACK_CONFIG_FILE black configuration file name
Use LINTER_DEFAULT to let the linter find it
.python-black
PYTHON_BLACK_RULES_PATH Path where to find linter configuration file Workspace folder, then Mega-Linter default rules
PYTHON_BLACK_DISABLE_ERRORS Run linter but consider errors as warnings true
PYTHON_BLACK_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use black in your favorite IDE to catch errors before Mega-Linter !

IDE Extension Name Install
Atom python-black Visit Web Site{target=_blank}
Emacs blacken Visit Web Site{target=_blank}
Emacs reformatter.el Visit Web Site{target=_blank}
Emacs elpy Visit Web Site{target=_blank}
IDEA black Visit Web Site{target=_blank}
Sublime Text sublack Visit Web Site{target=_blank}
Visual Studio Code VsCode Python Extension Install in VsCode{target=_blank}

Mega-Linter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default Mega-Linter Flavor 88 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 45 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .py

Example calls

black --diff --check myfile.py
black --config .python-black --diff --check myfile.py
black --config .python-black myfile.py

Help content

Usage: black [OPTIONS] [SRC]...

  The uncompromising code formatter.

Options:
  -c, --code TEXT                 Format the code passed in as a string.
  -l, --line-length INTEGER       How many characters per line to allow.
                                  [default: 88]

  -t, --target-version [py27|py33|py34|py35|py36|py37|py38]
                                  Python versions that should be supported by
                                  Black's output. [default: per-file auto-
                                  detection]

  --pyi                           Format all input files like typing stubs
                                  regardless of file extension (useful when
                                  piping source on standard input).

  -S, --skip-string-normalization
                                  Don't normalize string quotes or prefixes.
  --check                         Don't write the files back, just return the
                                  status.  Return code 0 means nothing would
                                  change.  Return code 1 means some files
                                  would be reformatted. Return code 123 means
                                  there was an internal error.

  --diff                          Don't write the files back, just output a
                                  diff for each file on stdout.

  --color / --no-color            Show colored diff. Only applies when
                                  `--diff` is given.

  --fast / --safe                 If --fast given, skip temporary sanity
                                  checks. [default: --safe]

  --include TEXT                  A regular expression that matches files and
                                  directories that should be included on
                                  recursive searches.  An empty value means
                                  all files are included regardless of the
                                  name.  Use forward slashes for directories
                                  on all platforms (Windows, too).  Exclusions
                                  are calculated first, inclusions later.
                                  [default: \.pyi?$]

  --exclude TEXT                  A regular expression that matches files and
                                  directories that should be excluded on
                                  recursive searches.  An empty value means no
                                  paths are excluded. Use forward slashes for
                                  directories on all platforms (Windows, too).
                                  Exclusions are calculated first, inclusions
                                  later.  [default: /(\.direnv|\.eggs|\.git|\.
                                  hg|\.mypy_cache|\.nox|\.tox|\.venv|\.svn|_bu
                                  ild|buck-out|build|dist)/]

  --force-exclude TEXT            Like --exclude, but files and directories
                                  matching this regex will be excluded even
                                  when they are passed explicitly as arguments

  -q, --quiet                     Don't emit non-error messages to stderr.
                                  Errors are still emitted; silence those with
                                  2>/dev/null.

  -v, --verbose                   Also emit messages to stderr about files
                                  that were not changed or were ignored due to
                                  --exclude=.

  --version                       Show the version and exit.
  --config FILE                   Read configuration from FILE path.
  -h, --help                      Show this message and exit.

Installation on mega-linter Docker image

  • PIP packages (Python):

Example success log

Results of black linter (version 19.10)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/python_black/
-----------------------------------------------

[SUCCESS] .automation/test/python/python_good_1.py
    All done! ✨ 🍰 ✨
    1 file would be left unchanged.

Example error log

Results of black linter (version 19.10)
See documentation on https://nvuillam.github.io/mega-linter/descriptors/python_black/
-----------------------------------------------

[ERROR] .automation/test/python/python_bad_1.py
    error: cannot format .automation/test/python/python_bad_1.py: Cannot parse: 15:23: if github_token is None
    Oh no! 💥 💔 💥
    1 file would fail to reformat.