Registry / type-stubs / types-mock

types-mock

JSON →
library5.2.0.20260518pypypi✓ verified 29d ago

This package provides high-quality type stubs for the `mock` library (and by extension, `unittest.mock` from the standard library), enabling static type checkers like `mypy` to understand and validate usage of mock objects. Part of the `typeshed` project, it is actively maintained with a continuous release cadence reflecting updates to the stubs. The current version is 5.2.0.20260408.

pip install types-mock
INSTALL
IMPORT
SIG · TYPES-MOCK
T
types-mock
type-stubspythonv5.2.0.20260518
Install
1.5s avg
Import
337ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.386s · 17.8MB
glibc
py 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
Debug
Known issues
gotchaDo not import symbols directly from `types_mock`. This package provides type stubs for static analysis (e.g., by `mypy`), not runtime code. You should import `Mock`, `patch`, etc., from `unittest.mock` (or the `mock` backport library).
fix
Always import runtime mock objects from `from unittest.mock import ...` or `from mock import ...`.
affects: All versions
gotcha`types-mock` is solely for static type checking; installing it has no effect on your program's runtime behavior. It's a development dependency, typically used with tools like `mypy`.
fix
Understand that `types-mock` enhances developer productivity through static analysis, but doesn't alter how your code runs. Ensure `mypy` or another type checker is configured to use the installed stubs.
affects: All versions
gotchaThere are two main 'mock' libraries: the built-in `unittest.mock` (Python 3.3+) and the standalone `mock` backport for older Python versions. `types-mock` provides stubs for both, but ensure you are consistent in which library you are using in your project's runtime code.
fix
For Python 3.3 and newer, prefer `unittest.mock`. For older Python versions, install `pip install mock` and use `import mock`. Ensure your type checker configuration points to the correct stubs if there's any ambiguity (though typeshed usually handles this automatically).
affects: All versions
gotchaThe version number of `types-mock` (e.g., `5.2.0.20260408`) does not correspond to a specific version of the `mock` runtime library. Instead, it reflects the version of the stubs within the `typeshed` project and the date they were published.
fix
Don't try to map `types-mock` versions directly to `mock` or `unittest.mock` versions. Refer to the typeshed changelog or `mock` library documentation for specific API changes.
affects: All versions
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.
fix
Install 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.
fix
Explicitly 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.
fix
Ensure `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.
fix
Install `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'.
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources
types-mock — pip install types-mock · libregistry