Skip to main content

More Black! - Denser Black formatting

This is a fork of the Black code formatter

Requires Python 3.10

Black itself requires Python 3.10 or later to run. It still formats code written for Python 3.3 through 3.15.

Problem

I love Black formatting because I agree with its formatting choices, but it does have one pathology: Excessive indenting on data structures

Here is an example of Black formatting; many lines are wasted on lonely brackets:

my_method(
    [
        {
            "name": "a",
            "value": 42,
        }
    ]
)

Solution: More Black!

When there is only one property (or list item, or parameter), then do not make a new line.

my_method([{
    "name": "a",
    "value": 42,
}])

Usage

Please read the official Black documentation. The command is still black, and every option works the same.

pip install mo-black

More about this fork

Three rules differ from upstream, all of them in src/black/linegen.py and src/black/brackets.py:

A bracket whose body is a single item is not split there. _body_is_lone_item() decides the body is one thing — no comma, no operator, no and/or/if/in/is at that depth — and the split descends into it instead, leaving the brackets hugged. It applies at every depth, so nested single-key dictionaries collapse together:

schema = {"mappings": {"test": {"properties": {"one_value": {
    "type": "keyword",
    "store": True,
}}}}}

Every attribute dot is a split point, not only a dot that follows a call, so a long method chain breaks before each name.

Parentheses around something already atomic are removed. not (a) becomes not a, and foo((a), (b)) becomes foo(a, b). Every slot in the grammar's precedence cascade bottoms out at atom, so parentheses around one group nothing. Parentheses that group, build a tuple, or make a generator are left alone.

Everything else is upstream Black.

Development

Be sure you are in the mo-black main directory.

Setup virtual environment

python -m venv .venv
.venv\Scripts\activate

Install the package and its test requirements

pip install -e ".[d,jupyter]" pytest pytest-xdist

Run the tests

pytest tests

Here is the same for Linux and macOS...

python -m venv .venv
source .venv/bin/activate
pip install -e ".[d,jupyter]" pytest pytest-xdist
pytest tests

Deployment

packaging/deploy.py builds the distributions and uploads them.

python packaging/deploy.py --next-version           # what the next release would be
python packaging/deploy.py --compiled --build-only  # build it all, upload nothing
python packaging/deploy.py --compiled               # and upload

The version is never written into a file: deploy.py works out the next major.minor.%y%j and hands it to the build. Uploading happens only from master, and the tag is written afterwards, once PyPI has accepted.

--compiled additionally builds the mypyc wheels, which is what makes Black fast: one per interpreter and platform, in parallel, taking around twenty minutes. Without it you get the pure-Python wheel, which formats about 1.7x slower.

Download files

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

Source Distribution

mo_black-4.5.26234.tar.gz (670.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mo_black-4.5.26234-py3-none-any.whl (198.1 kB view details)

Uploaded Python 3

File details

Details for the file mo_black-4.5.26234.tar.gz.

File metadata

  • Download URL: mo_black-4.5.26234.tar.gz
  • Upload date:
  • Size: 670.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.1

File hashes

Hashes for mo_black-4.5.26234.tar.gz
Algorithm Hash digest
SHA256 468ed8d45bcd79a8dea591e022d7b3b3dc7b2f1f139046c3a4125b4597239702
MD5 2c95df601738639cf04906300ea200d9
BLAKE2b-256 9ca0af930bd1705337a57219eb19dffd36e7912213a6cef38e9c6df7e4cdf369

See more details on using hashes here.

File details

Details for the file mo_black-4.5.26234-py3-none-any.whl.

File metadata

  • Download URL: mo_black-4.5.26234-py3-none-any.whl
  • Upload date:
  • Size: 198.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.1

File hashes

Hashes for mo_black-4.5.26234-py3-none-any.whl
Algorithm Hash digest
SHA256 48dc9559d9927428b6bcd144c1ecf6541ea57e62cdc9d042c9d744cc9f20a997
MD5 f6f684c6f63fdf06a63a0b3c37001337
BLAKE2b-256 964418d77825a2d89a3bf415d81c590492723b6a6673b5a01be6eff61bc7b69f

See more details on using hashes here.

Release history Release notifications | RSS feed

22.364.23070

1 file

4.6.26234

12 files

This release

4.5.26234 This release

2 files

3.90.20218

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page