Skip to content

Commit 791d309

Browse files
cbornetmdrxy
andauthored
chore(langchain): add mypy warn_unreachable setting (#32529)
See https://mypy.readthedocs.io/en/stable/config_file.html#confval-warn_unreachable --------- Co-authored-by: Mason Daugherty <github@mdrxy.com>
1 parent d3d23e2 commit 791d309

File tree

23 files changed

+55
-46
lines changed

23 files changed

+55
-46
lines changed

libs/langchain/langchain/agents/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ def _iter_next_step(
13751375
elif callable(self.handle_parsing_errors):
13761376
observation = self.handle_parsing_errors(e)
13771377
else:
1378-
msg = "Got unexpected type of `handle_parsing_errors`"
1378+
msg = "Got unexpected type of `handle_parsing_errors`" # type: ignore[unreachable]
13791379
raise ValueError(msg) from e # noqa: TRY004
13801380
output = AgentAction("_Exception", observation, text)
13811381
if run_manager:
@@ -1514,7 +1514,7 @@ async def _aiter_next_step(
15141514
elif callable(self.handle_parsing_errors):
15151515
observation = self.handle_parsing_errors(e)
15161516
else:
1517-
msg = "Got unexpected type of `handle_parsing_errors`"
1517+
msg = "Got unexpected type of `handle_parsing_errors`" # type: ignore[unreachable]
15181518
raise ValueError(msg) from e # noqa: TRY004
15191519
output = AgentAction("_Exception", observation, text)
15201520
tool_run_kwargs = self._action_agent.tool_run_logging_kwargs()

libs/langchain/langchain/agents/format_scratchpad/openai_functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from collections.abc import Sequence
3+
from typing import Any
34

45
from langchain_core.agents import AgentAction, AgentActionMessageLog
56
from langchain_core.messages import AIMessage, BaseMessage, FunctionMessage
@@ -30,7 +31,7 @@ def _convert_agent_action_to_messages(
3031

3132
def _create_function_message(
3233
agent_action: AgentAction,
33-
observation: str,
34+
observation: Any,
3435
) -> FunctionMessage:
3536
"""Convert agent action and observation into a function message.
3637
Args:

libs/langchain/langchain/agents/format_scratchpad/tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from collections.abc import Sequence
3+
from typing import Any
34

45
from langchain_core.agents import AgentAction
56
from langchain_core.messages import (
@@ -13,7 +14,7 @@
1314

1415
def _create_tool_message(
1516
agent_action: ToolAgentAction,
16-
observation: str,
17+
observation: Any,
1718
) -> ToolMessage:
1819
"""Convert agent action and observation into a tool message.
1920

libs/langchain/langchain/agents/mrkl/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def _validate_tools(cls, tools: Sequence[BaseTool]) -> None:
170170
raise ValueError(msg)
171171
for tool in tools:
172172
if tool.description is None:
173-
msg = (
173+
msg = ( # type: ignore[unreachable]
174174
f"Got a tool {tool.name} without a description. For this agent, "
175175
f"a description must always be provided."
176176
)

libs/langchain/langchain/agents/output_parsers/json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class JSONAgentOutputParser(AgentOutputParser):
4545
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
4646
try:
4747
response = parse_json_markdown(text)
48-
if isinstance(response, list):
48+
if isinstance(response, list): # type: ignore[unreachable]
4949
# gpt turbo frequently ignores the directive to emit a single action
50-
logger.warning("Got multiple action responses: %s", response)
50+
logger.warning("Got multiple action responses: %s", response) # type: ignore[unreachable]
5151
response = response[0]
5252
if response["action"] == "Final Answer":
5353
return AgentFinish({"output": response["action_input"]}, text)

libs/langchain/langchain/chains/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def input_keys(self) -> list[str]:
286286
def output_keys(self) -> list[str]:
287287
"""Keys expected to be in the chain output."""
288288

289-
def _validate_inputs(self, inputs: dict[str, Any]) -> None:
289+
def _validate_inputs(self, inputs: Any) -> None:
290290
"""Check that all inputs are present."""
291291
if not isinstance(inputs, dict):
292292
_input_keys = set(self.input_keys)

libs/langchain/langchain/chains/conversational_retrieval/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _get_chat_history(chat_history: list[CHAT_TURN_TYPE]) -> str:
5454
ai = "Assistant: " + dialogue_turn[1]
5555
buffer += f"\n{human}\n{ai}"
5656
else:
57-
msg = (
57+
msg = ( # type: ignore[unreachable]
5858
f"Unsupported chat history format: {type(dialogue_turn)}."
5959
f" Full chat history: {chat_history} "
6060
)

libs/langchain/langchain/chains/query_constructor/parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
try:
1010
check_package_version("lark", gte_version="1.1.5")
1111
from lark import Lark, Transformer, v_args
12+
13+
_HAS_LARK = True
1214
except ImportError:
1315

1416
def v_args(*_: Any, **__: Any) -> Any: # type: ignore[misc]
@@ -17,6 +19,7 @@ def v_args(*_: Any, **__: Any) -> Any: # type: ignore[misc]
1719

1820
Transformer = object # type: ignore[assignment,misc]
1921
Lark = object # type: ignore[assignment,misc]
22+
_HAS_LARK = False
2023

2124
from langchain_core.structured_query import (
2225
Comparator,
@@ -260,8 +263,7 @@ def get_parser(
260263
Returns:
261264
Lark parser for the query language.
262265
"""
263-
# QueryTransformer is None when Lark cannot be imported.
264-
if QueryTransformer is None:
266+
if not _HAS_LARK:
265267
msg = "Cannot import lark, please install it with 'pip install lark'."
266268
raise ImportError(msg)
267269
transformer = QueryTransformer(

libs/langchain/langchain/chains/structured_output/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class Dog(BaseModel):
439439
output_parser=output_parser,
440440
**kwargs,
441441
)
442-
msg = (
442+
msg = ( # type: ignore[unreachable]
443443
f"Invalid mode {mode}. Expected one of 'openai-tools', 'openai-functions', "
444444
f"'openai-json'."
445445
)

libs/langchain/langchain/embeddings/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def from_bytes_store(
336336
)
337337
raise ValueError(msg)
338338
else:
339-
msg = (
339+
msg = ( # type: ignore[unreachable]
340340
"key_encoder must be either 'blake2b', 'sha1', 'sha256', 'sha512' "
341341
"or a callable that encodes keys."
342342
)

0 commit comments

Comments
 (0)