Skip to content

Commit e323317

Browse files
authored
fix(core): Use WaitStrategy internally for wait_for function (#942)
# fix(core): Use WaitStrategy internally for wait_for function ## Summary Refactor the deprecated `wait_for` function to use the new `WaitStrategy` system internally instead of the `@wait_container_is_ready` decorator. This fixes the import-time deprecation warning from #874. See [alexanderankin's comment](#874 (comment)) stating core should no longer warn, and [jonaslb's reply](#874 (comment)) identifying that `wait_for` is still decorated with `@wait_container_is_ready` at module level, so it generates a warning. ## Problem The `wait_for` function was decorated with `@wait_container_is_ready()`, which emits a deprecation warning at **decoration time** (import time), not when the function is called. This caused warnings even when users never used `wait_for`. ### Reproducer ```bash mkdir /tmp/reproduce && cd /tmp/reproduce echo 'import testcontainers.core.waiting_utils def test_import(): pass' > test_import.py uv run --with testcontainers --with pytest pytest test_import.py ``` ``` =============================== warnings summary =============================== waiting_utils.py:215: DeprecationWarning: The @wait_container_is_ready decorator is deprecated and will be removed in a future version... @wait_container_is_ready() 1 passed, 1 warning ``` Note: `wait_for` is never called - the warning is emitted just by importing the module.
1 parent 183e1aa commit e323317

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

core/testcontainers/core/waiting_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ def wrapper(wrapped: Callable[..., Any], instance: Any, args: tuple[Any], kwargs
216216
return cast("Callable[[F], F]", wrapper)
217217

218218

219-
@wait_container_is_ready()
220219
def wait_for(condition: Callable[..., bool]) -> bool:
221220
warnings.warn(
222221
"The wait_for function is deprecated and will be removed in a future version. "
@@ -226,7 +225,15 @@ def wait_for(condition: Callable[..., bool]) -> bool:
226225
DeprecationWarning,
227226
stacklevel=2,
228227
)
229-
return condition()
228+
229+
class CallableWaitStrategy(WaitStrategy):
230+
def wait_until_ready(self, container: WaitStrategyTarget) -> None:
231+
pass # Required by ABC, but unused
232+
233+
strategy = CallableWaitStrategy()
234+
if not strategy._poll(condition):
235+
raise TimeoutError(f"Condition not satisfied within {strategy._startup_timeout}s")
236+
return True
230237

231238

232239
_NOT_EXITED_STATUSES = {"running", "created"}

0 commit comments

Comments
 (0)