Creating a pre commit rules environment

Got it ๐Ÿ‘ Here’s the same setup written in plain text only (no code block formatting), so you can copy it all at once:




Step 1: Install pre-commit

Run in terminal:

pip install pre-commit


Step 2: Create a file named .pre-commit-config.yaml in your project root and put this inside:


repos:


  • repo: https://github.com/psf/black
  • rev: 24.3.0
  • hooks:
  • id: black


  • repo: https://github.com/pycqa/isort
  • rev: 5.13.2
  • hooks:
  • id: isort


  • repo: https://github.com/pycqa/flake8
  • rev: 6.1.0
  • hooks:
  • id: flake8


  • repo: https://github.com/pre-commit/mirrors-mypy
  • rev: v1.10.0
  • hooks:
  • id: mypy


  • repo: https://github.com/PyCQA/bandit
  • rev: 1.7.9
  • hooks:
  • id: bandit




Step 3: Install the hooks

Run:

pre-commit install


Step 4: Run hooks on all files (first time setup)

Run:

pre-commit run –all-files




Now, every time you do git commit, the following will run automatically:


  • Black → formats your code
  • isort → organizes imports
  • Flake8 → checks style issues
  • Mypy → checks type hints
  • Bandit → scans for security issues



From Blogger iPhone client