Coverage for src / lexigram / contracts / data / sql / mapper.py: 0%
13 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-15 18:57 +0800
1"""Read-only mapper protocol for data transformation contracts.
3Defines the contract for mapping between domain/storage models without
4mutating either side.
5"""
7from __future__ import annotations
9from typing import Generic, Protocol, TypeVar, runtime_checkable
11S = TypeVar("S")
12T = TypeVar("T")
14__all__ = ["DataMapperProtocol", "ReadOnlyMapperProtocol", "S", "T"]
17@runtime_checkable
18class ReadOnlyMapperProtocol(Protocol, Generic[S, T]):
19 """Protocol for a stateless, read-only mapper between two types.
21 Implementations translate a source model of type ``S`` to a target
22 model of type ``T`` without modifying either. The batch variant
23 is provided for efficiency when mapping large collections.
24 """
26 def to_target(self, source: S) -> T:
27 """Map a single source instance to the target type.
29 Args:
30 source: Source model instance to translate.
32 Returns:
33 Corresponding target model instance.
34 """
35 ...
37 def to_target_batch(self, sources: list[S]) -> list[T]:
38 """Map a sequence of source instances to the target type.
40 Args:
41 sources: Sequence of source model instances to translate.
43 Returns:
44 List of corresponding target model instances in the same order.
45 """
46 ...
49@runtime_checkable
50class DataMapperProtocol(ReadOnlyMapperProtocol[S, T], Protocol, Generic[S, T]):
51 """Protocol for a bidirectional stateless mapper between two types.
53 Extends :class:`ReadOnlyMapperProtocol` with the ability to map from
54 the target type back to the source type, enabling round-trip
55 transformations (e.g. domain model ↔ storage row).
56 """
58 def to_source(self, target: T) -> S:
59 """Map a single target instance back to the source type.
61 Args:
62 target: Target model instance to translate.
64 Returns:
65 Corresponding source model instance.
66 """
67 ...
69 def to_source_batch(self, targets: list[T]) -> list[S]:
70 """Map a sequence of target instances back to the source type.
72 Args:
73 targets: Sequence of target model instances to translate.
75 Returns:
76 List of corresponding source model instances in the same order.
77 """
78 ...