Install & Compatibility
Where this runs
tested against v5.2.0.20260518 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.386s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.288s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Mock
✓ from unittest.mock import Mock
✗ from types_mock import Mock
`types-mock` provides stubs for runtime libraries like `unittest.mock` (or the `mock` backport). You should import the runtime symbols directly from `unittest.mock` (or `mock`), not from `types_mock`.
patch
✓ from unittest.mock import patch
✗ from types_mock import patch
`types-mock` provides stubs for runtime libraries like `unittest.mock` (or the `mock` backport). You should import the runtime symbols directly from `unittest.mock` (or `mock`), not from `types_mock`.
This example demonstrates how `types-mock`, when used with `mypy`, provides type-checking for `unittest.mock.Mock` and `MagicMock` objects. By using `spec=MyService`, `mypy` ensures that the mock adheres to the `MyService` interface, catching potential type errors that might otherwise go unnoticed during testing setup.
import sys
from unittest.mock import Mock, MagicMock
# Define a simple service with type hints
class MyService:
def fetch_data(self, url: str) -> dict:
return {"status": "ok", "url": url, "data": "real content"}
def process_data(service: MyService, target_url: str) -> str:
data = service.fetch_data(target_url)
if data and data.get("status") == "ok":
return f"Successfully processed data from {data.get('url')}"
return "Failed to process data"
# --- Usage with types-mock (implicitly through mypy) ---
# 1. Mocking a method explicitly typed
mock_service_typed: MyService = Mock(spec=MyService) # Use spec for stricter type checking
mock_service_typed.fetch_data.return_value = {"status": "ok", "url": "mocked.com", "data": "mocked content"}
print(f"Typed Mock Result: {process_data(mock_service_typed, 'http://example.com')}")
# mypy would catch this if uncommented because 123 is not a string for url:
# print(process_data(mock_service_typed, 123))
# 2. MagicMock is also typed
magic_mock_example: MagicMock = MagicMock()
magic_mock_example.__len__.return_value = 5
print(f"MagicMock length: {len(magic_mock_example)}")
# To see types-mock in action, save this as `app.py`, then run:
# pip install mypy
# mypy app.py
Errors
Common errors & fixes
error: "Mock" has no attribute "call_args"
Mypy cannot infer the types of `unittest.mock.Mock` attributes like `call_args`, `assert_called_once_with`, or `return_value` without type stubs.
fixInstall the `types-mock` package (`pip install types-mock`) to provide `mypy` with the necessary type information for mock objects.
error: Untyped decorator makes function "..." untyped
Mypy struggles to infer the types of arguments injected by `unittest.mock.patch` decorators, leading to functions being marked as untyped.
fixExplicitly type the patched argument in the decorated function's signature (e.g., `def test_func(self, mock_obj: MagicMock)`).
error: "Mock" not callable
Mypy expects a callable object but encounters a `unittest.mock.Mock` instance that it cannot determine to be callable without proper type stubs.
fixEnsure `types-mock` is installed (`pip install types-mock`) to provide correct callable types for `Mock` objects, or explicitly cast the mock to a callable type (e.g., `mock_func: Callable[..., Any] = Mock()`).
error: "Mock" has no attribute "async_return_value"
Mypy cannot find type information for specific asynchronous attributes like `async_return_value` on `unittest.mock.Mock` objects without type stubs.
fixInstall `types-mock` (`pip install types-mock`) to provide `mypy` with comprehensive type stubs for all `Mock` attributes, including those for async mocking.
Upgrade
Version history
5.2.0.20260518latest on PyPI · released May 18, 2026
Audit
Dependencies
mockoptionalThis package provides type stubs for the 'mock' library (which is a backport of 'unittest.mock' for older Python versions). While 'types-mock' itself has no runtime dependencies, users typically install it to type-check their code that uses the 'mock' library or 'unittest.mock'.