Skip to main content

Pre-commit hook to fix bare magic commands in Databricks .py-format notebooks

Project description

databricks-notebook-linter

A pre-commit hook that fixes bare magic commands in Databricks .py-format notebooks.

Problem

Databricks exports notebooks as .py files with special comment markers. Magic commands like %pip install and !nvidia-smi appear as bare lines, which are invalid Python syntax. This breaks linters (ruff, flake8) and type checkers (ty, mypy) that try to parse these files.

Solution

This tool prefixes bare magic commands with # MAGIC, converting them to Python comments that Databricks still recognizes and executes:

# Before
%pip install some-package==1.0.4

# After
# MAGIC %pip install some-package==1.0.4

It handles:

  • Single-line magic commands (%pip, %sql, %md, %sh, %fs, %run, %python, %r, %scala)
  • Shell bang commands (!nvidia-smi)
  • Multiline continuations (%pip install -U \)
  • Block-level magic -- if a %pip or ! command is inside an if, for, try, or other block, the entire block is prefixed
  • Nested blocks -- magic three levels deep prefixes all enclosing levels
  • Compound blocks -- if/elif/else, try/except/finally treated as single units
  • Mixed cells -- regular Python lines outside blocks are left untouched

The tool is idempotent -- running it twice produces the same result.

Usage

As a pre-commit hook

Add to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/yipitdata/databricks-notebook-linter
    rev: v0.1.0
    hooks:
      - id: fix-databricks-magic

By default the hook runs in check mode -- it reports unfixed magic commands and fails without modifying files. To auto-fix files on commit, pass --fix:

hooks:
  - id: fix-databricks-magic
    args: [--fix]

As a CLI tool

pip install databricks-notebook-linter

# Check mode (default): report issues, exit 1 if any found
fix-databricks-magic path/to/notebook.py

# Fix mode: rewrite files in place, exit 1 if any changed
fix-databricks-magic --fix path/to/notebook.py

Check mode output

notebook.py:5: bare magic command '%pip install foo' needs '# MAGIC' prefix
notebook.py:10: line in block containing magic needs '# MAGIC' prefix

How it works

  1. Checks if the file starts with # Databricks notebook source -- skips non-notebook files
  2. Splits the file into cells on # COMMAND ---------- boundaries
  3. For each cell, scans for bare magic lines (lines starting with %pip, !, etc.)
  4. If magic is at the top level, marks just that line (and any continuation lines)
  5. If magic is indented inside a block, walks backwards to find the top-level enclosing block and forwards to find the end of compound blocks (else, except, finally), then marks every line in the block
  6. Prefixes all marked lines with # MAGIC, preserving relative indentation for block-internal lines

Development

make setup    # install dependencies
make test     # run tests (with 100% branch coverage enforcement)
make lint     # run ruff
make format   # auto-format

Releasing

# All at once: tag, publish to PyPI, push
make release VERSION=x.y.z

# Or in two steps:
make tag-release VERSION=x.y.z    # bump, commit, tag (local only)
make push-release VERSION=x.y.z   # build, publish to PyPI, push commit + tag

tag-release validates a clean working tree on main, runs tests and lint, bumps the version in pyproject.toml and README.md, commits, and creates an annotated tag. push-release builds, publishes to PyPI, then pushes. Nothing reaches the remote until the PyPI publish succeeds.

License

MIT

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

databricks_notebook_linter-0.1.0.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

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

databricks_notebook_linter-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file databricks_notebook_linter-0.1.0.tar.gz.

File metadata

  • Download URL: databricks_notebook_linter-0.1.0.tar.gz
  • Upload date:
  • Size: 45.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for databricks_notebook_linter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f3ba7ef63e984d31377e16044fee962240d5f8fc6af0c772744f559bd4606ae
MD5 6fbcb9211ac136e66db680b63e0405a1
BLAKE2b-256 60f673a8880807efcd18c3c1c6d927e379254f5dcda766101e3624eb960b355e

See more details on using hashes here.

File details

Details for the file databricks_notebook_linter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: databricks_notebook_linter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for databricks_notebook_linter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d9a8415f32e2d61fa1dc397683f007b459f48c5706a19c4d426091cbdf1baa7
MD5 e323ef4529bfddb1b14b72d7e214774b
BLAKE2b-256 0dafc3030281d324b02f89a922eb13029ceee8a2a99fb3461d339369e8237a22

See more details on using hashes here.

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