Skip to content

Commit 4a91178

Browse files
batmany13claude
andauthored
feat: add ManagedBy StrEnum for managed_by parameter (#1807)
* feat: add ManagedBy StrEnum to replace Literal type for managed_by Define a common ManagedBy StrEnum in connectorkits/target.py and use it across all 6 target connectors instead of scattered Literal["system", "user"] annotations. This gives a single source of truth, better IDE support, and clearer semantics. Also fixes pre-existing mypy errors in the SQLite target test by adding a type annotation and using the new enum. Closes #1806 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: import ManagedBy from target module directly instead of statediff Since ManagedBy lives in connectorkits/target.py, connectors should import it from there (target.ManagedBy) rather than going through statediff. This makes the provenance clear. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 328e2ad commit 4a91178

9 files changed

Lines changed: 51 additions & 36 deletions

File tree

python/cocoindex/connectorkits/statediff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class TrackingRecordTransition(_Generic[_TrackingRecordT], _NamedTuple):
9292
prev_may_be_missing: bool
9393

9494

95-
ManagedBy = _Literal["system", "user"]
95+
from cocoindex.connectorkits.target import ManagedBy as ManagedBy
9696

9797

9898
@_unpickle_safe
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Shared types for target connectors."""
2+
3+
from __future__ import annotations
4+
5+
__all__ = ["ManagedBy"]
6+
7+
import enum as _enum
8+
9+
10+
class ManagedBy(_enum.StrEnum):
11+
"""Who manages the lifecycle of the target resource."""
12+
13+
SYSTEM = "system"
14+
USER = "user"

python/cocoindex/connectors/doris/_target.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from typing_extensions import TypeVar
4747

4848
import cocoindex as coco
49-
from cocoindex.connectorkits import statediff
49+
from cocoindex.connectorkits import statediff, target
5050
from cocoindex.connectorkits.fingerprint import fingerprint_object
5151
from cocoindex._internal.datatype import (
5252
AnyType,
@@ -942,7 +942,7 @@ class _TableKey(NamedTuple):
942942
@dataclass
943943
class _TableSpec:
944944
table_schema: TableSchema[Any]
945-
managed_by: Literal["system", "user"] = "system"
945+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
946946
vector_indexes: list[VectorIndexDef] | None = None
947947
inverted_indexes: list[InvertedIndexDef] | None = None
948948

@@ -1191,7 +1191,7 @@ def table_target(
11911191
table_name: str,
11921192
table_schema: TableSchema[RowT],
11931193
*,
1194-
managed_by: Literal["system", "user"] = "system",
1194+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
11951195
vector_indexes: list[VectorIndexDef] | None = None,
11961196
inverted_indexes: list[InvertedIndexDef] | None = None,
11971197
) -> coco.TargetState[_RowHandler]:
@@ -1210,7 +1210,7 @@ def declare_table_target(
12101210
table_name: str,
12111211
table_schema: TableSchema[RowT],
12121212
*,
1213-
managed_by: Literal["system", "user"] = "system",
1213+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12141214
vector_indexes: list[VectorIndexDef] | None = None,
12151215
inverted_indexes: list[InvertedIndexDef] | None = None,
12161216
) -> "DorisTableTarget[RowT, coco.PendingS]":
@@ -1232,7 +1232,7 @@ async def mount_table_target(
12321232
table_name: str,
12331233
table_schema: TableSchema[RowT],
12341234
*,
1235-
managed_by: Literal["system", "user"] = "system",
1235+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12361236
vector_indexes: list[VectorIndexDef] | None = None,
12371237
inverted_indexes: list[InvertedIndexDef] | None = None,
12381238
) -> "DorisTableTarget[RowT]":

python/cocoindex/connectors/lancedb/_target.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import numpy as np
3838

3939
import cocoindex as coco
40-
from cocoindex.connectorkits import statediff
40+
from cocoindex.connectorkits import statediff, target
4141
from cocoindex.connectorkits.fingerprint import fingerprint_object
4242
from cocoindex._internal.datatype import (
4343
AnyType,
@@ -489,7 +489,7 @@ class _TableSpec:
489489
"""Specification for a LanceDB table."""
490490

491491
table_schema: TableSchema[Any]
492-
managed_by: Literal["system", "user"] = "system"
492+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
493493

494494

495495
@unpickle_safe
@@ -793,7 +793,7 @@ def table_target(
793793
table_name: str,
794794
table_schema: TableSchema[RowT],
795795
*,
796-
managed_by: Literal["system", "user"] = "system",
796+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
797797
) -> coco.TargetState[_RowHandler]:
798798
"""
799799
Create a TargetState for a LanceDB table target.
@@ -823,7 +823,7 @@ def declare_table_target(
823823
table_name: str,
824824
table_schema: TableSchema[RowT],
825825
*,
826-
managed_by: Literal["system", "user"] = "system",
826+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
827827
) -> TableTarget[RowT, coco.PendingS]:
828828
"""
829829
Create a TableTarget for writing rows to a LanceDB table.
@@ -849,7 +849,7 @@ async def mount_table_target(
849849
table_name: str,
850850
table_schema: TableSchema[RowT],
851851
*,
852-
managed_by: Literal["system", "user"] = "system",
852+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
853853
) -> TableTarget[RowT]:
854854
"""
855855
Mount a table target and return a ready-to-use TableTarget.

python/cocoindex/connectors/postgres/_target.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import numpy as np
4040

4141
import cocoindex as coco
42-
from cocoindex.connectorkits import statediff
42+
from cocoindex.connectorkits import statediff, target
4343
from cocoindex.connectorkits.fingerprint import fingerprint_object
4444
from cocoindex._internal.datatype import (
4545
AnyType,
@@ -783,7 +783,7 @@ class _TableSpec:
783783
"""Specification for a PostgreSQL table."""
784784

785785
table_schema: TableSchema[Any]
786-
managed_by: Literal["system", "user"] = "system"
786+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
787787

788788

789789
@unpickle_safe
@@ -1257,7 +1257,7 @@ def table_target(
12571257
table_schema: TableSchema[RowT],
12581258
*,
12591259
pg_schema_name: str | None = None,
1260-
managed_by: Literal["system", "user"] = "system",
1260+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12611261
) -> coco.TargetState[_RowHandler]:
12621262
"""
12631263
Create a TargetState for a PostgreSQL table target.
@@ -1293,7 +1293,7 @@ def declare_table_target(
12931293
table_schema: TableSchema[RowT],
12941294
*,
12951295
pg_schema_name: str | None = None,
1296-
managed_by: Literal["system", "user"] = "system",
1296+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12971297
) -> TableTarget[RowT, coco.PendingS]:
12981298
"""
12991299
Create a TableTarget for writing rows to a PostgreSQL table.
@@ -1327,7 +1327,7 @@ async def mount_table_target(
13271327
table_schema: TableSchema[RowT],
13281328
*,
13291329
pg_schema_name: str | None = None,
1330-
managed_by: Literal["system", "user"] = "system",
1330+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13311331
) -> TableTarget[RowT]:
13321332
"""
13331333
Mount a table target and return a ready-to-use TableTarget.

python/cocoindex/connectors/qdrant/_target.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
) from e
3333

3434
import cocoindex as coco
35-
from cocoindex.connectorkits import statediff
35+
from cocoindex.connectorkits import statediff, target
3636
from cocoindex.connectorkits.fingerprint import fingerprint_object
3737
from cocoindex._internal.datatype import TypeChecker
3838
from cocoindex._internal.serde import unpickle_safe
@@ -271,7 +271,7 @@ class _CollectionKey(NamedTuple):
271271
@dataclass
272272
class _CollectionSpec:
273273
schema: CollectionSchema
274-
managed_by: Literal["system", "user"] = "system"
274+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
275275

276276

277277
@unpickle_safe
@@ -511,7 +511,7 @@ def collection_target(
511511
collection_name: str,
512512
schema: CollectionSchema,
513513
*,
514-
managed_by: Literal["system", "user"] = "system",
514+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
515515
) -> "coco.TargetState[_PointHandler]":
516516
"""
517517
Create a TargetState for a Qdrant collection target.
@@ -538,7 +538,7 @@ def declare_collection_target(
538538
collection_name: str,
539539
schema: CollectionSchema,
540540
*,
541-
managed_by: Literal["system", "user"] = "system",
541+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
542542
) -> "CollectionTarget[coco.PendingS]":
543543
"""Declare a Qdrant collection target.
544544
@@ -562,7 +562,7 @@ async def mount_collection_target(
562562
collection_name: str,
563563
schema: CollectionSchema,
564564
*,
565-
managed_by: Literal["system", "user"] = "system",
565+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
566566
) -> "CollectionTarget[coco.ResolvedS]":
567567
"""
568568
Mount a collection target and return a ready-to-use CollectionTarget.

python/cocoindex/connectors/sqlite/_target.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import numpy as np
3535

3636
import cocoindex as coco
37-
from cocoindex.connectorkits import statediff
37+
from cocoindex.connectorkits import statediff, target
3838
from cocoindex.connectorkits.fingerprint import fingerprint_object
3939
from cocoindex._internal.rwlock import RWLock
4040
from cocoindex._internal.serde import unpickle_safe
@@ -586,7 +586,7 @@ class _TableSpec:
586586
"""Specification for a SQLite table."""
587587

588588
table_schema: TableSchema[Any]
589-
managed_by: Literal["system", "user"] = "system"
589+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
590590
virtual_table_def: Vec0TableDef | None = None
591591

592592

@@ -1114,7 +1114,7 @@ def table_target(
11141114
table_name: str,
11151115
table_schema: TableSchema[RowT],
11161116
*,
1117-
managed_by: Literal["system", "user"] = "system",
1117+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
11181118
virtual_table_def: Vec0TableDef | None = None,
11191119
) -> "coco.TargetState[_RowHandler]":
11201120
"""
@@ -1151,7 +1151,7 @@ def declare_table_target(
11511151
table_name: str,
11521152
table_schema: TableSchema[RowT],
11531153
*,
1154-
managed_by: Literal["system", "user"] = "system",
1154+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
11551155
virtual_table_def: Vec0TableDef | None = None,
11561156
) -> "TableTarget[RowT, coco.PendingS]":
11571157
"""
@@ -1198,7 +1198,7 @@ async def mount_table_target(
11981198
table_name: str,
11991199
table_schema: TableSchema[RowT],
12001200
*,
1201-
managed_by: Literal["system", "user"] = "system",
1201+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12021202
virtual_table_def: Vec0TableDef | None = None,
12031203
) -> "TableTarget[RowT]":
12041204
"""

python/cocoindex/connectors/surrealdb/_target.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import numpy as np
4848

4949
import cocoindex as coco
50-
from cocoindex.connectorkits import statediff
50+
from cocoindex.connectorkits import statediff, target
5151
from cocoindex.connectorkits.fingerprint import fingerprint_object
5252
from cocoindex._internal.datatype import (
5353
AnyType,
@@ -754,7 +754,7 @@ class _TableSpec:
754754
is_relation: bool
755755
from_tables: tuple[str, ...] | None # sorted table names for FROM clause
756756
to_tables: tuple[str, ...] | None # sorted table names for TO clause
757-
managed_by: Literal["system", "user"] = "system"
757+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM
758758

759759

760760
@dataclass(frozen=True, slots=True)
@@ -1284,7 +1284,7 @@ def table_target(
12841284
table_name: str,
12851285
table_schema: TableSchema[RowT] | None = None,
12861286
*,
1287-
managed_by: Literal["system", "user"] = "system",
1287+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
12881288
) -> coco.TargetState[_RecordHandler]:
12891289
"""Create a TargetState for a SurrealDB table."""
12901290
_validate_identifier(table_name, "table name")
@@ -1304,7 +1304,7 @@ def declare_table_target(
13041304
table_name: str,
13051305
table_schema: TableSchema[RowT] | None = None,
13061306
*,
1307-
managed_by: Literal["system", "user"] = "system",
1307+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13081308
) -> TableTarget[RowT, coco.PendingS]:
13091309
"""Declare a table target and return a pending TableTarget."""
13101310
provider = coco.declare_target_state_with_child(
@@ -1318,7 +1318,7 @@ async def mount_table_target(
13181318
table_name: str,
13191319
table_schema: TableSchema[RowT] | None = None,
13201320
*,
1321-
managed_by: Literal["system", "user"] = "system",
1321+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13221322
) -> TableTarget[RowT]:
13231323
"""Mount a table target and return a ready-to-use TableTarget."""
13241324
provider = await coco.mount_target(
@@ -1334,7 +1334,7 @@ def relation_target(
13341334
to_table: TableTarget[Any] | Collection[TableTarget[Any]],
13351335
table_schema: TableSchema[RowT] | None = None,
13361336
*,
1337-
managed_by: Literal["system", "user"] = "system",
1337+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13381338
) -> coco.TargetState[_RecordHandler]:
13391339
"""Create a TargetState for a SurrealDB relation table."""
13401340
_validate_identifier(table_name, "relation table name")
@@ -1362,7 +1362,7 @@ def declare_relation_target(
13621362
to_table: TableTarget[Any] | Collection[TableTarget[Any]],
13631363
table_schema: TableSchema[RowT] | None = None,
13641364
*,
1365-
managed_by: Literal["system", "user"] = "system",
1365+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13661366
) -> RelationTarget[RowT, coco.PendingS]:
13671367
"""Declare a relation target and return a pending RelationTarget."""
13681368
from_names = _normalize_table_names(from_table)
@@ -1387,7 +1387,7 @@ async def mount_relation_target(
13871387
to_table: TableTarget[Any] | Collection[TableTarget[Any]],
13881388
table_schema: TableSchema[RowT] | None = None,
13891389
*,
1390-
managed_by: Literal["system", "user"] = "system",
1390+
managed_by: target.ManagedBy = target.ManagedBy.SYSTEM,
13911391
) -> RelationTarget[RowT]:
13921392
"""Mount a relation target and return a ready-to-use RelationTarget."""
13931393
from_names = _normalize_table_names(from_table)

python/tests/connectors/test_sqlite_target.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import cocoindex as coco
1717
from cocoindex._internal.context_keys import ContextProvider
18+
from cocoindex.connectorkits import target
1819
from cocoindex.connectors import sqlite
1920
from cocoindex.resources.schema import VectorSchema
2021

@@ -528,13 +529,13 @@ def test_user_managed_table(sqlite_db: tuple[sqlite.ManagedConnection, Path]) ->
528529
test_env = make_test_env(managed_conn, "test_user_managed_table")
529530

530531
async def declare_user_managed_rows() -> None:
531-
table = await coco.use_mount(
532+
table: sqlite.TableTarget[SimpleRow] = await coco.use_mount(
532533
coco.component_subpath("setup", "table"),
533534
sqlite.declare_table_target,
534535
SQLITE_DB,
535536
"user_managed",
536537
await sqlite.TableSchema.from_class(SimpleRow, primary_key=["id"]),
537-
managed_by="user",
538+
managed_by=target.ManagedBy.USER,
538539
)
539540

540541
for row in user_rows:

0 commit comments

Comments
 (0)