skillfed

flake8-mock-spec

A linter that checks mocks are constructed with the spec argument

flake8-mock-spec v1.4.0 127.5K downloads/30d#11,739 on PyPI
Permissive license Apache 2.0 Abandoned released

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-spec

uv

uv add flake8-mock-spec

poetry

poetry add flake8-mock-spec

Installing 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

Environment :: ConsoleFramework :: Flake8Intended Audience :: DevelopersLicense :: OSI Approved :: Apache Software LicenseLicense :: Other/Proprietary LicenseProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Topic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Quality Assurance

Tags

flake8 mock linterunittest mock spec checkermock specification enforcementflake8 testing pluginmock object validationprevent mock misusespec argument linter
testingmock-validationflake8-plugin

More Python Modules packages