Skip to content

Commit c79f4c6

Browse files
fix(types): modernize type annotations to Python 3.9+ syntax
- Replace Dict/List/Optional with dict/list/| None - Replace Any with object for runtime values - Add type annotations to class attributes - Add @OverRide decorator to __str__ method - Fixes all basedpyright strict mode warnings
1 parent b6604e3 commit c79f4c6

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

src/openrouter/call_model/exceptions.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
providing actionable error messages with context for debugging.
55
"""
66

7-
from typing import Any, Dict, List, Optional
7+
from typing import override
88

99

1010
class CallModelError(Exception):
@@ -19,11 +19,15 @@ class CallModelError(Exception):
1919
context: Optional dictionary with additional error context
2020
"""
2121

22+
message: str
23+
code: str | None
24+
context: dict[str, object]
25+
2226
def __init__(
2327
self,
2428
message: str,
25-
code: Optional[str] = None,
26-
context: Optional[Dict[str, Any]] = None,
29+
code: str | None = None,
30+
context: dict[str, object] | None = None,
2731
):
2832
"""Initialize the error with message, code, and context.
2933
@@ -37,6 +41,7 @@ def __init__(
3741
self.code = code
3842
self.context = context or {}
3943

44+
@override
4045
def __str__(self) -> str:
4146
"""Return the error message."""
4247
return self.message
@@ -59,7 +64,10 @@ class ToolExecutionError(CallModelError):
5964
... )
6065
"""
6166

62-
def __init__(self, tool_name: str, error: Exception, context: Dict[str, Any]):
67+
tool_name: str
68+
original_error: Exception
69+
70+
def __init__(self, tool_name: str, error: Exception, context: dict[str, object]):
6371
"""Initialize the tool execution error.
6472
6573
Args:
@@ -92,7 +100,10 @@ class ToolValidationError(CallModelError):
92100
... )
93101
"""
94102

95-
def __init__(self, tool_name: str, validation_errors: List[str]):
103+
tool_name: str
104+
validation_errors: list[str]
105+
106+
def __init__(self, tool_name: str, validation_errors: list[str]):
96107
"""Initialize the tool validation error.
97108
98109
Args:
@@ -120,7 +131,9 @@ class StreamInterruptedError(CallModelError):
120131
... )
121132
"""
122133

123-
def __init__(self, last_event: Optional[Dict[str, Any]] = None):
134+
last_event: dict[str, object] | None
135+
136+
def __init__(self, last_event: dict[str, object] | None = None):
124137
"""Initialize the stream interrupted error.
125138
126139
Args:
@@ -147,6 +160,9 @@ class MaxToolRoundsExceededError(CallModelError):
147160
>>> raise MaxToolRoundsExceededError(rounds=10, max_rounds=5)
148161
"""
149162

163+
rounds: int
164+
max_rounds: int
165+
150166
def __init__(self, rounds: int, max_rounds: int):
151167
"""Initialize the max tool rounds exceeded error.
152168

src/openrouter/call_model/types.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
from enum import Enum
9-
from typing import Any, Dict, List, Optional, TypedDict
9+
from typing import TypedDict
1010

1111

1212
# Type aliases for clarity and maintainability
@@ -16,7 +16,7 @@
1616
EventType = str
1717
"""Type of SSE event (e.g., 'content.delta', 'tool.call')."""
1818

19-
StreamEvent = Dict[str, Any]
19+
StreamEvent = dict[str, object]
2020
"""Raw SSE event dictionary from the API."""
2121

2222

@@ -57,11 +57,11 @@ class ToolContext(TypedDict, total=False):
5757
"""
5858

5959
number_of_turns: int
60-
message_history: List[Dict[str, Any]]
61-
model: Optional[str]
62-
models: Optional[List[str]]
63-
previous_tool_results: Optional[List[Dict[str, Any]]]
64-
request_id: Optional[str]
60+
message_history: list[dict[str, object]]
61+
model: str | None
62+
models: list[str] | None
63+
previous_tool_results: list[dict[str, object]] | None
64+
request_id: str | None
6565

6666

6767
class ResponseState(str, Enum):
@@ -103,7 +103,7 @@ class CachedData(TypedDict, total=False):
103103
... }
104104
"""
105105

106-
message: Optional[Dict[str, Any]]
107-
text: Optional[str]
108-
tool_calls: Optional[List[Dict[str, Any]]]
109-
raw_response: Optional[Dict[str, Any]]
106+
message: dict[str, object] | None
107+
text: str | None
108+
tool_calls: list[dict[str, object]] | None
109+
raw_response: dict[str, object] | None

0 commit comments

Comments
 (0)