Skip to content

Commit 3204df1

Browse files
committed
chore(mypy): make python codebase strictly typed
1 parent 9f081e0 commit 3204df1

File tree

12 files changed

+139
-111
lines changed

12 files changed

+139
-111
lines changed

python/cocoindex/auth_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ def add_auth_entry(key: str, value: T) -> AuthEntryReference[T]:
2424
return AuthEntryReference(key)
2525

2626

27-
def ref_auth_entry(key: str) -> AuthEntryReference:
27+
def ref_auth_entry(key: str) -> AuthEntryReference[T]:
2828
"""Reference an auth entry by its key."""
2929
return AuthEntryReference(key)

python/cocoindex/cli.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from dotenv import load_dotenv, find_dotenv
1010
from rich.console import Console
1111
from rich.table import Table
12+
from typing import Any
1213

13-
from . import flow, lib, setting, query
14+
from . import flow, lib, setting
1415
from .setup import sync_setup, drop_setup, flow_names_with_setup, apply_setup_changes
1516

1617
# Create ServerSettings lazily upon first call, as environment variables may be loaded from files, etc.
@@ -126,7 +127,7 @@ def _load_user_app(app_target: str) -> types.ModuleType:
126127
default=None,
127128
show_default=False,
128129
)
129-
def cli(env_file: str | None):
130+
def cli(env_file: str | None) -> None:
130131
"""
131132
CLI for Cocoindex.
132133
"""
@@ -146,7 +147,7 @@ def cli(env_file: str | None):
146147

147148
@cli.command()
148149
@click.argument("app_target", type=str, required=False)
149-
def ls(app_target: str | None):
150+
def ls(app_target: str | None) -> None:
150151
"""
151152
List all flows.
152153
@@ -198,7 +199,7 @@ def ls(app_target: str | None):
198199
"--color/--no-color", default=True, help="Enable or disable colored output."
199200
)
200201
@click.option("--verbose", is_flag=True, help="Show verbose output with full details.")
201-
def show(app_flow_specifier: str, color: bool, verbose: bool):
202+
def show(app_flow_specifier: str, color: bool, verbose: bool) -> None:
202203
"""
203204
Show the flow spec and schema.
204205
@@ -235,7 +236,7 @@ def show(app_flow_specifier: str, color: bool, verbose: bool):
235236

236237
@cli.command()
237238
@click.argument("app_target", type=str)
238-
def setup(app_target: str):
239+
def setup(app_target: str) -> None:
239240
"""
240241
Check and apply backend setup changes for flows, including the internal and target storage
241242
(to export).
@@ -273,7 +274,7 @@ def setup(app_target: str):
273274
"even if not defined in the current process."
274275
"If used, APP_TARGET and any listed flow names are ignored.",
275276
)
276-
def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool):
277+
def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool) -> None:
277278
"""
278279
Drop the backend setup for flows.
279280
@@ -354,7 +355,7 @@ def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool):
354355
default=False,
355356
help="Avoid printing anything to the standard output, e.g. statistics.",
356357
)
357-
def update(app_flow_specifier: str, live: bool, quiet: bool):
358+
def update(app_flow_specifier: str, live: bool, quiet: bool) -> Any:
358359
"""
359360
Update the index to reflect the latest data from data sources.
360361
@@ -389,7 +390,9 @@ def update(app_flow_specifier: str, live: bool, quiet: bool):
389390
default=True,
390391
help="Use already-cached intermediate data if available.",
391392
)
392-
def evaluate(app_flow_specifier: str, output_dir: str | None, cache: bool = True):
393+
def evaluate(
394+
app_flow_specifier: str, output_dir: str | None, cache: bool = True
395+
) -> None:
393396
"""
394397
Evaluate the flow and dump flow outputs to files.
395398
@@ -472,7 +475,7 @@ def server(
472475
cors_origin: str | None,
473476
cors_cocoindex: bool,
474477
cors_local: int | None,
475-
):
478+
) -> None:
476479
"""
477480
Start a HTTP server providing REST APIs.
478481

python/cocoindex/convert.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import uuid
99

1010
from enum import Enum
11-
from typing import Any, Callable, get_origin, Mapping
11+
from typing import Any, Callable, get_origin, Mapping, Type
1212
from .typing import (
1313
analyze_type_info,
1414
encode_enriched_type,
@@ -41,7 +41,7 @@ def encode_engine_value(value: Any) -> Any:
4141
def make_engine_value_decoder(
4242
field_path: list[str],
4343
src_type: dict[str, Any],
44-
dst_annotation,
44+
dst_annotation: Type[Any] | None,
4545
) -> Callable[[Any], Any]:
4646
"""
4747
Make a decoder from an engine value to a Python value.
@@ -60,7 +60,7 @@ def make_engine_value_decoder(
6060
if (
6161
dst_annotation is None
6262
or dst_annotation is inspect.Parameter.empty
63-
or dst_annotation is Any
63+
or get_origin(dst_annotation) is Any
6464
):
6565
if src_type_kind == "Struct" or src_type_kind in TABLE_TYPES:
6666
raise ValueError(
@@ -102,7 +102,7 @@ def make_engine_value_decoder(
102102
field_path, engine_fields_schema[1:], elem_type_info.struct_type
103103
)
104104

105-
def decode(value):
105+
def decode(value: Any) -> Any | None:
106106
if value is None:
107107
return None
108108
return {key_decoder(v[0]): value_decoder(v[1:]) for v in value}
@@ -111,7 +111,7 @@ def decode(value):
111111
field_path, engine_fields_schema, elem_type_info.struct_type
112112
)
113113

114-
def decode(value):
114+
def decode(value: Any) -> Any | None:
115115
if value is None:
116116
return None
117117
return [elem_decoder(v) for v in value]
@@ -129,7 +129,7 @@ def _make_engine_struct_value_decoder(
129129
field_path: list[str],
130130
src_fields: list[dict[str, Any]],
131131
dst_struct_type: type,
132-
) -> Callable[[list], Any]:
132+
) -> Callable[[list[Any]], Any]:
133133
"""Make a decoder from an engine field values to a Python value."""
134134

135135
src_name_to_idx = {f["name"]: i for i, f in enumerate(src_fields)}
@@ -156,7 +156,7 @@ def _make_engine_struct_value_decoder(
156156

157157
def make_closure_for_value(
158158
name: str, param: inspect.Parameter
159-
) -> Callable[[list], Any]:
159+
) -> Callable[[list[Any]], Any]:
160160
src_idx = src_name_to_idx.get(name)
161161
if src_idx is not None:
162162
field_path.append(f".{name}")

0 commit comments

Comments
 (0)