Skip to content

Commit b046470

Browse files
authored
chore(mypy): make python codebase strictly typed (#554)
* feat: use mypy strict mode and exclude tests * chore(mypy): make python codebase strictly typed * fix: fix unbound variable and already defined error * refactor: update all deprecated usage of `Type`
1 parent 764abe8 commit b046470

File tree

13 files changed

+143
-114
lines changed

13 files changed

+143
-114
lines changed

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ dev = ["ruff"]
3333

3434
[tool.mypy]
3535
python_version = "3.11"
36-
# Disable for not as we have missing type annotations. See https://github.com/cocoindex-io/cocoindex/issues/539
37-
# strict = true
36+
strict = true
37+
files = "python/cocoindex"
38+
exclude = "python/cocoindex/tests"

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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from rich.console import Console
1111
from rich.panel import Panel
1212
from rich.table import Table
13+
from typing import Any
1314

1415
from . import flow, lib, setting
1516
from .setup import sync_setup, drop_setup, flow_names_with_setup, apply_setup_changes
@@ -127,7 +128,7 @@ def _load_user_app(app_target: str) -> types.ModuleType:
127128
default=None,
128129
show_default=False,
129130
)
130-
def cli(env_file: str | None):
131+
def cli(env_file: str | None) -> None:
131132
"""
132133
CLI for Cocoindex.
133134
"""
@@ -147,7 +148,7 @@ def cli(env_file: str | None):
147148

148149
@cli.command()
149150
@click.argument("app_target", type=str, required=False)
150-
def ls(app_target: str | None):
151+
def ls(app_target: str | None) -> None:
151152
"""
152153
List all flows.
153154
@@ -199,7 +200,7 @@ def ls(app_target: str | None):
199200
"--color/--no-color", default=True, help="Enable or disable colored output."
200201
)
201202
@click.option("--verbose", is_flag=True, help="Show verbose output with full details.")
202-
def show(app_flow_specifier: str, color: bool, verbose: bool):
203+
def show(app_flow_specifier: str, color: bool, verbose: bool) -> None:
203204
"""
204205
Show the flow spec and schema.
205206
@@ -236,7 +237,7 @@ def show(app_flow_specifier: str, color: bool, verbose: bool):
236237

237238
@cli.command()
238239
@click.argument("app_target", type=str)
239-
def setup(app_target: str):
240+
def setup(app_target: str) -> None:
240241
"""
241242
Check and apply backend setup changes for flows, including the internal and target storage
242243
(to export).
@@ -274,7 +275,7 @@ def setup(app_target: str):
274275
"even if not defined in the current process."
275276
"If used, APP_TARGET and any listed flow names are ignored.",
276277
)
277-
def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool):
278+
def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool) -> None:
278279
"""
279280
Drop the backend setup for flows.
280281
@@ -355,7 +356,7 @@ def drop(app_target: str | None, flow_name: tuple[str, ...], drop_all: bool):
355356
default=False,
356357
help="Avoid printing anything to the standard output, e.g. statistics.",
357358
)
358-
def update(app_flow_specifier: str, live: bool, quiet: bool):
359+
def update(app_flow_specifier: str, live: bool, quiet: bool) -> Any:
359360
"""
360361
Update the index to reflect the latest data from data sources.
361362
@@ -390,7 +391,9 @@ def update(app_flow_specifier: str, live: bool, quiet: bool):
390391
default=True,
391392
help="Use already-cached intermediate data if available.",
392393
)
393-
def evaluate(app_flow_specifier: str, output_dir: str | None, cache: bool = True):
394+
def evaluate(
395+
app_flow_specifier: str, output_dir: str | None, cache: bool = True
396+
) -> None:
394397
"""
395398
Evaluate the flow and dump flow outputs to files.
396399
@@ -473,7 +476,7 @@ def server(
473476
cors_origin: str | None,
474477
cors_cocoindex: bool,
475478
cors_local: int | None,
476-
):
479+
) -> None:
477480
"""
478481
Start a HTTP server providing REST APIs.
479482

python/cocoindex/convert.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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: Any,
4545
) -> Callable[[Any], Any]:
4646
"""
4747
Make a decoder from an engine value to a Python value.
@@ -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)