scieee AI-readable full text Open interactive document viewer

Pre-commit to better code

Dasgupta, Abhishek

Abstract

In this walkthrough, I will walk through using pre-commit to make your code better. Pre-commit, along with automated linters and type checkers, enforces guidelines on every commit, from stylistic issues such as whitespaces and indents, to automatically checking your code for common errors, such as missing imports or incorrect types. In collaborative projects, pre-commit can be invaluable in ensuring that the codebase is of a uniform quality.A recording of this session is available on YouTube: https://youtu.be/u1ZCX9BqrXA

Full text

Pre-commit to better code Abhishek Dasgupta RSECon25, Warwick https://github.com/abhidg/pre-commit-to-better-code Pre-commit to better code Why pre-commit? Workflow without pre-commit • Write code • Commit • git push • CI/CD, e.g. GitHub Actions ◦ Linting (flake8, ruff, lintr) ◦ Type checking (mypy) ◦ Unit and integration tests xkcd.com/1296 What if we could check for (and correct) these common issues even before committing? pre-commit • Defined rules • Execution on every git commit • Checks: suceeds or fails • Fixes: changes source files before committing • Rules are like filters, by applying them you can sift out and alter code before hitting version control Setup 1. Install pre-commit ◦ Use your package manager ◦ Or pip install pre-commit 2. Navigate to the git repo 3. pre-commit sample-config > .pre-commitconfig.yaml 4. pre-commit install 5. pre-commit will now check your code with the default config .pre-commit-config.yaml # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v3.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files • A repo is a collection of hooks , specified by id . Some hooks can take parameters • Once pre-commit is setup, it will activate on every git commit call Example for Python repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.6.5 hooks: # ruff performs linting - id: ruff args: [ --fix ] # ruff-format performs autoformatting - id: ruff-format Initial pre-commit run $ pre-commit run --all-files [INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... [INFO] Installing environment for https://github.com/astral-sh/ruff-pre-commit. [INFO] Once installed this environment will be reused. [INFO] This may take a few minutes... check yaml...............................................................Passed fix end of files.........................................................Passed trim trailing whitespace.................................................Passed ruff..................................................................... Failed - hook id: ruff - exit code: 1 docs/tutorial/zonal_stats.ipynb:cell 3:9:1: E402 Module level import not at top of cell | 7 | here = Path.cwd().parent / "data" 8 | 9 | from geoglue.memoryraster import MemoryRaster | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E402 10 | 11 | # Here we load data from a shapefile, but we can also use the | Found 1 error. ruff-format..............................................................Passed