From 2e4013ba3dee802b8280591bec1df629beed09d3 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Sat, 20 Dec 2025 16:00:37 +0200 Subject: [PATCH 1/3] [docker] Fix `Container.attach()` return type Signed-off-by: Emmanuel Ferdman --- .../docker/@tests/test_cases/check_attach.py | 8 ++++ stubs/docker/docker/api/container.pyi | 40 +++++++++++++++++-- stubs/docker/docker/models/containers.pyi | 35 +++++++++++++++- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 stubs/docker/@tests/test_cases/check_attach.py diff --git a/stubs/docker/@tests/test_cases/check_attach.py b/stubs/docker/@tests/test_cases/check_attach.py new file mode 100644 index 000000000000..6709aa49f0a6 --- /dev/null +++ b/stubs/docker/@tests/test_cases/check_attach.py @@ -0,0 +1,8 @@ +from __future__ import annotations + +from docker.models.containers import Container + + +def check_attach_stream(c: Container) -> None: + for line in c.attach(stdout=True, stderr=True, stream=True, logs=True): + line.decode("utf-8") diff --git a/stubs/docker/docker/api/container.pyi b/stubs/docker/docker/api/container.pyi index f8e225493ee6..fb676cbb3e0c 100644 --- a/stubs/docker/docker/api/container.pyi +++ b/stubs/docker/docker/api/container.pyi @@ -24,15 +24,49 @@ class _TopResult(TypedDict): _Container: TypeAlias = _HasId | _HasID | str class ContainerApiMixin: + @overload def attach( self, container: _Container, stdout: bool = True, stderr: bool = True, - stream: bool = False, + stream: Literal[False] = False, logs: bool = False, - demux: bool = False, - ): ... + demux: Literal[False] = False, + ) -> bytes: ... + @overload + def attach( + self, + container: _Container, + stdout: bool = True, + stderr: bool = True, + stream: Literal[False] = False, + logs: bool = False, + *, + demux: Literal[True], + ) -> tuple[bytes | None, bytes | None]: ... + @overload + def attach( + self, + container: _Container, + stdout: bool = True, + stderr: bool = True, + *, + stream: Literal[True], + logs: bool = False, + demux: Literal[False] = False, + ) -> CancellableStream[bytes]: ... + @overload + def attach( + self, + container: _Container, + stdout: bool = True, + stderr: bool = True, + *, + stream: Literal[True], + logs: bool = False, + demux: Literal[True], + ) -> CancellableStream[tuple[bytes | None, bytes | None]]: ... def attach_socket(self, container: _Container, params=None, ws: bool = False): ... def commit( self, diff --git a/stubs/docker/docker/models/containers.pyi b/stubs/docker/docker/models/containers.pyi index 67097d2cc6bd..4bb709249df7 100644 --- a/stubs/docker/docker/models/containers.pyi +++ b/stubs/docker/docker/models/containers.pyi @@ -39,9 +39,40 @@ class Container(Model): def health(self) -> str: ... @property def ports(self) -> dict[Incomplete, Incomplete]: ... + @overload + def attach( + self, + *, + stdout: bool = True, + stderr: bool = True, + stream: Literal[False] = False, + logs: bool = False, + demux: Literal[False] = False, + ) -> bytes: ... + @overload + def attach( + self, + *, + stdout: bool = True, + stderr: bool = True, + stream: Literal[False] = False, + logs: bool = False, + demux: Literal[True], + ) -> tuple[bytes | None, bytes | None]: ... + @overload + def attach( + self, + *, + stdout: bool = True, + stderr: bool = True, + stream: Literal[True], + logs: bool = False, + demux: Literal[False] = False, + ) -> CancellableStream[bytes]: ... + @overload def attach( - self, **kwargs - ) -> str | tuple[str | None, str | None] | CancellableStream[str] | CancellableStream[tuple[str | None, str | None]]: ... + self, *, stdout: bool = True, stderr: bool = True, stream: Literal[True], logs: bool = False, demux: Literal[True] + ) -> CancellableStream[tuple[bytes | None, bytes | None]]: ... def attach_socket(self, **kwargs) -> SocketIO | _BufferedReaderStream | SSHSocket: ... def commit(self, repository: str | None = None, tag: str | None = None, **kwargs) -> Image: ... def diff(self) -> list[dict[str, Incomplete]]: ... From 2f21e9a8b36df8e6fa697148e843dfd1249f1e20 Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Sun, 21 Dec 2025 00:10:52 +0200 Subject: [PATCH 2/3] [docker] Fix `Container.attach()` return type Signed-off-by: Emmanuel Ferdman --- stubs/docker/@tests/test_cases/check_attach.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stubs/docker/@tests/test_cases/check_attach.py b/stubs/docker/@tests/test_cases/check_attach.py index 6709aa49f0a6..41ebbbc7338b 100644 --- a/stubs/docker/@tests/test_cases/check_attach.py +++ b/stubs/docker/@tests/test_cases/check_attach.py @@ -1,8 +1,11 @@ from __future__ import annotations -from docker.models.containers import Container +from typing_extensions import assert_type +from docker.models.containers import Container -def check_attach_stream(c: Container) -> None: - for line in c.attach(stdout=True, stderr=True, stream=True, logs=True): - line.decode("utf-8") +c: Container +assert_type(c.attach(), bytes) +assert_type(c.attach(stream=False), bytes) +for line in c.attach(stream=True): + assert_type(line, bytes) From ac1daa005e7ed29480bc9c1aa0e8fd4d8fac2faf Mon Sep 17 00:00:00 2001 From: Emmanuel Ferdman Date: Sun, 21 Dec 2025 00:16:09 +0200 Subject: [PATCH 3/3] [docker] Fix `Container.attach()` return type Signed-off-by: Emmanuel Ferdman --- stubs/docker/@tests/test_cases/check_attach.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/stubs/docker/@tests/test_cases/check_attach.py b/stubs/docker/@tests/test_cases/check_attach.py index 41ebbbc7338b..802099b9e2c2 100644 --- a/stubs/docker/@tests/test_cases/check_attach.py +++ b/stubs/docker/@tests/test_cases/check_attach.py @@ -4,8 +4,9 @@ from docker.models.containers import Container -c: Container -assert_type(c.attach(), bytes) -assert_type(c.attach(stream=False), bytes) -for line in c.attach(stream=True): - assert_type(line, bytes) + +def check_attach(c: Container) -> None: + assert_type(c.attach(), bytes) + assert_type(c.attach(stream=False), bytes) + for line in c.attach(stream=True): + assert_type(line, bytes)