Skip to content

Commit 9a17529

Browse files
[docker] Fix Container.attach() return type (#15155)
1 parent f8b61f6 commit 9a17529

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from __future__ import annotations
2+
3+
from typing_extensions import assert_type
4+
5+
from docker.models.containers import Container
6+
7+
8+
def check_attach(c: Container) -> None:
9+
assert_type(c.attach(), bytes)
10+
assert_type(c.attach(stream=False), bytes)
11+
for line in c.attach(stream=True):
12+
assert_type(line, bytes)

stubs/docker/docker/api/container.pyi

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,49 @@ class _TopResult(TypedDict):
2424
_Container: TypeAlias = _HasId | _HasID | str
2525

2626
class ContainerApiMixin:
27+
@overload
2728
def attach(
2829
self,
2930
container: _Container,
3031
stdout: bool = True,
3132
stderr: bool = True,
32-
stream: bool = False,
33+
stream: Literal[False] = False,
3334
logs: bool = False,
34-
demux: bool = False,
35-
): ...
35+
demux: Literal[False] = False,
36+
) -> bytes: ...
37+
@overload
38+
def attach(
39+
self,
40+
container: _Container,
41+
stdout: bool = True,
42+
stderr: bool = True,
43+
stream: Literal[False] = False,
44+
logs: bool = False,
45+
*,
46+
demux: Literal[True],
47+
) -> tuple[bytes | None, bytes | None]: ...
48+
@overload
49+
def attach(
50+
self,
51+
container: _Container,
52+
stdout: bool = True,
53+
stderr: bool = True,
54+
*,
55+
stream: Literal[True],
56+
logs: bool = False,
57+
demux: Literal[False] = False,
58+
) -> CancellableStream[bytes]: ...
59+
@overload
60+
def attach(
61+
self,
62+
container: _Container,
63+
stdout: bool = True,
64+
stderr: bool = True,
65+
*,
66+
stream: Literal[True],
67+
logs: bool = False,
68+
demux: Literal[True],
69+
) -> CancellableStream[tuple[bytes | None, bytes | None]]: ...
3670
def attach_socket(self, container: _Container, params=None, ws: bool = False): ...
3771
def commit(
3872
self,

stubs/docker/docker/models/containers.pyi

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,40 @@ class Container(Model):
3939
def health(self) -> str: ...
4040
@property
4141
def ports(self) -> dict[Incomplete, Incomplete]: ...
42+
@overload
43+
def attach(
44+
self,
45+
*,
46+
stdout: bool = True,
47+
stderr: bool = True,
48+
stream: Literal[False] = False,
49+
logs: bool = False,
50+
demux: Literal[False] = False,
51+
) -> bytes: ...
52+
@overload
53+
def attach(
54+
self,
55+
*,
56+
stdout: bool = True,
57+
stderr: bool = True,
58+
stream: Literal[False] = False,
59+
logs: bool = False,
60+
demux: Literal[True],
61+
) -> tuple[bytes | None, bytes | None]: ...
62+
@overload
63+
def attach(
64+
self,
65+
*,
66+
stdout: bool = True,
67+
stderr: bool = True,
68+
stream: Literal[True],
69+
logs: bool = False,
70+
demux: Literal[False] = False,
71+
) -> CancellableStream[bytes]: ...
72+
@overload
4273
def attach(
43-
self, **kwargs
44-
) -> str | tuple[str | None, str | None] | CancellableStream[str] | CancellableStream[tuple[str | None, str | None]]: ...
74+
self, *, stdout: bool = True, stderr: bool = True, stream: Literal[True], logs: bool = False, demux: Literal[True]
75+
) -> CancellableStream[tuple[bytes | None, bytes | None]]: ...
4576
def attach_socket(self, **kwargs) -> SocketIO | _BufferedReaderStream | SSHSocket: ...
4677
def commit(self, repository: str | None = None, tag: str | None = None, **kwargs) -> Image: ...
4778
def diff(self) -> list[dict[str, Incomplete]]: ...

0 commit comments

Comments
 (0)