Skip to content

Commit a347648

Browse files
refactor(typing): annotate self-returning methods with Self (#1008)
Co-authored-by: Roy Moore <roy@moore.co.il>
1 parent f181fa3 commit a347648

32 files changed

Lines changed: 93 additions & 56 deletions

File tree

src/testcontainers/community/azurite/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import os
1515
from typing import Optional
1616

17+
from typing_extensions import Self
18+
1719
from testcontainers.core.container import DockerContainer
1820
from testcontainers.core.utils import raise_for_deprecated_parameter
1921
from testcontainers.core.wait_strategies import PortWaitStrategy
@@ -217,7 +219,7 @@ def __get_external_connection_string(self) -> str:
217219

218220
return connection_string
219221

220-
def start(self) -> "AzuriteContainer":
222+
def start(self) -> Self:
221223
super().start()
222224
self._connect()
223225
return self

src/testcontainers/community/chroma/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import TYPE_CHECKING
22

33
from requests import ConnectionError, get
4+
from typing_extensions import Self
45

56
from testcontainers.core.container import DockerContainer
67
from testcontainers.core.utils import raise_for_deprecated_parameter
@@ -73,7 +74,7 @@ def _healthcheck(self) -> None:
7374
response: Response = get(url)
7475
response.raise_for_status()
7576

76-
def start(self) -> "ChromaContainer":
77+
def start(self) -> Self:
7778
"""This method starts the Chroma container and runs the healthcheck
7879
to verify that the container is ready to use."""
7980
super().start()

src/testcontainers/community/generic/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Union
22

33
import httpx
4+
from typing_extensions import Self
45

56
from testcontainers.core.container import DockerContainer
67
from testcontainers.core.exceptions import ContainerStartException
@@ -53,7 +54,7 @@ def _create_connection_url(self) -> str:
5354
url = f"http://{host}:{exposed_port}"
5455
return url
5556

56-
def start(self) -> "ServerContainer":
57+
def start(self) -> Self:
5758
super().start()
5859
self._connect()
5960
return self

src/testcontainers/community/generic/sql.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from typing import Any, Optional
33
from urllib.parse import quote, urlencode
44

5+
from typing_extensions import Self
6+
57
from testcontainers.core.container import DockerContainer
68
from testcontainers.core.exceptions import ContainerStartException
79
from testcontainers.core.waiting_utils import WaitStrategy
@@ -83,7 +85,7 @@ def _create_connection_url(
8385

8486
return url
8587

86-
def start(self) -> "SqlContainer":
88+
def start(self) -> Self:
8789
"""
8890
Start the database container and perform initialization.
8991

src/testcontainers/community/influxdb/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from requests import get
1717
from requests.exceptions import ConnectionError, ReadTimeout
18+
from typing_extensions import Self
1819

1920
from testcontainers.core.container import DockerContainer
2021
from testcontainers.core.waiting_utils import wait_container_is_ready
@@ -74,7 +75,7 @@ def get_influxdb_version(self) -> str:
7475

7576
return self._health_check().get("version")
7677

77-
def start(self) -> "InfluxDbContainer":
78+
def start(self) -> Self:
7879
"""
7980
Spawns a container of the InfluxDB Docker image, ready to be used.
8081
"""

src/testcontainers/community/influxdb/version1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Optional
1515

1616
from influxdb import InfluxDBClient
17+
from typing_extensions import Self
1718

1819
from .base import InfluxDbContainer
1920

@@ -58,7 +59,7 @@ def get_client(self, **client_kwargs):
5859

5960
return InfluxDBClient(self.get_container_host_ip(), self.get_exposed_port(self.container_port), **client_kwargs)
6061

61-
def start(self) -> "InfluxDb1Container":
62+
def start(self) -> Self:
6263
"""
6364
Overridden for better typing reason
6465
"""

src/testcontainers/community/influxdb/version2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Optional
1616

1717
from influxdb_client import InfluxDBClient, Organization
18+
from typing_extensions import Self
1819

1920
from .base import InfluxDbContainer
2021

@@ -70,7 +71,7 @@ def __init__(
7071
if env_value:
7172
self.with_env(env_key, env_value)
7273

73-
def start(self) -> "InfluxDb2Container":
74+
def start(self) -> Self:
7475
"""
7576
Overridden for better typing reason
7677
"""

src/testcontainers/community/k3s/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
import logging
1515

16+
from typing_extensions import Self
17+
1618
from testcontainers.core.config import testcontainers_config
1719
from testcontainers.core.container import DockerContainer
1820
from testcontainers.core.waiting_utils import wait_for_logs
@@ -55,7 +57,7 @@ def __init__(self, image="rancher/k3s:latest", enable_cgroup_mount=True, **kwarg
5557
def _connect(self) -> None:
5658
wait_for_logs(self, predicate="Node controller sync successful", timeout=testcontainers_config.timeout)
5759

58-
def start(self) -> "K3SContainer":
60+
def start(self) -> Self:
5961
super().start()
6062
self._connect()
6163
return self

src/testcontainers/community/kafka/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def tc_start(self) -> None:
183183
)
184184
self.create_file(data, KafkaContainer.TC_START_SCRIPT)
185185

186-
def start(self, timeout: int = 30) -> "KafkaContainer":
186+
def start(self, timeout: int = 30) -> Self:
187187
script = KafkaContainer.TC_START_SCRIPT
188188
command = f'sh -c "while [ ! -f {script} ]; do sleep 0.1; done; sh {script}"'
189189
self.configure()

src/testcontainers/community/kafka/_redpanda.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from io import BytesIO
66
from textwrap import dedent
77

8+
from typing_extensions import Self
9+
810
from testcontainers.core.container import DockerContainer
911
from testcontainers.core.wait_strategies import LogMessageWaitStrategy
1012

@@ -66,7 +68,7 @@ def tc_start(self) -> None:
6668

6769
self.create_file(data, RedpandaContainer.TC_START_SCRIPT)
6870

69-
def start(self, timeout=10) -> "RedpandaContainer":
71+
def start(self, timeout=10) -> Self:
7072
script = RedpandaContainer.TC_START_SCRIPT
7173
command = f'-c "while [ ! -f {script} ]; do sleep 0.1; done; sh {script}"'
7274
self.with_command(command)

0 commit comments

Comments
 (0)