Files
langgraph/libs/sdk-py/tests/integration/test_runs.py
T
Elior Nataf LackritzandGitHub ea5f9cc9fb chore: enforce PLC0415 in tests for the remaining packages (#8547)
Follow-up to #8540, which turned on `PLC0415` (import-outside-top-level)
for checkpoint-postgres and checkpoint-sqlite. This does the remaining
six packages: checkpoint, checkpoint-conformance, langgraph, prebuilt,
cli, sdk-py.

Scoped to tests, per @sydney-runkle's call on #8540: library code is
exempted with `per-file-ignores`, since it still has deferred imports
nobody has reviewed and mixing that in would make this hard to read.

## What changed

Function-level imports across 56 test files moved to module level. Nine
could not move and carry an explicit `# noqa: PLC0415` with a reason:

| File | Why it stays local |
|---|---|
| `libs/langgraph/tests/test_deprecation.py` (4) | the import has to run
inside `pytest.warns` for the warning to be observed |
| `libs/langgraph/tests/test_serde_allowlist.py` | try/except guard,
skips when langchain_core is absent |
| `libs/langgraph/tests/test_delta_channel_benchmark.py` | optional
psycopg probe |
| `libs/checkpoint/tests/test_conformance_delta.py` (3) | protected by a
module-level `pytest.importorskip`; hoisting past the guard turns a skip
into a collection error |

That last one is the trap: an import moved above `pytest.importorskip`
silently defeats the guard. I hit it locally and it turned the skip into
a `ModuleNotFoundError` at collection. Every file with an `importorskip`
or `except ImportError` was checked by hand for this.

## Verification

`make lint` and `make test` in each of the six:

| Package | Tests |
|---|---|
| checkpoint | 156 passed, 17 skipped |
| checkpoint-conformance | 1 passed |
| langgraph | 1968 passed, 4 skipped |
| prebuilt | 284 passed |
| cli | 336 passed |
| sdk-py | 493 passed |

Also confirmed the rule actually fires: a throwaway test file with a
function-level import is flagged in all six packages, and the source
exemption holds.
2026-08-07 09:40:18 -04:00

127 lines
3.9 KiB
Python

"""`RunsClient` non-streaming surface.
`cancel` is covered in `test_cancel.py`. This file covers create / get /
list / wait. The canonical `agent` graph interrupts at `ask_human`, so a
plain `runs.create` lands in the `interrupted` state. We use
`interrupt_before=["ask_human"]` so the run pauses before the interrupting
node and reaches a deterministic non-success terminal.
"""
from __future__ import annotations
import pytest
from langgraph_sdk._async.http import HttpClient
from langgraph_sdk._async.runs import RunsClient
from langgraph_sdk._sync.http import SyncHttpClient
from langgraph_sdk._sync.runs import SyncRunsClient
from .conftest import ASSISTANT_ID
pytestmark = pytest.mark.integration
def _async_runs(raw):
return RunsClient(HttpClient(raw))
def _sync_runs(raw):
return SyncRunsClient(SyncHttpClient(raw))
async def test_runs_create_get_list_async(async_threads) -> None:
threads, raw = async_threads
runs = _async_runs(raw)
thread = await threads.create(
metadata={"suite": "integration", "label": "runs-async"}
)
tid = thread["thread_id"]
try:
created = await runs.create(
tid,
ASSISTANT_ID,
input={"messages": [], "value": "init", "items": []},
)
run_id = created["run_id"]
assert created["thread_id"] == tid
fetched = await runs.get(tid, run_id)
assert fetched["run_id"] == run_id
listed = await runs.list(tid, limit=10)
assert any(r["run_id"] == run_id for r in listed)
finally:
await threads.delete(tid)
def test_runs_create_get_list_sync(sync_threads) -> None:
threads, raw = sync_threads
runs = _sync_runs(raw)
thread = threads.create(metadata={"suite": "integration", "label": "runs-sync"})
tid = thread["thread_id"]
try:
created = runs.create(
tid,
ASSISTANT_ID,
input={"messages": [], "value": "init", "items": []},
)
run_id = created["run_id"]
assert created["thread_id"] == tid
fetched = runs.get(tid, run_id)
assert fetched["run_id"] == run_id
listed = runs.list(tid, limit=10)
assert any(r["run_id"] == run_id for r in listed)
finally:
threads.delete(tid)
async def test_runs_wait_async(async_threads) -> None:
"""`wait` blocks until the run reaches a terminal state and returns its values."""
threads, raw = async_threads
runs = _async_runs(raw)
thread = await threads.create(
metadata={"suite": "integration", "label": "wait-async"}
)
tid = thread["thread_id"]
try:
# `interrupt_before` makes the run pause before `ask_human` rather
# than running into the dynamic `interrupt(...)` inside it; the run
# ends up in `interrupted` status with a deterministic terminal.
result = await runs.wait(
tid,
ASSISTANT_ID,
input={"messages": [], "value": "init", "items": []},
interrupt_before=["ask_human"],
)
# The result is the terminal `values` payload for this run.
assert isinstance(result, dict)
assert "items" in result
assert "streamed" in result["items"]
assert "tool" in result["items"]
finally:
await threads.delete(tid)
def test_runs_wait_sync(sync_threads) -> None:
threads, raw = sync_threads
runs = _sync_runs(raw)
thread = threads.create(metadata={"suite": "integration", "label": "wait-sync"})
tid = thread["thread_id"]
try:
result = runs.wait(
tid,
ASSISTANT_ID,
input={"messages": [], "value": "init", "items": []},
interrupt_before=["ask_human"],
)
assert isinstance(result, dict)
assert "items" in result
assert "streamed" in result["items"]
assert "tool" in result["items"]
finally:
threads.delete(tid)