flake8-mock-spec
raw JSON → 1.4.0 verified Sat May 09 auth: no python
A flake8 plugin that ensures mocks are constructed with the `spec` argument, preventing fragile tests. Version 1.4.0 supports Python 3.8+ and requires flake8. It has a release cadence of roughly once a year.
pip install flake8-mock-spec Common errors
error W8001 mock with no spec ↓
cause The plugin flags any call to Mock(), MagicMock(), or NonCallableMock() without the spec argument.
fix
Add spec=YourClass to the mock constructor, e.g., Mock(spec=MyClass).
Warnings
gotcha The plugin only flags mock.Mock, mock.MagicMock, and mock.NonCallableMock calls without a spec argument. It does not check patched objects (e.g., patch('module.ClassName')) or other mock library variants. ↓
fix Ensure you also manually check patches; use spec parameter in patch calls where appropriate.
deprecated As of version 1.4.0, the plugin is compatible with Python 3.8+ and flake8 4+; older Python versions (3.6, 3.7) are not supported. ↓
fix Upgrade to the latest version and ensure Python >=3.8.1.
Imports
- flake8_mock_spec
from flake8_mock_spec import *
Quickstart
# Install the plugin
# pip install flake8-mock-spec
# Then run flake8 on your code:
# flake8 myproject/
# Example of a lint violation (W8001):
mock = Mock() # W8001: mock with no spec
# Correct way:
mock = Mock(spec=SomeClass)