Skip to content

nbqa

GitHub stars GitHub release (latest SemVer) GitHub last commit GitHub commit activity GitHub contributors

nbqa + mypy provides static type checking for Jupyter notebooks.

nbqa (NoteBook QA) runs standard Python linters on Jupyter notebooks by extracting code cells and running the linter on them. Combined with mypy, it brings static type checking to notebook development.

Key features:

  • Type Checking in Notebooks: Catch type errors in notebook cells before execution
  • Cell-Aware Errors: Error messages reference correct cell and line numbers
  • Mypy Integration: Uses your existing mypy configuration
  • Supports Complex Types: Handles generics, protocols, type inference, etc.
  • Gradual Typing: Add type hints incrementally to notebooks
  • IDE Integration: Works with Jupyter Lab, VS Code, and other notebook editors

Common use cases: - Type-check data science notebooks before production deployment - Catch type errors in ML model training code - Ensure type consistency across notebook projects - Validate notebook code in CI/CD pipelines

Note: This linter only checks .ipynb files. Use PYTHON_MYPY for .py and .pyi files.

nbqa documentation

nbQA - GitHub

Configuration in MegaLinter

Variable Description Default value
PYTHON_NBQA_MYPY_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
PYTHON_NBQA_MYPY_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
Ex: -s --foo "bar"
PYTHON_NBQA_MYPY_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
PYTHON_NBQA_MYPY_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
PYTHON_NBQA_MYPY_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
list_of_files
PYTHON_NBQA_MYPY_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".ipynb"]
PYTHON_NBQA_MYPY_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
PYTHON_NBQA_MYPY_PRE_COMMANDS List of bash commands to run before the linter None
PYTHON_NBQA_MYPY_POST_COMMANDS List of bash commands to run after the linter None
PYTHON_NBQA_MYPY_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling PYTHON_NBQA_MYPY and its pre/post commands None
PYTHON_NBQA_MYPY_DISABLE_ERRORS Run linter but consider errors as warnings false
PYTHON_NBQA_MYPY_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
PYTHON_NBQA_MYPY_CLI_EXECUTABLE Override CLI executable ['nbqa']

MegaLinter Flavors

This linter is available in the following flavors

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 134 Docker Image Size (tag) Docker Pulls
cupcake MegaLinter for the most commonly used languages 91 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 68 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .ipynb

How the linting is performed

  • nbqa is called once with the list of files as arguments (list_of_files CLI lint mode)

Example calls

nbqa mypy myfile.ipynb
nbqa mypy --config-file .mypy.ini myfile.ipynb

Help content

usage: nbqa <code quality tool> <notebook or directory> <nbqa options> <code quality tool arguments>

Please specify:
- 1) a code quality tool (e.g. `black`, `pyupgrade`, `flake`, ...)
- 2) some notebooks (or, if supported by the tool, directories)
- 3) (optional) flags for nbqa (e.g. `--nbqa-diff`, `--nbqa-shell`)
- 4) (optional) flags for code quality tool (e.g. `--line-length` for `black`)

Examples:
    nbqa flake8 notebook.ipynb
    nbqa black notebook.ipynb --line-length=96
    nbqa pyupgrade notebook_1.ipynb notebook_2.ipynb

See https://nbqa.readthedocs.io/en/latest/index.html for more details on how to run `nbqa`.

Run any standard Python code-quality tool on a Jupyter notebook.

positional arguments:
  command               Command to run, e.g. `flake8`.
  root_dirs             Notebooks or directories to run command on.

options:
  -h, --help            show this help message and exit
  --nbqa-files NBQA_FILES
                        Global file include pattern.
  --nbqa-exclude NBQA_EXCLUDE
                        Global file exclude pattern.
  --nbqa-diff           Show diff which would result from running tool.
  --nbqa-shell          Run `command` directly rather than `python -m command`
  --nbqa-process-cells NBQA_PROCESS_CELLS
                        Process code within these cell magics. You can pass
                        multiple options, e.g. `nbqa black my_notebook.ipynb
                        --nbqa-process-cells add_to,write_to` by placing
                        commas between them.
  --version             show program's version number and exit
  --nbqa-dont-skip-bad-cells
                        Don't skip cells with invalid syntax.
  --nbqa-skip-celltags NBQA_SKIP_CELLTAGS
                        Skip cells with have any of the given celltags.
  --nbqa-md             Process markdown cells, rather than Python ones.

Installation on mega-linter Docker image

  • Dockerfile commands :
# renovate: datasource=pypi depName=nbqa
ARG PIP_NBQA_VERSION=1.9.1
# renovate: datasource=pypi depName=mypy
ARG PIP_MYPY_VERSION=1.19.1
ENV MYPY_CACHE_DIR=/tmp