Skip to main content

The most uncompromising Python code formatter ever!

Project description

Dethklok

We are here to make coffee metal. We will make everything metal. Blacker than the blackest black times infinity.

  • Nathan Explosion, Dethklok

The most uncompromising Python code formatter ever!

Dethklok uses a few code formatters under the hood:

  • astunparse -- extracted and improved unparse.py from the CPython source code. This script gets AST and formats it into the code, without knowing anything about how the code looked like before. From Python 3.9, it is a part of the standard library: ast.unparse.
  • autopep8 -- a powerful and mature code formatter. We run it to make the produced code a bit less ugly.

Installation

Supported Python versions are limited by what astunparse supports. For now, it's Python <=3.7.

python3 -m pip install --user dethklok

Usage

python3 -m dethklok format **/*.py

FAQ

Q: Is it flake8 compatible?

Almost. The tool outputs some statements in a single-line. The most noticeable example is docstrings. None of autoformatters I tried can make it multiline again.

Q: When should I use it?

The short answer is "NEVER". This is an experimental project that shows that producing the same AST without taking into account the initial formatting can't make the code readable. I am a mediaocre developer but still formatting of my code has an intention and makes the code more readable: I group elements, I left comments, I align columns, I parenthesize parts of complex arithmetic operations. While some parts of the code can be easily autoformatted without breaking it, some parts shouldn't.

Q: Is it a joke about black or what?

Well, a bit. Black targets to reformat everything, to have an opinion about part of every Python code. I admire that goal and really hope the project will grow and become mature some day. However, right now it's unstable and tries to fix things it can't fix (yet).

There are some formatters with similar philosophy in other languages (go format, cargo format, elixir format), and there are amazing. The difference is that these languages were designed with the formatter in mind, and every new feature is thoughtfully integrated with the formatter to make the result nice and idiomatic. Python is different.

Q: What should I use instead.

Luckily, there are plenty of cool formatters. They are doing only a small task but handle it really well. A few notable examples:

  • autopep8 fixes only parts of the code that flake8 complaints about.
  • isort groups and sorts imports.
  • add-trailing-comma adds trailing commas and formats long function call statements.
  • unify changes single quotes by double or vice versa, depends on what you prefer.

See the full list in awesome-python-code-formatters.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dethklok-1.0.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

dethklok-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file dethklok-1.0.0.tar.gz.

File metadata

  • Download URL: dethklok-1.0.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: DepHell/0.8.3

File hashes

Hashes for dethklok-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d261656839a44535c5deac911df95c972a59f63340d27b0e978696857479ae9f
MD5 e5ab42e334dffc0a9a9e18b94b12b292
BLAKE2b-256 3dd04ff7ced7a05094c50abbbed38dfd093dae88bf7f39d01ef4056c9f9aa73e

See more details on using hashes here.

File details

Details for the file dethklok-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dethklok-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: DepHell/0.8.3

File hashes

Hashes for dethklok-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a09306030e32aa175d95199984cfcff36a4ce6130f49404965dc819a27e738a
MD5 cca19ddc57d2c9455af002232a870de6
BLAKE2b-256 3fb778dd447d5b9116b0549789a972151c2451e9194d78efc63b3d192ebadaa0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page