Skip to main content
Ctrl K

python-mock-precommit-hook

precommit hook to prevent monkeypatching in Python

1
contributor

Cite this software

Description

python-mock-hooks

DOI Research Software Directory Badge

A pre-commit / prek hook that restricts mocking in Python tests:

  • Blocks:
    • unittest.mock imports and references.
    • mocker.patch and mocker.patch.* usage from pytest-mock
    • All monkeypatch pytest methods except those permitted below.
  • Permits:
    • monkeypatch.chdir
    • monkeypatch.setenv
    • monkeypatch.delenv
    • monkeypatch.undo
    • mocker.Mock, mocker.spy, and other pytest-mock APIs besides patch.

For HTTP tests, prefer recording requests with pytest-recording.

Check failures explain the policy to developers and LLM coding agents.

Ruff does not support custom lint plugins, so this hook adds these testing policies as a separate check alongside Ruff.

Usage

Requires Python 3.11 or newer. Dependencies are installed automatically. Choose one configuration format, replacing the revision with a release tag or commit SHA.

For .pre-commit-config.yaml (pre-commit or prek):

repos:
  - repo: https://github.com/i-VRESSE/python-mock-hooks
    rev: v0.2.0
    hooks:
      - id: python-mock-hooks

For prek.toml (prek):

[[repos]]
repo = "https://github.com/i-VRESSE/python-mock-hooks"
rev = "v0.2.0"
hooks = [{ id = "python-mock-hooks" }]

Run prek run python-mock-hooks --all-files (or use pre-commit instead of prek).

Checks Python files under tests/ by default. For another layout, set files on the hook:

  • YAML: files: ^(tests|test)/.*\.py$
  • TOML: files = '^(tests|test)/.*\.py$'

Contributing

See CONTRIBUTING.md for development and implementation details.

Acknowledgments

Rule and rule tests adapted from protein-quest. Inspired by PFCCLab/ast-grep-pre-commit-mirror.

License

Apache-2.0.

Keywords
No keywords available
License
</>Source code

Participating organisations

Netherlands eScience Center

Contributors

Related projects

Protein Detective

A software for AI-driven identification of unknown proteins in cryo-EM density maps

Updated 10 months ago
In progress