Skip to content

Commit 7ec5be4

Browse files
authored
chore!: drop Python 3.9 and adopt X|Y typing syntax (#427)
* start * chore: drop Python 3.9 and adopt X|Y typing syntax * rm optional
1 parent 33c0680 commit 7ec5be4

20 files changed

Lines changed: 126 additions & 132 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ that includes it. Once it reaches the end of its lifespan, the experiment will b
8181
| `QueryExpander` | Query Expansion Component | 0.14.3 |
8282
| `MultiQueryEmbeddingRetriever` | MultiQueryEmbeddingRetriever | 0.14.3 |
8383
| `MultiQueryTextRetriever` | MultiQueryTextRetriever | 0.14.3 |
84-
| EmbeddingBasedDocumentSplitter | Document Splitting | 0.15.2 |
84+
| `EmbeddingBasedDocumentSplitter` | Document Splitting | 0.15.2 |
8585

8686
### Discontinued experiments
8787

haystack_experimental/chat_message_stores/in_memory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from dataclasses import replace
6-
from typing import Any, Iterable, Optional
6+
from typing import Any, Iterable
77

88
from haystack import default_from_dict, default_to_dict
99
from haystack.dataclasses import ChatMessage, ChatRole
@@ -42,7 +42,7 @@ class InMemoryChatMessageStore:
4242
```
4343
"""
4444

45-
def __init__(self, skip_system_messages: bool = True, last_k: Optional[int] = 10) -> None:
45+
def __init__(self, skip_system_messages: bool = True, last_k: int | None = 10) -> None:
4646
"""
4747
Create an InMemoryChatMessageStore.
4848
@@ -135,7 +135,7 @@ def write_messages(self, chat_history_id: str, messages: list[ChatMessage]) -> i
135135

136136
return len(messages_to_write)
137137

138-
def retrieve_messages(self, chat_history_id: str, last_k: Optional[int] = None) -> list[ChatMessage]:
138+
def retrieve_messages(self, chat_history_id: str, last_k: int | None = None) -> list[ChatMessage]:
139139
"""
140140
Retrieves all stored chat messages.
141141

haystack_experimental/chat_message_stores/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from typing import Any, Optional, Protocol
5+
from typing import Any, Protocol
66

77
from haystack.dataclasses import ChatMessage
88

@@ -74,7 +74,7 @@ def delete_all_messages(self) -> None:
7474
"""
7575
...
7676

77-
def retrieve_messages(self, chat_history_id: str, last_k: Optional[int] = None) -> list[ChatMessage]:
77+
def retrieve_messages(self, chat_history_id: str, last_k: int | None = None) -> list[ChatMessage]:
7878
"""
7979
Retrieves chat messages from the ChatMessageStore.
8080

haystack_experimental/components/agents/agent.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import inspect
99
from dataclasses import dataclass
10-
from typing import Any, Optional, Union
10+
from typing import Any
1111

1212
# Monkey patch Haystack's AgentSnapshot with our extended version
1313
import haystack.dataclasses.breakpoints as hdb
@@ -77,8 +77,8 @@ class _ExecutionContext(Haystack_ExecutionContext):
7777
parameter in their `run()` and `run_async()` methods.
7878
"""
7979

80-
tool_execution_decisions: Optional[list[ToolExecutionDecision]] = None
81-
confirmation_strategy_context: Optional[dict[str, Any]] = None
80+
tool_execution_decisions: list[ToolExecutionDecision] | None = None
81+
confirmation_strategy_context: dict[str, Any] | None = None
8282

8383

8484
class Agent(HaystackAgent):
@@ -136,16 +136,16 @@ def __init__(
136136
self,
137137
*,
138138
chat_generator: ChatGenerator,
139-
tools: Optional[ToolsType] = None,
140-
system_prompt: Optional[str] = None,
141-
exit_conditions: Optional[list[str]] = None,
142-
state_schema: Optional[dict[str, Any]] = None,
139+
tools: ToolsType | None = None,
140+
system_prompt: str | None = None,
141+
exit_conditions: list[str] | None = None,
142+
state_schema: dict[str, Any] | None = None,
143143
max_agent_steps: int = 100,
144-
streaming_callback: Optional[StreamingCallbackT] = None,
144+
streaming_callback: StreamingCallbackT | None = None,
145145
raise_on_tool_invocation_failure: bool = False,
146-
confirmation_strategies: Optional[dict[str, ConfirmationStrategy]] = None,
147-
tool_invoker_kwargs: Optional[dict[str, Any]] = None,
148-
chat_message_store: Optional[ChatMessageStore] = None,
146+
confirmation_strategies: dict[str, ConfirmationStrategy] | None = None,
147+
tool_invoker_kwargs: dict[str, Any] | None = None,
148+
chat_message_store: ChatMessageStore | None = None,
149149
) -> None:
150150
"""
151151
Initialize the agent component.
@@ -190,14 +190,14 @@ def __init__(
190190
def _initialize_fresh_execution(
191191
self,
192192
messages: list[ChatMessage],
193-
streaming_callback: Optional[StreamingCallbackT],
193+
streaming_callback: StreamingCallbackT | None,
194194
requires_async: bool,
195195
*,
196-
system_prompt: Optional[str] = None,
197-
generation_kwargs: Optional[dict[str, Any]] = None,
198-
tools: Optional[Union[ToolsType, list[str]]] = None,
199-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
200-
chat_message_store_kwargs: Optional[dict[str, Any]] = None,
196+
system_prompt: str | None = None,
197+
generation_kwargs: dict[str, Any] | None = None,
198+
tools: ToolsType | list[str] | None = None,
199+
confirmation_strategy_context: dict[str, Any] | None = None,
200+
chat_message_store_kwargs: dict[str, Any] | None = None,
201201
**kwargs: dict[str, Any],
202202
) -> _ExecutionContext:
203203
"""
@@ -264,12 +264,12 @@ def _initialize_fresh_execution(
264264
def _initialize_from_snapshot( # type: ignore[override]
265265
self,
266266
snapshot: AgentSnapshot,
267-
streaming_callback: Optional[StreamingCallbackT],
267+
streaming_callback: StreamingCallbackT | None,
268268
requires_async: bool,
269269
*,
270-
generation_kwargs: Optional[dict[str, Any]] = None,
271-
tools: Optional[Union[ToolsType, list[str]]] = None,
272-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
270+
generation_kwargs: dict[str, Any] | None = None,
271+
tools: ToolsType | list[str] | None = None,
272+
confirmation_strategy_context: dict[str, Any] | None = None,
273273
) -> _ExecutionContext:
274274
"""
275275
Initialize execution context from an AgentSnapshot.
@@ -320,15 +320,15 @@ def _initialize_from_snapshot( # type: ignore[override]
320320
def run( # type: ignore[override] # noqa: PLR0915 PLR0912
321321
self,
322322
messages: list[ChatMessage],
323-
streaming_callback: Optional[StreamingCallbackT] = None,
323+
streaming_callback: StreamingCallbackT | None = None,
324324
*,
325-
generation_kwargs: Optional[dict[str, Any]] = None,
326-
break_point: Optional[AgentBreakpoint] = None,
327-
snapshot: Optional[AgentSnapshot] = None,
328-
system_prompt: Optional[str] = None,
329-
tools: Optional[Union[ToolsType, list[str]]] = None,
330-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
331-
chat_message_store_kwargs: Optional[dict[str, Any]] = None,
325+
generation_kwargs: dict[str, Any] | None = None,
326+
break_point: AgentBreakpoint | None = None,
327+
snapshot: AgentSnapshot | None = None,
328+
system_prompt: str | None = None,
329+
tools: ToolsType | list[str] | None = None,
330+
confirmation_strategy_context: dict[str, Any] | None = None,
331+
chat_message_store_kwargs: dict[str, Any] | None = None,
332332
**kwargs: Any,
333333
) -> dict[str, Any]:
334334
"""
@@ -558,15 +558,15 @@ def run( # type: ignore[override] # noqa: PLR0915 PLR0912
558558
async def run_async( # type: ignore[override] # noqa: PLR0915
559559
self,
560560
messages: list[ChatMessage],
561-
streaming_callback: Optional[StreamingCallbackT] = None,
561+
streaming_callback: StreamingCallbackT | None = None,
562562
*,
563-
generation_kwargs: Optional[dict[str, Any]] = None,
564-
break_point: Optional[AgentBreakpoint] = None,
565-
snapshot: Optional[AgentSnapshot] = None,
566-
system_prompt: Optional[str] = None,
567-
tools: Optional[Union[ToolsType, list[str]]] = None,
568-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
569-
chat_message_store_kwargs: Optional[dict[str, Any]] = None,
563+
generation_kwargs: dict[str, Any] | None = None,
564+
break_point: AgentBreakpoint | None = None,
565+
snapshot: AgentSnapshot | None = None,
566+
system_prompt: str | None = None,
567+
tools: ToolsType | list[str] | None = None,
568+
confirmation_strategy_context: dict[str, Any] | None = None,
569+
chat_message_store_kwargs: dict[str, Any] | None = None,
570570
**kwargs: Any,
571571
) -> dict[str, Any]:
572572
"""

haystack_experimental/components/agents/human_in_the_loop/dataclasses.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from dataclasses import asdict, dataclass
6-
from typing import Any, Optional
6+
from typing import Any
77

88

99
@dataclass
@@ -23,8 +23,8 @@ class ConfirmationUIResult:
2323
"""
2424

2525
action: str # "confirm", "reject", "modify"
26-
feedback: Optional[str] = None
27-
new_tool_params: Optional[dict[str, Any]] = None
26+
feedback: str | None = None
27+
new_tool_params: dict[str, Any] | None = None
2828

2929

3030
@dataclass
@@ -49,9 +49,9 @@ class ToolExecutionDecision:
4949

5050
tool_name: str
5151
execute: bool
52-
tool_call_id: Optional[str] = None
53-
feedback: Optional[str] = None
54-
final_tool_params: Optional[dict[str, Any]] = None
52+
tool_call_id: str | None = None
53+
feedback: str | None = None
54+
final_tool_params: dict[str, Any] | None = None
5555

5656
def to_dict(self) -> dict[str, Any]:
5757
"""

haystack_experimental/components/agents/human_in_the_loop/errors.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from typing import Optional
6-
75

86
class HITLBreakpointException(Exception):
97
"""
108
Exception raised when a tool execution is paused by a ConfirmationStrategy (e.g. BreakpointConfirmationStrategy).
119
"""
1210

13-
def __init__(
14-
self, message: str, tool_name: str, snapshot_file_path: str, tool_call_id: Optional[str] = None
15-
) -> None:
11+
def __init__(self, message: str, tool_name: str, snapshot_file_path: str, tool_call_id: str | None = None) -> None:
1612
"""
1713
Initialize the HITLBreakpointException.
1814

haystack_experimental/components/agents/human_in_the_loop/strategies.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from dataclasses import replace
6-
from typing import TYPE_CHECKING, Any, Optional
6+
from typing import TYPE_CHECKING, Any
77

88
from haystack.components.agents.state import State
99
from haystack.components.tools.tool_invoker import ToolInvoker
@@ -52,8 +52,8 @@ def run(
5252
tool_name: str,
5353
tool_description: str,
5454
tool_params: dict[str, Any],
55-
tool_call_id: Optional[str] = None,
56-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
55+
tool_call_id: str | None = None,
56+
confirmation_strategy_context: dict[str, Any] | None = None,
5757
) -> ToolExecutionDecision:
5858
"""
5959
Run the human-in-the-loop strategy for a given tool and its parameters.
@@ -125,8 +125,8 @@ async def run_async(
125125
tool_name: str,
126126
tool_description: str,
127127
tool_params: dict[str, Any],
128-
tool_call_id: Optional[str] = None,
129-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
128+
tool_call_id: str | None = None,
129+
confirmation_strategy_context: dict[str, Any] | None = None,
130130
) -> ToolExecutionDecision:
131131
"""
132132
Async version of run. Calls the sync run() method by default.
@@ -210,8 +210,8 @@ def run(
210210
tool_name: str,
211211
tool_description: str,
212212
tool_params: dict[str, Any],
213-
tool_call_id: Optional[str] = None,
214-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
213+
tool_call_id: str | None = None,
214+
confirmation_strategy_context: dict[str, Any] | None = None,
215215
) -> ToolExecutionDecision:
216216
"""
217217
Run the breakpoint confirmation strategy for a given tool and its parameters.
@@ -248,8 +248,8 @@ async def run_async(
248248
tool_name: str,
249249
tool_description: str,
250250
tool_params: dict[str, Any],
251-
tool_call_id: Optional[str] = None,
252-
confirmation_strategy_context: Optional[dict[str, Any]] = None,
251+
tool_call_id: str | None = None,
252+
confirmation_strategy_context: dict[str, Any] | None = None,
253253
) -> ToolExecutionDecision:
254254
"""
255255
Async version of run. Calls the sync run() method.
@@ -304,7 +304,7 @@ def _prepare_tool_args(
304304
tool: Tool,
305305
tool_call_arguments: dict[str, Any],
306306
state: State,
307-
streaming_callback: Optional[StreamingCallbackT] = None,
307+
streaming_callback: StreamingCallbackT | None = None,
308308
enable_streaming_passthrough: bool = False,
309309
) -> dict[str, Any]:
310310
"""

haystack_experimental/components/agents/human_in_the_loop/types.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from typing import Any, Optional, Protocol
5+
from typing import Any, Protocol
66

77
from haystack.core.serialization import default_from_dict, default_to_dict
88

@@ -67,8 +67,8 @@ def run(
6767
tool_name: str,
6868
tool_description: str,
6969
tool_params: dict[str, Any],
70-
tool_call_id: Optional[str] = None,
71-
**kwargs: Optional[dict[str, Any]],
70+
tool_call_id: str | None = None,
71+
**kwargs: dict[str, Any] | None,
7272
) -> ToolExecutionDecision:
7373
"""
7474
Run the confirmation strategy for a given tool and its parameters.
@@ -92,8 +92,8 @@ async def run_async(
9292
tool_name: str,
9393
tool_description: str,
9494
tool_params: dict[str, Any],
95-
tool_call_id: Optional[str] = None,
96-
**kwargs: Optional[dict[str, Any]],
95+
tool_call_id: str | None = None,
96+
**kwargs: dict[str, Any] | None,
9797
) -> ToolExecutionDecision:
9898
"""
9999
Async version of run. Run the confirmation strategy for a given tool and its parameters.

haystack_experimental/components/agents/human_in_the_loop/user_interfaces.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import json
66
from threading import Lock
7-
from typing import Any, Optional
7+
from typing import Any
88

99
from haystack.core.serialization import default_to_dict
1010
from rich.console import Console
@@ -20,7 +20,7 @@
2020
class RichConsoleUI(ConfirmationUI):
2121
"""Rich console interface for user interaction."""
2222

23-
def __init__(self, console: Optional[Console] = None):
23+
def __init__(self, console: Console | None = None):
2424
self.console = console or Console()
2525

2626
def get_user_confirmation(

haystack_experimental/components/generators/chat/openai.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from dataclasses import replace
6-
from typing import Any, Optional
6+
from typing import Any
77

88
from haystack import component
99
from haystack.components.generators.chat.openai import OpenAIChatGenerator as BaseOpenAIChatGenerator
@@ -56,12 +56,12 @@ class OpenAIChatGenerator(BaseOpenAIChatGenerator):
5656
def run(
5757
self,
5858
messages: list[ChatMessage],
59-
streaming_callback: Optional[StreamingCallbackT] = None,
60-
generation_kwargs: Optional[dict[str, Any]] = None,
59+
streaming_callback: StreamingCallbackT | None = None,
60+
generation_kwargs: dict[str, Any] | None = None,
6161
*,
62-
tools: Optional[ToolsType] = None,
63-
tools_strict: Optional[bool] = None,
64-
hallucination_score_config: Optional[HallucinationScoreConfig] = None,
62+
tools: ToolsType | None = None,
63+
tools_strict: bool | None = None,
64+
hallucination_score_config: HallucinationScoreConfig | None = None,
6565
) -> dict[str, list[ChatMessage]]:
6666
"""
6767
Invokes chat completion based on the provided messages and generation parameters.
@@ -123,12 +123,12 @@ def run(
123123
async def run_async(
124124
self,
125125
messages: list[ChatMessage],
126-
streaming_callback: Optional[StreamingCallbackT] = None,
127-
generation_kwargs: Optional[dict[str, Any]] = None,
126+
streaming_callback: StreamingCallbackT | None = None,
127+
generation_kwargs: dict[str, Any] | None = None,
128128
*,
129-
tools: Optional[ToolsType] = None,
130-
tools_strict: Optional[bool] = None,
131-
hallucination_score_config: Optional[HallucinationScoreConfig] = None,
129+
tools: ToolsType | None = None,
130+
tools_strict: bool | None = None,
131+
hallucination_score_config: HallucinationScoreConfig | None = None,
132132
) -> dict[str, list[ChatMessage]]:
133133
"""
134134
Asynchronously invokes chat completion based on the provided messages and generation parameters.

0 commit comments

Comments
 (0)