A pre-commit hook that automatically updates mypy's additional_dependencies in your .pre-commit-config.yaml with the appropriate type stubs based on your project dependencies.
- Automatic type stub detection: Checks PyPI for available type stubs (
types-*packages) for your dependencies - Multiple dependency sources: Supports both
pyproject.tomlandrequirements.txtfiles - Platform-specific dependencies: Handles conditional dependencies (e.g.,
pywin32==310; sys_platform == 'win32') - Preserves YAML formatting: Updates your pre-commit config while maintaining proper YAML structure
Add this to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/OpenTrafficCam/dependency-update-pre-commit
rev: v0.1.0
hooks:
- id: update-mypy-dependenciesThe hook automatically runs when you modify:
pyproject.tomlrequirements.txtrequirements-dev.txt
It will:
- Parse your project dependencies from
pyproject.toml(preferred) or requirements files - Check PyPI for available type stub packages
- Update the
additional_dependenciesin your mypy hook configuration
You can also run the hook manually:
pre-commit run update-mypy-dependencies --all-filesOr run the script directly:
python update_mypy_dependencies.pyThe hook reads dependencies from:
[project.dependencies]- Production dependencies[dependency-groups.dev]- Development dependencies (PEP 735 format)
Example:
[project]
dependencies = [
"requests==2.32.0",
"pandas==2.0.0",
]
[dependency-groups]
dev = [
"pytest==8.0.0",
]If pyproject.toml is not found, the hook reads from:
requirements.txtrequirements-dev.txt
- Dependency Parsing: Extracts package names and versions from your dependency files
- Type Stub Lookup: For each package, checks if a corresponding
types-{package}exists on PyPI - Config Update: Updates the
additional_dependencieslist in the mypy hook section of.pre-commit-config.yaml
Given these dependencies in pyproject.toml:
dependencies = [
"requests==2.32.0",
"pandas==2.0.0",
]The hook will update your .pre-commit-config.yaml:
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
additional_dependencies:
- pandas==2.0.0
- requests==2.32.0
- types-requests # Type stub found on PyPI- Python >= 3.11
- PyYAML >= 6.0
- requests >= 2.31.0
GPL-3.0-only
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues, please file a bug report at: https://github.com/OpenTrafficCam/dependency-update-pre-commit/issues