Skip to main content

A minimalist Python formatting tool

Project description

Parsimony

A minimalist line-breaker that adds the fewest breaks to fit the line.

Unlike black/blue/ruff, which explode the outermost bracket first and stack every closing bracket on its own line, this tool makes the smallest change that fits an over-long line, using two complementary strategies: exploding a bracket, or breaking a method chain.

Exploding a bracket opens the minimum number of brackets and coalesces adjacent ones. e.g.

response = authed_client().get(reverse('forwarding:detail', args=[c.id]))

becomes

response = authed_client().get(reverse(
    'forwarding:detail',
    args=[c.id],
))

rather than the deep staircase that black/ruff produce.

Breaking a method chain splits a long .-chain one segment per line. It is the fallback for lines no bracket can shorten. e.g.

django_queryset = SomeModel.objects.filter(active=True).order_by('-id')

becomes

django_queryset = (
    SomeModel
    .objects
    .filter(active=True)
    .order_by('-id')
)

The motivation is readability: coalescing keeps related calls grouped visually and reduces vertical noise, while chain-breaking gives long fluent pipelines a clean one-operation-per-line shape.

Installation

pip install kaapstorm-parsimony

Algorithm

While a physical line exceeds LINE_LENGTH, explode one container that intersects an over-long line, chosen by:

  1. multi-item containers only (>= 2 args/elements); single-item containers are never opened — see "Contract" below.
  2. outermost (shallowest) of those.

Then re-measure and repeat. "Explode" = each element on its own line at a +4 hanging indent, with a trailing comma, and the closing bracket dedented to the opening line's indent. Because we only open the chosen container and never its single-item parents, adjacent openers like get(reverse( remain coalesced. Coalescing is not about depth, only about not opening single-item wrappers.

When no multi-item container intersects a remaining over-long line, fall back to breaking the outermost method chain on it (>= 2 call segments, as shown above): wrap it in parentheses and put the head plus each .attr on its own +4 line. Bracket explosion is preferred — a chain is only broken when opening a bracket cannot fix the line — which keeps breaks minimal. Any brackets already opened inside a segment are re-indented to stay aligned under their now-deeper segment.

Contract

The tool only ever adds breaks to over-long lines; it never removes or rewrites existing breaks. This makes it idempotent, safe to re-run, and safe to combine with hand-formatting. Preserving existing line breaks is intentional, so it cannot be combined with ruff format, which would re-flow its output back into the staircase. Pair it with ruff-as-linter (E501 off) instead.

Multi-item-only is also a correctness guardrail: opening a single-item subscript would turn x[0] into x[0,] (== x[(0,)]), which changes meaning. Restricting to multi-item containers avoids that. It also avoids ugly single-element splits.

Known limitations

  • Lines long for non-bracket, non-method-chain reasons — ternaries, boolean/arithmetic chains, pure attribute chains (no calls), long string literals — are left untouched and reported, not fixed.
  • No "join" pass: it will not re-flow code that another tool has already split. It only acts on lines that are physically too long.
  • Comments inside brackets and pre-existing trailing commas are not specially handled.

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

kaapstorm_parsimony-0.1.1.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

kaapstorm_parsimony-0.1.1-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file kaapstorm_parsimony-0.1.1.tar.gz.

File metadata

  • Download URL: kaapstorm_parsimony-0.1.1.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for kaapstorm_parsimony-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b85bb54ed3f929b740b7b4a6e26edc5bfc71d555d94c0f4d48844e405759f46d
MD5 2aecf249c61fc206d691f224d43208a8
BLAKE2b-256 ae75193d97d8218d6fa2699ecb198b62186187d7462a1351f8d25866714c0336

See more details on using hashes here.

Provenance

The following attestation bundles were made for kaapstorm_parsimony-0.1.1.tar.gz:

Publisher: publish.yml on kaapstorm/parsimony

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kaapstorm_parsimony-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kaapstorm_parsimony-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ef681def74b124f927a90bd5cd5a04a2053dd9113e892a49254c01eba17f942
MD5 b220c0c9ea13cf4ff9d06855c827a0cf
BLAKE2b-256 50579a7f5087417b5a9f0e6b3441e3769a05e737bf03d10f6c7dd39045649eba

See more details on using hashes here.

Provenance

The following attestation bundles were made for kaapstorm_parsimony-0.1.1-py3-none-any.whl:

Publisher: publish.yml on kaapstorm/parsimony

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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