Skip to content

Commit 3e6dd71

Browse files
lint
1 parent 82a3b2c commit 3e6dd71

File tree

7 files changed

+33
-26
lines changed

7 files changed

+33
-26
lines changed

core/testcontainers/core/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def status(self) -> str:
285285
"""Get container status for compatibility with wait strategies."""
286286
if not self._container:
287287
return "not_started"
288-
return self._container.status
288+
return cast("str", self._container.status)
289289

290290
def exec(self, command: Union[str, list[str]]) -> ExecResult:
291291
if not self._container:

core/testcontainers/core/generic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def _create_connection_url(
6262
if self._container is None:
6363
raise ContainerStartException("container has not been started")
6464
host = host or self.get_container_host_ip()
65+
assert port is not None
6566
port = self.get_exposed_port(port)
6667
quoted_password = quote(password, safe=" +")
6768
url = f"{dialect}://{username}:{quoted_password}@{host}:{port}"

core/testcontainers/core/wait_strategies.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
import re
3030
import time
3131
from datetime import timedelta
32-
from typing import Union
32+
from typing import TYPE_CHECKING, Union
3333

3434
from testcontainers.core.utils import setup_logger
3535

3636
# Import base classes from waiting_utils to make them available for tests
3737
from .waiting_utils import WaitStrategy
3838

39+
if TYPE_CHECKING:
40+
from .waiting_utils import WaitStrategyTarget
41+
3942
logger = setup_logger(__name__)
4043

4144

@@ -89,7 +92,7 @@ def with_poll_interval(self, interval: Union[float, timedelta]) -> "LogMessageWa
8992
self._poll_interval = interval
9093
return self
9194

92-
def wait_until_ready(self, container) -> None:
95+
def wait_until_ready(self, container: "WaitStrategyTarget") -> None:
9396
"""
9497
Wait until the specified message appears in the container logs.
9598

core/testcontainers/core/waiting_utils.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414

1515
import re
1616
import time
17-
import traceback
1817
import warnings
1918
from abc import ABC, abstractmethod
2019
from datetime import timedelta
21-
from typing import Any, Callable, Protocol, TypeVar, TYPE_CHECKING, Union, cast
20+
from typing import Any, Callable, Optional, Protocol, TypeVar, Union, cast
2221

2322
import wrapt
2423

2524
from testcontainers.core.config import testcontainers_config as config
2625
from testcontainers.core.utils import setup_logger
2726

28-
if TYPE_CHECKING:
29-
from testcontainers.core.container import DockerContainer
30-
3127
logger = setup_logger(__name__)
3228

3329
# Get a tuple of transient exceptions for which we'll retry. Other exceptions will be raised.
@@ -36,6 +32,7 @@
3632
# Type variables for generic functions
3733
F = TypeVar("F", bound=Callable[..., Any])
3834

35+
3936
class WaitStrategyTarget(Protocol):
4037
"""
4138
Protocol defining the interface that containers must implement for wait strategies.
@@ -163,7 +160,7 @@ def wait_until_ready(self, container: WaitStrategyTarget) -> Any:
163160
logger.debug(f"Connection attempt failed: {e!s}")
164161
time.sleep(self._poll_interval)
165162

166-
@wrapt.decorator
163+
@wrapt.decorator # type: ignore[misc]
167164
def wrapper(wrapped: Callable[..., Any], instance: Any, args: list[Any], kwargs: dict[str, Any]) -> Any:
168165
# Use the LegacyWaitStrategy to handle retries with proper timeout
169166
strategy = LegacyWaitStrategy(wrapped, instance, args, kwargs)
@@ -177,6 +174,7 @@ def wrapper(wrapped: Callable[..., Any], instance: Any, args: list[Any], kwargs:
177174

178175
return cast("Callable[[F], F]", wrapper)
179176

177+
180178
@wait_container_is_ready()
181179
def wait_for(condition: Callable[..., bool]) -> bool:
182180
warnings.warn(
@@ -244,8 +242,13 @@ def wait_for_logs(
244242
# For more complex scenarios, use structured wait strategies directly:
245243
container.waiting_for(LogMessageWaitStrategy("ready"))
246244
"""
247-
# Only warn for legacy usage (string or callable predicates, not WaitStrategy objects)
248-
if not isinstance(predicate, WaitStrategy):
245+
if isinstance(predicate, WaitStrategy):
246+
start = time.time()
247+
predicate.with_startup_timeout(int(timeout)).with_poll_interval(interval)
248+
predicate.wait_until_ready(container)
249+
return time.time() - start
250+
else:
251+
# Only warn for legacy usage (string or callable predicates, not WaitStrategy objects)
249252
warnings.warn(
250253
"The wait_for_logs function with string or callable predicates is deprecated and will be removed in a future version. "
251254
"Use structured wait strategies instead: "
@@ -254,11 +257,6 @@ def wait_for_logs(
254257
DeprecationWarning,
255258
stacklevel=2,
256259
)
257-
if isinstance(predicate, WaitStrategy):
258-
start = time.time()
259-
predicate.with_startup_timeout(int(timeout)).with_poll_interval(interval)
260-
predicate.wait_until_ready(container)
261-
return time.time() - start
262260

263261
# Original implementation for backwards compatibility
264262
re_predicate: Optional[Callable[[str], Any]] = None

core/testcontainers/socat/socat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,7 @@ def start(self) -> "SocatContainer":
8585
@wait_container_is_ready(OSError)
8686
def _connect(self) -> None:
8787
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
88-
s.connect((self.get_container_host_ip(), int(self.get_exposed_port(next(iter(self.ports))))))
88+
next_port = next(iter(self.ports))
89+
# todo remove this limitation
90+
assert isinstance(next_port, int)
91+
s.connect((self.get_container_host_ip(), int(self.get_exposed_port(next_port))))

core/tests/test_wait_strategies.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import itertools
12
import re
23
import time
4+
import typing
35
from datetime import timedelta
4-
from unittest.mock import Mock, patch, MagicMock
6+
from unittest.mock import Mock, patch
7+
58
import pytest
6-
import itertools
79

8-
from testcontainers.core.container import DockerContainer
9-
from testcontainers.core.wait_strategies import (
10-
LogMessageWaitStrategy,
11-
WaitStrategy,
12-
)
10+
from testcontainers.core.wait_strategies import LogMessageWaitStrategy
11+
from testcontainers.core.waiting_utils import WaitStrategy
12+
13+
if typing.TYPE_CHECKING:
14+
from testcontainers.core.waiting_utils import WaitStrategyTarget
1315

1416

1517
class ConcreteWaitStrategy(WaitStrategy):

core/tests/test_waiting_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_wait_container_is_ready_decorator_basic() -> None:
1818
"""Test the basic wait_container_is_ready decorator functionality."""
1919

2020
@wait_container_is_ready()
21-
def simple_check():
21+
def simple_check() -> bool:
2222
return True
2323

2424
result = simple_check()
@@ -29,7 +29,7 @@ def test_wait_container_is_ready_decorator_with_container() -> None:
2929
"""Test wait_container_is_ready decorator with a real container."""
3030

3131
@wait_container_is_ready()
32-
def check_container_logs(container):
32+
def check_container_logs(container: DockerContainer) -> bool:
3333
stdout, stderr = container.get_logs()
3434
return b"Hello from Docker!" in stdout or b"Hello from Docker!" in stderr
3535

0 commit comments

Comments
 (0)