Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mypy errors #2036

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion py/bin/sanitize_schema_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: # noqa: N802
type."""
if self.is_rootmodel_class(node):
# Filter out model_config assignments for RootModel classes
new_body = []
new_body: list[ast.stmt] = []
for item in node.body:
if isinstance(item, ast.Assign):
targets = item.targets
Expand Down
9 changes: 4 additions & 5 deletions py/packages/genkit/src/genkit/core/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import inspect
import json
from collections.abc import Callable
from enum import Enum
from typing import Any
from typing import Any, Callable

from genkit.core.tracing import tracer
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter
Expand Down Expand Up @@ -62,7 +61,7 @@ def __init__(
self,
kind: ActionKind,
name: str,
fn: Callable,
fn: Callable[..., Any],
description: str | None = None,
metadata: dict[ActionMetadataKey, Any] | None = None,
span_metadata: dict[str, str] | None = None,
Expand All @@ -81,7 +80,7 @@ def __init__(
self.kind: ActionKind = kind
self.name = name

def tracing_wrapper(*args, **kwargs):
def tracing_wrapper(*args: Any, **kwargs: Any) -> ActionResponse:
"""Wraps the callable function in a tracing span and adds metadata
to it."""

Expand Down Expand Up @@ -111,7 +110,7 @@ def tracing_wrapper(*args, **kwargs):
else:
span.set_attribute('genkit:output', json.dumps(output))

return ActionResponse(response=output, trace_id=trace_id)
return ActionResponse(response=output, traceId=trace_id)

self.fn = tracing_wrapper
self.description = description
Expand Down
5 changes: 4 additions & 1 deletion py/packages/genkit/src/genkit/core/action_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
def test_action_enum_behaves_like_str() -> None:
"""Ensure the ActionType behaves like a string and to ensure we're using the
correct variants."""
assert ActionKind.CHATLLM == 'chat-llm'
assert isinstance(ActionKind.CHATLLM, str)
"""Mypy treats enums as distinct types even when they inherit str.
Suppressed strict equality check for enums."""
assert ActionKind.CHATLLM == 'chat-llm' # type: ignore[comparison-overlap]
assert ActionKind.CUSTOM == 'custom'
assert ActionKind.EMBEDDER == 'embedder'
assert ActionKind.EVALUATOR == 'evaluator'
Expand Down
6 changes: 3 additions & 3 deletions py/packages/genkit/src/genkit/core/plugin_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from __future__ import annotations

import abc
import typing
from typing import TYPE_CHECKING, Any

from genkit.core.schema_types import GenerateRequest, GenerateResponse

if typing.TYPE_CHECKING:
if TYPE_CHECKING:
from genkit.veneer import Genkit


Expand Down Expand Up @@ -38,7 +38,7 @@ def attach_to_veneer(self, veneer: Genkit) -> None:
pass

def _add_model_to_veneer(
self, veneer: Genkit, name: str, metadata: dict | None = None
self, veneer: Genkit, name: str, metadata: dict[str, Any] | None = None
) -> None:
"""
Defines plugin's model in the Genkit Registry
Expand Down
9 changes: 8 additions & 1 deletion py/packages/genkit/src/genkit/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def lookup_action(self, kind: ActionKind, name: str) -> Action | None:
"""
if kind in self.actions and name in self.actions[kind]:
return self.actions[kind][name]
return None

def lookup_action_by_key(self, key: str) -> Action | None:
"""Lookup an action by its key.
Expand All @@ -60,5 +61,11 @@ def lookup_action_by_key(self, key: str) -> Action | None:
'Expected format: `<kind>/<name>`'
)
raise ValueError(msg)
kind, name = tokens
kind_str, name = tokens

try:
kind = ActionKind[kind_str]
except KeyError as err:
raise ValueError(f'Invalid action kind: `{kind_str}`') from err

return self.lookup_action(kind, name)