Skip to content

Commit 6426144

Browse files
cbornetmdrxy
andauthored
feat(langchain): add ruff rules TRY (#32047)
See https://docs.astral.sh/ruff/rules/#tryceratops-try * TRY004 (replace by TypeError) in main code is escaped with `noqa` to not break backward compatibility. The rule is still interesting for new code. * TRY301 ignored at the moment. This one is quite hard to fix and I'm not sure it's very interesting to activate it. Co-authored-by: Mason Daugherty <[email protected]>
1 parent 8b8d90b commit 6426144

File tree

32 files changed

+72
-72
lines changed

32 files changed

+72
-72
lines changed

libs/langchain/langchain/_api/module_import.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,12 @@ def import_by_name(name: str) -> Any:
106106
"<https://python.langchain.com/docs/versions/v0_2/>"
107107
),
108108
)
109-
return result
110109
except Exception as e:
111110
msg = f"module {new_module} has no attribute {name}"
112111
raise AttributeError(msg) from e
113112

113+
return result
114+
114115
if fallback_module:
115116
try:
116117
module = importlib.import_module(fallback_module)
@@ -139,12 +140,13 @@ def import_by_name(name: str) -> Any:
139140
"<https://python.langchain.com/docs/versions/v0_2/>"
140141
),
141142
)
142-
return result
143143

144144
except Exception as e:
145145
msg = f"module {fallback_module} has no attribute {name}"
146146
raise AttributeError(msg) from e
147147

148+
return result
149+
148150
msg = f"module {package} has no attribute {name}"
149151
raise AttributeError(msg)
150152

libs/langchain/langchain/agents/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ def _iter_next_step(
13801380
observation = self.handle_parsing_errors(e)
13811381
else:
13821382
msg = "Got unexpected type of `handle_parsing_errors`"
1383-
raise ValueError(msg) from e
1383+
raise ValueError(msg) from e # noqa: TRY004
13841384
output = AgentAction("_Exception", observation, text)
13851385
if run_manager:
13861386
run_manager.on_agent_action(output, color="green")
@@ -1519,7 +1519,7 @@ async def _aiter_next_step(
15191519
observation = self.handle_parsing_errors(e)
15201520
else:
15211521
msg = "Got unexpected type of `handle_parsing_errors`"
1522-
raise ValueError(msg) from e
1522+
raise ValueError(msg) from e # noqa: TRY004
15231523
output = AgentAction("_Exception", observation, text)
15241524
tool_run_kwargs = self._action_agent.tool_run_logging_kwargs()
15251525
observation = await ExceptionTool().arun(

libs/langchain/langchain/agents/chat/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _construct_scratchpad(
5555
agent_scratchpad = super()._construct_scratchpad(intermediate_steps)
5656
if not isinstance(agent_scratchpad, str):
5757
msg = "agent_scratchpad should be of type string."
58-
raise ValueError(msg)
58+
raise ValueError(msg) # noqa: TRY004
5959
if agent_scratchpad:
6060
return (
6161
f"This was your previous work "

libs/langchain/langchain/agents/openai_assistant/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,12 @@ def invoke(
358358
run = self._wait_for_run(run.id, run.thread_id)
359359
except BaseException as e:
360360
run_manager.on_chain_error(e)
361-
raise e
361+
raise
362362
try:
363363
response = self._get_response(run)
364364
except BaseException as e:
365365
run_manager.on_chain_error(e, metadata=run.dict())
366-
raise e
366+
raise
367367
else:
368368
run_manager.on_chain_end(response)
369369
return response
@@ -494,12 +494,12 @@ async def ainvoke(
494494
run = await self._await_for_run(run.id, run.thread_id)
495495
except BaseException as e:
496496
run_manager.on_chain_error(e)
497-
raise e
497+
raise
498498
try:
499499
response = self._get_response(run)
500500
except BaseException as e:
501501
run_manager.on_chain_error(e, metadata=run.dict())
502-
raise e
502+
raise
503503
else:
504504
run_manager.on_chain_end(response)
505505
return response

libs/langchain/langchain/agents/output_parsers/openai_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def parse_result(
8787
) -> Union[AgentAction, AgentFinish]:
8888
if not isinstance(result[0], ChatGeneration):
8989
msg = "This output parser only works on ChatGeneration output"
90-
raise ValueError(msg)
90+
raise ValueError(msg) # noqa: TRY004
9191
message = result[0].message
9292
return self._parse_ai_message(message)
9393

libs/langchain/langchain/agents/output_parsers/openai_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def parse_result(
6161
) -> Union[list[AgentAction], AgentFinish]:
6262
if not isinstance(result[0], ChatGeneration):
6363
msg = "This output parser only works on ChatGeneration output"
64-
raise ValueError(msg)
64+
raise ValueError(msg) # noqa: TRY004
6565
message = result[0].message
6666
return parse_ai_message_to_openai_tool_action(message)
6767

libs/langchain/langchain/agents/output_parsers/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def parse_result(
9898
) -> Union[list[AgentAction], AgentFinish]:
9999
if not isinstance(result[0], ChatGeneration):
100100
msg = "This output parser only works on ChatGeneration output"
101-
raise ValueError(msg)
101+
raise ValueError(msg) # noqa: TRY004
102102
message = result[0].message
103103
return parse_ai_message_to_tool_action(message)
104104

libs/langchain/langchain/agents/structured_chat/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _construct_scratchpad(
5656
agent_scratchpad = super()._construct_scratchpad(intermediate_steps)
5757
if not isinstance(agent_scratchpad, str):
5858
msg = "agent_scratchpad should be of type string."
59-
raise ValueError(msg)
59+
raise ValueError(msg) # noqa: TRY004
6060
if agent_scratchpad:
6161
return (
6262
f"This was your previous work "

libs/langchain/langchain/agents/structured_chat/output_parser.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,8 @@ def get_format_instructions(self) -> str:
7171
def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
7272
try:
7373
if self.output_fixing_parser is not None:
74-
parsed_obj: Union[AgentAction, AgentFinish] = (
75-
self.output_fixing_parser.parse(text)
76-
)
77-
else:
78-
parsed_obj = self.base_parser.parse(text)
79-
return parsed_obj
74+
return self.output_fixing_parser.parse(text)
75+
return self.base_parser.parse(text)
8076
except Exception as e:
8177
msg = f"Could not parse LLM output: {text}"
8278
raise OutputParserException(msg) from e

libs/langchain/langchain/chains/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def invoke(
174174
)
175175
except BaseException as e:
176176
run_manager.on_chain_error(e)
177-
raise e
177+
raise
178178
run_manager.on_chain_end(outputs)
179179

180180
if include_run_info:
@@ -228,7 +228,7 @@ async def ainvoke(
228228
)
229229
except BaseException as e:
230230
await run_manager.on_chain_error(e)
231-
raise e
231+
raise
232232
await run_manager.on_chain_end(outputs)
233233

234234
if include_run_info:

0 commit comments

Comments
 (0)