Skip to content

wbolster/black-macchiato

Repository files navigation

black-macchiato

https://circleci.com/gh/wbolster/black-macchiato.svg?style=svg
I see some Python and I want it painted black
Mick Jagger (The Rolling Stones)

What?

This is a small utility built on top of the black Python code formatter to enable formatting of partial files.

Why?

Python code should be black, just like coffee. However, sometimes other people insist on adding milk for unexplicable reasons. Since caffè latte is undrinkable, you eventually settle for caffè macchiato as a compromise.

In other words, you want to use black for the code you write, but for some reason you cannot convert whole files, e.g. when contributing to upstream codebases that are not under your complete control.

However, partial formatting is not supported by black itself, for various good reasons, and it won't be implemented either (134, 142, 245, 370, 511).

This is where black-macchiato enters the stage. This tool is for those who want to do partial formatting anyway. It also accepts indented blocks, which means that you can format an indented method inside a class, or a small block of code.

Note that this tool is a stopgap measure, and you should avoid using it if you can.

How?

To install, use:

pip install black-macchiato

The black-macchiato command reads from standard input, and writes to standard output. Any command line flags are forwarded to black.

Alternatively, you can invoke the module directly through the python executable, which may be preferable depending on your setup. Use python -m macchiato instead of black-macchiato in that case.

Example:

$ echo "    if True: print('hi')" | black-macchiato
    if True:
        print("hi")

Integrating this piping of input and output with your editor is left as an exercise to the reader. For instance, Vim users can use vim-black-macchiato, and Emacs users can use python-black.el.

History

  • 1.3.0 (2020-05-14)
    • Refactor so that wrapping functionality can be reused more easily (7)
    • Use project config file, if any (4, 7)
    • Improved Vim integration (6)
  • 1.2.0 (2020-03-23)
    • Handle else/elif/except/finally and indented functions better (5)
  • 1.1.0 (2019-05-17)
    • Handle lines ending with a colon
    • Add tests
  • 1.0.1 (2019-02-06)
    • Add more metadata
  • 1.0.0 (2019-02-06)
    • Initial release

License

BSD. See LICENSE.rst.

About

paints part of your python code black

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages