From 765dad36ec71777c1fde80307c62a0c4f7c693fd Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 31 Mar 2025 11:55:34 +0200 Subject: [PATCH 1/2] Allow use of `PYTHONOPTIMIZE=1` --- pydantic_ai_slim/pydantic_ai/_agent_graph.py | 5 ++--- pydantic_ai_slim/pydantic_ai/agent.py | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/_agent_graph.py b/pydantic_ai_slim/pydantic_ai/_agent_graph.py index 2f27aac8da..e3785e7adf 100644 --- a/pydantic_ai_slim/pydantic_ai/_agent_graph.py +++ b/pydantic_ai_slim/pydantic_ai/_agent_graph.py @@ -371,9 +371,8 @@ async def run( ) -> Union[ModelRequestNode[DepsT, NodeRunEndT], End[result.FinalResult[NodeRunEndT]]]: # noqa UP007 async with self.stream(ctx): pass - - assert (next_node := self._next_node) is not None, 'the stream should set `self._next_node` before it ends' - return next_node + assert self._next_node is not None, 'the stream should set `self._next_node` before it ends' + return self._next_node @asynccontextmanager async def stream( diff --git a/pydantic_ai_slim/pydantic_ai/agent.py b/pydantic_ai_slim/pydantic_ai/agent.py index da19e10a55..3362bee51e 100644 --- a/pydantic_ai_slim/pydantic_ai/agent.py +++ b/pydantic_ai_slim/pydantic_ai/agent.py @@ -327,8 +327,8 @@ async def main(): async for _ in agent_run: pass - assert (final_result := agent_run.result) is not None, 'The graph run did not finish properly' - return final_result + assert agent_run.result is not None, 'The graph run did not finish properly' + return agent_run.result @asynccontextmanager async def iter( From a68c28fe44ef5b7c17a7a9b566a55db467ddd6ce Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Mon, 31 Mar 2025 11:58:06 +0200 Subject: [PATCH 2/2] Add ruff rule --- pyproject.toml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index adcd5dfece..5f7a48fafc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,7 +102,16 @@ include = [ ] [tool.ruff.lint] -extend-select = ["Q", "RUF100", "C90", "UP", "I", "D", "TID251"] +extend-select = [ + "Q", + "RUF100", + "RUF018", # https://docs.astral.sh/ruff/rules/assignment-in-assert/ + "C90", + "UP", + "I", + "D", + "TID251", +] flake8-quotes = { inline-quotes = "single", multiline-quotes = "double" } isort = { combine-as-imports = true, known-first-party = ["pydantic_ai"] } mccabe = { max-complexity = 15 }