Skip to content

Commit 6a47c1b

Browse files
committed
shift args and format
1 parent 0148f1f commit 6a47c1b

5 files changed

Lines changed: 37 additions & 43 deletions

File tree

src/runloop_api_client/lib/polling_async.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from .polling import PollingConfig, PollingTimeout
66

7-
T = TypeVar('T')
7+
T = TypeVar("T")
8+
89

910
async def async_poll_until(
1011
retriever: Callable[[], Awaitable[T]],
@@ -14,27 +15,27 @@ async def async_poll_until(
1415
) -> T:
1516
"""
1617
Poll until a condition is met or timeout/max attempts are reached.
17-
18+
1819
Args:
1920
retriever: Async or sync callable that returns the object to check
2021
is_terminal: Callable that returns True when polling should stop
2122
config: Optional polling configuration
2223
on_error: Optional error handler that can return a value to continue polling
2324
or re-raise the exception to stop polling
24-
25+
2526
Returns:
2627
The final state of the polled object
27-
28+
2829
Raises:
2930
PollingTimeout: When max attempts or timeout is reached
3031
"""
3132
if config is None:
3233
config = PollingConfig()
33-
34+
3435
attempts = 0
3536
start_time = time.time()
3637
last_result: Union[T, None] = None
37-
38+
3839
while True:
3940
try:
4041
last_result = await retriever()
@@ -43,23 +44,17 @@ async def async_poll_until(
4344
last_result = on_error(e)
4445
else:
4546
raise
46-
47+
4748
if is_terminal(last_result):
4849
return last_result
49-
50+
5051
attempts += 1
5152
if attempts >= config.max_attempts:
52-
raise PollingTimeout(
53-
f"Exceeded maximum attempts ({config.max_attempts})",
54-
last_result
55-
)
56-
53+
raise PollingTimeout(f"Exceeded maximum attempts ({config.max_attempts})", last_result)
54+
5755
if config.timeout_seconds is not None:
5856
elapsed = time.time() - start_time
5957
if elapsed >= config.timeout_seconds:
60-
raise PollingTimeout(
61-
f"Exceeded timeout of {config.timeout_seconds} seconds",
62-
last_result
63-
)
64-
58+
raise PollingTimeout(f"Exceeded timeout of {config.timeout_seconds} seconds", last_result)
59+
6560
await asyncio.sleep(config.interval_seconds)

src/runloop_api_client/resources/devboxes/devboxes.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,13 +1576,20 @@ async def create_and_await_running(
15761576
PollingTimeout: If polling times out before devbox is running
15771577
RunloopError: If devbox enters a non-running terminal state
15781578
"""
1579-
# Pass all create_args to the underlying create method
1580-
devbox = await self.create(**(create_args or {}))
15811579

15821580
# Extract polling config and other request args
15831581
if request_args is None:
15841582
request_args = {}
15851583

1584+
# Pass all create_args, relevant request args to the underlying create method
1585+
devbox = await self.create(
1586+
**(create_args or {}),
1587+
extra_headers=request_args.get("extra_headers", None),
1588+
extra_query=request_args.get("extra_query", None),
1589+
extra_body=request_args.get("extra_body", None),
1590+
timeout=request_args.get("timeout", None),
1591+
)
1592+
15861593
return await self.await_running(
15871594
devbox.id,
15881595
polling_config=request_args.get("polling_config", None),

src/runloop_api_client/resources/scenarios/runs.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def await_scored(
316316
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
317317
) -> ScenarioRunView:
318318
"""Wait for a scenario run to be scored.
319-
319+
320320
Args:
321321
id: The ID of the scenario run to wait for
322322
polling_config: Optional polling configuration
@@ -332,13 +332,10 @@ def await_scored(
332332
PollingTimeout: If polling times out before scenario run is scored
333333
RunloopError: If scenario run enters a non-scored terminal state
334334
"""
335+
335336
def retrieve_run() -> ScenarioRunView:
336337
return self.retrieve(
337-
id,
338-
extra_headers=extra_headers,
339-
extra_query=extra_query,
340-
extra_body=extra_body,
341-
timeout=timeout
338+
id, extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
342339
)
343340

344341
def is_done_scoring(run: ScenarioRunView) -> bool:
@@ -347,9 +344,7 @@ def is_done_scoring(run: ScenarioRunView) -> bool:
347344
run = poll_until(retrieve_run, is_done_scoring, polling_config)
348345

349346
if run.state != "scored":
350-
raise RunloopError(
351-
f"Scenario run entered non-scored state unexpectedly: {run.state}"
352-
)
347+
raise RunloopError(f"Scenario run entered non-scored state unexpectedly: {run.state}")
353348

354349
return run
355350

@@ -366,7 +361,7 @@ def score_and_await(
366361
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
367362
) -> ScenarioRunView:
368363
"""Score a scenario run and wait for it to be scored.
369-
364+
370365
Args:
371366
id: The ID of the scenario run to score and wait for
372367
polling_config: Optional polling configuration
@@ -412,7 +407,7 @@ def score_and_complete(
412407
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
413408
) -> ScenarioRunView:
414409
"""Score a scenario run, wait for it to be scored, then complete it.
415-
410+
416411
Args:
417412
id: The ID of the scenario run to score, wait for, and complete
418413
polling_config: Optional polling configuration
@@ -445,6 +440,7 @@ def score_and_complete(
445440
timeout=timeout,
446441
)
447442

443+
448444
class AsyncRunsResource(AsyncAPIResource):
449445
@cached_property
450446
def with_raw_response(self) -> AsyncRunsResourceWithRawResponse:
@@ -728,7 +724,7 @@ async def await_scored(
728724
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
729725
) -> ScenarioRunView:
730726
"""Wait for a scenario run to be scored.
731-
727+
732728
Args:
733729
id: The ID of the scenario run to wait for
734730
polling_config: Optional polling configuration
@@ -744,13 +740,10 @@ async def await_scored(
744740
PollingTimeout: If polling times out before scenario run is scored
745741
RunloopError: If scenario run enters a non-scored terminal state
746742
"""
743+
747744
async def retrieve_run() -> ScenarioRunView:
748745
return await self.retrieve(
749-
id,
750-
extra_headers=extra_headers,
751-
extra_query=extra_query,
752-
extra_body=extra_body,
753-
timeout=timeout
746+
id, extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
754747
)
755748

756749
def is_done_scoring(run: ScenarioRunView) -> bool:
@@ -759,9 +752,7 @@ def is_done_scoring(run: ScenarioRunView) -> bool:
759752
run = await async_poll_until(retrieve_run, is_done_scoring, polling_config)
760753

761754
if run.state != "scored":
762-
raise RunloopError(
763-
f"Scenario run entered non-scored state unexpectedly: {run.state}"
764-
)
755+
raise RunloopError(f"Scenario run entered non-scored state unexpectedly: {run.state}")
765756

766757
return run
767758

@@ -778,7 +769,7 @@ async def score_and_await(
778769
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
779770
) -> ScenarioRunView:
780771
"""Score a scenario run and wait for it to be scored.
781-
772+
782773
Args:
783774
id: The ID of the scenario run to score and wait for
784775
polling_config: Optional polling configuration
@@ -824,7 +815,7 @@ async def score_and_complete(
824815
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
825816
) -> ScenarioRunView:
826817
"""Score a scenario run, wait for it to be scored, then complete it.
827-
818+
828819
Args:
829820
id: The ID of the scenario run to score, wait for, and complete
830821
polling_config: Optional polling configuration

src/runloop_api_client/resources/scenarios/scenarios.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,7 @@ async def start_run_and_await_env_ready(
926926

927927
return run
928928

929+
929930
class ScenariosResourceWithRawResponse:
930931
def __init__(self, scenarios: ScenariosResource) -> None:
931932
self._scenarios = scenarios

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)