-
Notifications
You must be signed in to change notification settings - Fork 88
Description
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 inzip()calls - prevents length mismatches B018: Useless expressions (e.g.,df_longon 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:
- Ensure example code follows best practices
- Avoid teaching patterns that could lead to bugs
- 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.ipynbdocs/source/notebooks/did_pymc_banks.ipynbdocs/source/notebooks/inv_prop_latent.ipynbdocs/source/notebooks/its_lift_test.ipynbdocs/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.