flake8-mock-spec
A linter that checks mocks are constructed with the spec argument
What it is and what it does
flake8-mock-spec is a linter plugin for flake8 that detects when unittest.mock objects (Mock, MagicMock, NonCallableMock, AsyncMock) are created without a spec or spec_set argument, and when patch operations lack spec-related arguments. It enforces a best practice: mocks should declare what interface they conform to, preventing tests from passing when they access methods or attributes that don't exist on the real object being mocked.
The plugin defines seven linting rules (TMS010–TMS013 for mock construction, TMS020–TMS022 for patch operations) that can be selectively suppressed. It integrates directly into your flake8 workflow with no additional configuration required beyond installation. However, the package has been abandoned since its single release in January 2023, meaning no updates or maintenance will occur.
Use it for:
- Catch bugs in unit tests where mocks are called with methods that don't exist on the real object, preventing false test passes.
- Enforce team standards that all mocks must declare their spec to keep test code aligned with actual interfaces.
- Validate patch operations in test suites to ensure they include spec or autospec arguments for safer mocking.
- Integrate mock validation into CI/CD pipelines via flake8 to fail builds when mocks are created without spec constraints.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
A flake8 plugin that enforces the use of the spec or spec_set argument when creating mock objects, helping catch bugs from incorrectly specified mocks.
Yes, if you are already using flake8 and want to catch mock misuse in your test suite. The plugin is lightweight, has no complex dependencies, and solves a real problem. However, be aware that it is abandoned and will not receive updates or bug fixes—if you encounter issues with newer versions of flake8 or Python, you will need to maintain or fork it yourself.
Install
flake8-mock-spec on PyPI
pip
pip install flake8-mock-specuv
uv add flake8-mock-specpoetry
poetry add flake8-mock-specInstalling flake8-mock-spec
Before you install
Low friction installation as a pure Python wheel with only flake8 as a runtime dependency. However, the package is abandoned—last release was 2023-01-14 with no updates since, so expect no maintenance or bug fixes going forward.
License in practice
Licensed under Apache 2.0 (permissive), allowing free use, modification, and distribution with minimal restrictions. No licensing friction for most projects.
Quickstart
pip install flake8-mock-spec
# Then run flake8 on your test files:
flake8 test_source.py
# The plugin will warn on mocks without spec:
# test_source.py:5:22: TMS010 unittest.mock.Mock instances should be constructed with the spec or spec_set argument
Verify before relying
- Compatibility with flake8 versions released after 2023-01-14 is unverified given the package's abandoned status.
- Whether the plugin works correctly with modern unittest.mock patterns or async testing frameworks beyond what the description covers.
Package facts
| License | Apache 2.0 (permissive) |
| Python support | supports the current Python release (>=3.8.1,<4.0.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — flake8 |
| Maintenance | abandoned — 1,308 days since the last release |
| First released | |
| Downloads | 127,524/month — #11,739 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: flake8_mock_spec-1.4.0-py3-none-any.whl
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
hackingHacking is a set of flake8 plugins that enforce…
permissive · top 15,000 on PyPI
flake8Flake8 is a command-line tool that combines…
permissive · top 1,000 on PyPI
mockmock provides a backport of Python's standard…
permissive · top 1,000 on PyPI
dlintDlint is a flake8 plugin that adds…
permissive · top 15,000 on PyPI
flexmockflexmock creates mock, stub, spy, and fake…
permissive · top 15,000 on PyPI
flake8-annotationsA Flake8 plugin that enforces PEP 3107-style…
permissive · top 15,000 on PyPI
flake8-formatter-junit-xmlFormats flake8 linting output as JUnit XML,…
permissive · top 15,000 on PyPI
calleeProvides argument matchers for unittest.mock…
permissive · top 15,000 on PyPI
flake8-banditIntegrates bandit security checks into flake8…
permissive · top 15,000 on PyPI
flake8-tidy-importsA flake8 plugin that detects and reports…
permissive · top 15,000 on PyPI