or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async-mocking.mdcore-mocking.mdindex.mdpatching.mdutilities.md
tile.json

tessl/pypi-types-mock

Type stubs for the mock library providing comprehensive type annotations for Mock, MagicMock, patch decorators, and other testing utilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/types-mock@5.2

To install, run

npx @tessl/cli install tessl/pypi-types-mock@5.2.0

index.mddocs/

Types Mock

Types Mock provides comprehensive type stubs for Python's mock testing framework, enabling full static type checking support for all mock testing utilities. It includes type annotations for Mock and MagicMock classes, the patch decorator system, AsyncMock for testing asynchronous code, and various testing utilities with complete IDE integration and type safety.

Package Information

  • Package Name: types-mock
  • Language: Python
  • Installation: pip install types-mock
  • Upstream: mock library

Core Imports

import mock

Common for specific utilities:

from mock import Mock, MagicMock, patch, AsyncMock
from mock import create_autospec, mock_open, PropertyMock
from mock import sentinel, DEFAULT, ANY, call

Basic Usage

from mock import Mock, MagicMock, patch, AsyncMock

# Basic Mock usage
mock_obj = Mock()
mock_obj.method.return_value = "test"
result = mock_obj.method()
mock_obj.method.assert_called_once()

# MagicMock with magic methods
magic_mock = MagicMock()
magic_mock.__len__.return_value = 5
assert len(magic_mock) == 5

# Patching for dependency injection
@patch('module.function')
def test_function(mock_func):
    mock_func.return_value = "mocked"
    # Test code here

# Async mocking for testing coroutines
async_mock = AsyncMock()
async_mock.return_value = "async result"
result = await async_mock()
async_mock.assert_awaited_once()

Architecture

The mock framework provides a hierarchical structure of mock objects:

  • NonCallableMock: Base mock class for non-callable objects with assertion methods
  • Mock: Callable mock combining NonCallableMock with CallableMixin
  • MagicMock: Mock with automatic magic method support for special operations
  • AsyncMock: Specialized mock for testing asynchronous code with await assertions
  • Patching System: Decorators and context managers for temporary object replacement
  • Call Tracking: Comprehensive call recording and assertion capabilities

Capabilities

Core Mocking

Essential mock classes for creating test doubles, including basic Mock objects, MagicMock with automatic magic method support, and NonCallableMock for non-callable objects. These provide comprehensive assertion methods and call tracking.

class Mock:
    def __init__(self, spec=None, side_effect=None, return_value=..., wraps=None, name=None, spec_set=None, **kwargs) -> None: ...
    def assert_called_with(self, *args, **kwargs) -> None: ...
    def assert_called_once_with(self, *args, **kwargs) -> None: ...
    def assert_not_called(self) -> None: ...

class MagicMock(Mock):
    def mock_add_spec(self, spec, spec_set: bool = False) -> None: ...

class NonCallableMock:
    def __init__(self, spec=None, wraps=None, name=None, spec_set=None, **kwargs) -> None: ...
    def reset_mock(self, visited=None, *, return_value: bool = False, side_effect: bool = False) -> None: ...

Core Mocking

Async Mocking

Specialized mock class for testing asynchronous code with await-specific assertions and async-aware call tracking. Provides comprehensive support for testing coroutines, async context managers, and async iterators.

class AsyncMock(Mock):
    def assert_awaited(self) -> None: ...
    def assert_awaited_once(self) -> None: ...
    def assert_awaited_with(self, *args, **kwargs) -> None: ...
    def assert_awaited_once_with(self, *args, **kwargs) -> None: ...
    def assert_not_awaited(self) -> None: ...
    await_count: int
    await_args: call | None

Async Mocking

Patching System

Comprehensive patching decorators and context managers for temporarily replacing objects during testing. Supports object attribute patching, multiple patching, and dictionary patching with automatic cleanup.

def patch(target, new=..., spec=None, create: bool = False, spec_set=None, autospec=None, new_callable=None, **kwargs): ...
def patch.object(target, attribute: str, new=..., spec=None, create: bool = False, **kwargs): ...
def patch.multiple(target, spec=None, create: bool = False, **kwargs): ...
patch.stopall() -> None

Patching System

Utilities

Helper functions and specialized mock classes for common testing scenarios, including automatic spec creation, file mocking, property mocking, and mock sealing to prevent accidental attribute access.

def create_autospec(spec, spec_set: bool = False, instance: bool = False, **kwargs): ...
def mock_open(mock=None, read_data: str = "") -> Mock: ...
def seal(mock) -> None: ...

class PropertyMock(Mock):
    def __get__(self, obj, obj_type=None): ...
    def __set__(self, obj, value) -> None: ...

Utilities

Constants and Sentinels

# Version information
__version__: str  # Package version string
version_info: tuple[int, int, int]  # Version tuple

# Special matching objects
ANY: object  # Matches any value in assertions
DEFAULT: object  # Default return value marker
sentinel: object  # Factory for creating unique sentinel objects

# Call tracking
call: object  # Factory for creating call objects for assertions

# Configuration
FILTER_DIR: bool  # Controls directory filtering behavior

Type Definitions

# Call tracking types
class call:
    args: tuple[Any, ...]
    kwargs: dict[str, Any]
    def call_list(self) -> list[call]: ...

# Exception types  
class InvalidSpecError(Exception): ...