Skip to content

Apply stricter ruff linting rules to documentation notebooks #574

@drbenvincent

Description

@drbenvincent

Background

We recently added stricter ruff linting rules (#571) to the codebase (B, F, UP, C4, SIM) to catch potential bugs and enforce modern Python patterns. However, documentation notebooks in docs/ are currently excluded from these checks.

Current State

The .pre-commit-config.yaml file has this exclusion:

- id: ruff
  types_or: [ python, pyi, jupyter ]
  args: [ --fix ]
  # Exclude docs/ to avoid applying strict linting rules to example notebooks
  # Remove this exclusion if you want to enforce strict rules on documentation
  exclude: ^docs/

Issues Found in Notebooks

When running ruff check docs/ the following issues are detected:

  • B006: Mutable default arguments (e.g., priors={}) - can cause subtle bugs
  • B905: Missing strict= parameter in zip() calls - prevents length mismatches
  • B018: Useless expressions (e.g., df_long on its own line)
  • Other minor style improvements

NOTE:

We will want to suppress B018 when applied to notebooks. Can do with this change to pyproject.toml:

[tool.ruff.lint.per-file-ignores]
# Allow useless expressions in notebooks (used to display dataframes/outputs)
"*.ipynb" = ["B018"]

Proposed Action

Fix the linting issues in documentation notebooks to:

  1. Ensure example code follows best practices
  2. Avoid teaching patterns that could lead to bugs
  3. Maintain consistency between source code and documentation

Implementation

Run the following to auto-fix most issues:

ruff check --fix --unsafe-fixes docs/

Then manually review and fix any remaining issues. Once complete, remove the exclude: ^docs/ line from .pre-commit-config.yaml.

Affected Files

Based on the last check, approximately 12 issues across these notebooks:

  • docs/source/knowledgebase/structural_causal_models.ipynb
  • docs/source/notebooks/did_pymc_banks.ipynb
  • docs/source/notebooks/inv_prop_latent.ipynb
  • docs/source/notebooks/its_lift_test.ipynb
  • docs/source/notebooks/iv_weak_instruments.ipynb

Priority

Low - This is a code quality improvement for documentation. The examples work correctly, but would benefit from following stricter best practices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    devopsDevOps relateddocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions