Skip to content

Commit 0e8d454

Browse files
committed
feat(custom-source): python SDK types
1 parent 1a14087 commit 0e8d454

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

python/cocoindex/op.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
Protocol,
1313
dataclass_transform,
1414
Annotated,
15+
TypeVar,
16+
Generic,
17+
Literal,
1518
get_args,
1619
)
1720

@@ -421,6 +424,53 @@ def _inner(fn: Callable[..., Any]) -> Callable[..., Any]:
421424
return _inner
422425

423426

427+
########################################################
428+
# Custom source connector
429+
########################################################
430+
431+
432+
@dataclasses.dataclass
433+
class SourceReadOptions:
434+
include_ordinal: bool = False
435+
include_content_version_fp: bool = False
436+
include_value: bool = False
437+
438+
439+
T = TypeVar("T")
440+
441+
NON_EXISTENCE = "NON_EXISTENCE"
442+
443+
444+
@dataclasses.dataclass
445+
class SourceRowData(Generic[T]):
446+
"""
447+
The data of a source row.
448+
449+
- value: The value of the source row.
450+
- ordinal: The ordinal of the source row.
451+
- content_version_fp: The content version fingerprint of the source row.
452+
"""
453+
454+
value: T | Literal["NON_EXISTENCE"] | None
455+
ordinal: int | None = None
456+
content_version_fp: bytes | None = None
457+
458+
459+
# Users will define a source spec class first (e.g. CustomSource), then provide a source connector like this:
460+
#
461+
# @cocoindex.op.source_connector(spec_cls=CustomSource, key_type=KeyType, value_type=ValueType)
462+
# class CustomSourceConnector:
463+
# @staticmethod
464+
# async def create(spec: CustomSource) -> Self:
465+
# ...
466+
#
467+
# async def list(self, options: SourceReadOptions) -> AsyncIterator[tuple[KeyType, SourceRowData[ValueType]]]:
468+
# ...
469+
#
470+
# async def get_value(self, key: KeyType, options: SourceReadOptions) -> SourceRowData[ValueType]:
471+
# ...
472+
473+
424474
########################################################
425475
# Custom target connector
426476
########################################################

0 commit comments

Comments
 (0)