core: Add ruff rules TRY (tryceratops) (#29388)

TRY004 ("use TypeError rather than ValueError") existing errors are
marked as ignore to preserve backward compatibility.
LMK if you prefer to fix some of them.

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Christophe Bornet
2025-01-24 06:01:40 +01:00
committed by GitHub
parent 723b603f52
commit dbb6b7b103
26 changed files with 138 additions and 126 deletions

View File

@@ -103,7 +103,7 @@ def _runnable(inputs: dict) -> str:
if inputs["text"] == "bar":
return "second"
if isinstance(inputs["exception"], ValueError):
raise RuntimeError
raise RuntimeError # noqa: TRY004
return "third"

View File

@@ -1600,7 +1600,7 @@ async def test_event_stream_with_retry() -> None:
def fail(inputs: str) -> None:
"""Simple func."""
msg = "fail"
raise Exception(msg)
raise ValueError(msg)
chain = RunnableLambda(success) | RunnableLambda(fail).with_retry(
stop_after_attempt=1,

View File

@@ -1556,7 +1556,7 @@ async def test_event_stream_with_retry() -> None:
def fail(inputs: str) -> None:
"""Simple func."""
msg = "fail"
raise Exception(msg)
raise ValueError(msg)
chain = RunnableLambda(success) | RunnableLambda(fail).with_retry(
stop_after_attempt=1,
@@ -1906,7 +1906,7 @@ async def test_runnable_with_message_history() -> None:
return fn(*args, **kwargs)
except Exception as e:
raised_errors.append(e)
raise e
raise
return _get_output_messages
@@ -2097,7 +2097,7 @@ class StreamingRunnable(Runnable[Input, Output]):
final_output = None
for element in self.iterable:
if isinstance(element, BaseException):
raise element
raise element # noqa: TRY301
yield element
if final_output is None:
@@ -2409,10 +2409,10 @@ async def test_break_astream_events() -> None:
self.started = True
try:
await asyncio.sleep(0.5)
return input
except asyncio.CancelledError:
self.cancelled = True
raise
return input
def reset(self) -> None:
self.started = False
@@ -2474,10 +2474,10 @@ async def test_cancel_astream_events() -> None:
self.started = True
try:
await asyncio.sleep(0.5)
return input
except asyncio.CancelledError:
self.cancelled = True
raise
return input
def reset(self) -> None:
self.started = False