Skip to content

Commit 0042bcc

Browse files
donBarbosAvasam
andauthored
Improve docker.types (#13809)
Co-authored-by: Avasam <samuel.06@hotmail.com>
1 parent a46eea7 commit 0042bcc

File tree

8 files changed

+204
-166
lines changed

8 files changed

+204
-166
lines changed

stubs/docker/docker/api/container.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
from _typeshed import Incomplete
3-
from typing import Literal, TypedDict, overload, type_check_only
3+
from typing import Any, Literal, TypedDict, overload, type_check_only
44
from typing_extensions import TypeAlias
55

66
from docker._types import WaitContainerResponse
@@ -61,7 +61,9 @@ class ContainerApiMixin:
6161
detach: bool = False,
6262
stdin_open: bool = False,
6363
tty: bool = False,
64-
ports: list[int] | None = None,
64+
# list is invariant, enumerating all possible union combination would be too complex for:
65+
# list[str | int | tuple[int | str, str] | tuple[int | str, ...]]
66+
ports: dict[str, dict[Incomplete, Incomplete]] | list[Any] | None = None,
6567
environment: dict[str, str] | list[str] | None = None,
6668
volumes: str | list[str] | None = None,
6769
network_disabled: bool = False,

stubs/docker/docker/api/network.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Iterable
23
from typing import Any, Literal, TypedDict, type_check_only
34
from typing_extensions import TypeAlias
45

@@ -43,7 +44,7 @@ class NetworkApiMixin:
4344
ipv4_address: Incomplete | None = None,
4445
ipv6_address: Incomplete | None = None,
4546
aliases: Incomplete | None = None,
46-
links: Incomplete | None = None,
47+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
4748
link_local_ips: Incomplete | None = None,
4849
driver_opt: Incomplete | None = None,
4950
mac_address: Incomplete | None = None,

stubs/docker/docker/models/containers.pyi

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from _typeshed import Incomplete
3+
from collections.abc import Iterable, Mapping
34
from typing import Literal, NamedTuple, TypedDict, overload
45
from typing_extensions import NotRequired
56

@@ -152,7 +153,7 @@ class ContainerCollection(Collection[Container]):
152153
entrypoint: str | list[str] | None = None,
153154
environment: dict[str, str] | list[str] | None = None,
154155
extra_hosts: dict[str, str] | None = None,
155-
group_add: list[str | int] | None = None,
156+
group_add: Iterable[str | int] | None = None,
156157
healthcheck: dict[Incomplete, Incomplete] | None = None,
157158
hostname: str | None = None,
158159
init: bool | None = None,
@@ -161,7 +162,7 @@ class ContainerCollection(Collection[Container]):
161162
isolation: str | None = None,
162163
kernel_memory: str | int | None = None,
163164
labels: dict[str, str] | list[str] | None = None,
164-
links: dict[str, str | None] | None = None,
165+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
165166
log_config: LogConfig | None = None,
166167
lxc_conf: dict[Incomplete, Incomplete] | None = None,
167168
mac_address: str | None = None,
@@ -181,7 +182,7 @@ class ContainerCollection(Collection[Container]):
181182
pid_mode: str | None = None,
182183
pids_limit: int | None = None,
183184
platform: str | None = None,
184-
ports: dict[str, int | list[int] | tuple[str, int] | None] | None = None,
185+
ports: Mapping[str, int | list[int] | tuple[str, int] | None] | None = None,
185186
privileged: bool = False,
186187
publish_all_ports: bool = False,
187188
read_only: bool | None = None,
@@ -247,7 +248,7 @@ class ContainerCollection(Collection[Container]):
247248
entrypoint: str | list[str] | None = None,
248249
environment: dict[str, str] | list[str] | None = None,
249250
extra_hosts: dict[str, str] | None = None,
250-
group_add: list[str | int] | None = None,
251+
group_add: Iterable[str | int] | None = None,
251252
healthcheck: dict[Incomplete, Incomplete] | None = None,
252253
hostname: str | None = None,
253254
init: bool | None = None,
@@ -256,7 +257,7 @@ class ContainerCollection(Collection[Container]):
256257
isolation: str | None = None,
257258
kernel_memory: str | int | None = None,
258259
labels: dict[str, str] | list[str] | None = None,
259-
links: dict[str, str | None] | None = None,
260+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
260261
log_config: LogConfig | None = None,
261262
lxc_conf: dict[Incomplete, Incomplete] | None = None,
262263
mac_address: str | None = None,
@@ -276,7 +277,7 @@ class ContainerCollection(Collection[Container]):
276277
pid_mode: str | None = None,
277278
pids_limit: int | None = None,
278279
platform: str | None = None,
279-
ports: dict[str, int | list[int] | tuple[str, int] | None] | None = None,
280+
ports: Mapping[str, int | list[int] | tuple[str, int] | None] | None = None,
280281
privileged: bool = False,
281282
publish_all_ports: bool = False,
282283
read_only: bool | None = None,
@@ -338,7 +339,7 @@ class ContainerCollection(Collection[Container]):
338339
entrypoint: str | list[str] | None = None,
339340
environment: dict[str, str] | list[str] | None = None,
340341
extra_hosts: dict[str, str] | None = None,
341-
group_add: list[str | int] | None = None,
342+
group_add: Iterable[str | int] | None = None,
342343
healthcheck: dict[Incomplete, Incomplete] | None = None,
343344
hostname: str | None = None,
344345
init: bool | None = None,
@@ -347,7 +348,7 @@ class ContainerCollection(Collection[Container]):
347348
isolation: str | None = None,
348349
kernel_memory: str | int | None = None,
349350
labels: dict[str, str] | list[str] | None = None,
350-
links: dict[str, str | None] | None = None,
351+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
351352
log_config: LogConfig | None = None,
352353
lxc_conf: dict[Incomplete, Incomplete] | None = None,
353354
mac_address: str | None = None,
@@ -367,7 +368,7 @@ class ContainerCollection(Collection[Container]):
367368
pid_mode: str | None = None,
368369
pids_limit: int | None = None,
369370
platform: str | None = None,
370-
ports: dict[str, int | list[int] | tuple[str, int] | None] | None = None,
371+
ports: Mapping[str, int | list[int] | tuple[str, int] | None] | None = None,
371372
privileged: bool = False,
372373
publish_all_ports: bool = False,
373374
read_only: bool | None = None,

stubs/docker/docker/types/containers.pyi

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
from _typeshed import Incomplete
2-
from typing import Literal
2+
from collections.abc import Iterable, Mapping
3+
from typing import Any, Final, Literal
34

45
from docker._types import ContainerWeightDevice
56

7+
from .. import errors
68
from .base import DictType
9+
from .healthcheck import Healthcheck
10+
from .networks import NetworkingConfig
711
from .services import Mount
812

913
class LogConfigTypesEnum:
10-
JSON: Incomplete
11-
SYSLOG: Incomplete
12-
JOURNALD: Incomplete
13-
GELF: Incomplete
14-
FLUENTD: Incomplete
15-
NONE: Incomplete
14+
JSON: Final = "json-file"
15+
SYSLOG: Final = "syslog"
16+
JOURNALD: Final = "journald"
17+
GELF: Final = "gelf"
18+
FLUENTD: Final = "fluentd"
19+
NONE: Final = "none"
1620

1721
class LogConfig(DictType):
1822
types: type[LogConfigTypesEnum]
@@ -68,21 +72,21 @@ class HostConfig(dict[str, Incomplete]):
6872
def __init__(
6973
self,
7074
version: str,
71-
binds: Incomplete | None = None,
72-
port_bindings: Incomplete | None = None,
73-
lxc_conf: dict[Incomplete, Incomplete] | None = None,
75+
binds: dict[str, Mapping[str, str]] | list[str] | None = None,
76+
port_bindings: Mapping[int | str, Incomplete] | None = None,
77+
lxc_conf: dict[str, Incomplete] | list[dict[str, Incomplete]] | None = None,
7478
publish_all_ports: bool = False,
75-
links: dict[str, str | None] | None = None,
79+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
7680
privileged: bool = False,
77-
dns: list[Incomplete] | None = None,
78-
dns_search: list[Incomplete] | None = None,
81+
dns: list[str] | None = None,
82+
dns_search: list[str] | None = None,
7983
volumes_from: list[str] | None = None,
8084
network_mode: str | None = None,
81-
restart_policy: dict[Incomplete, Incomplete] | None = None,
85+
restart_policy: Mapping[str, str | int] | None = None,
8286
cap_add: list[str] | None = None,
8387
cap_drop: list[str] | None = None,
8488
devices: list[str] | None = None,
85-
extra_hosts: dict[Incomplete, Incomplete] | None = None,
89+
extra_hosts: dict[str, Incomplete] | list[Incomplete] | None = None,
8690
read_only: bool | None = None,
8791
pid_mode: str | None = None,
8892
ipc_mode: str | None = None,
@@ -95,18 +99,18 @@ class HostConfig(dict[str, Incomplete]):
9599
kernel_memory: str | int | None = None,
96100
mem_swappiness: int | None = None,
97101
cgroup_parent: str | None = None,
98-
group_add: list[str | int] | None = None,
102+
group_add: Iterable[str | int] | None = None,
99103
cpu_quota: int | None = None,
100104
cpu_period: int | None = None,
101105
blkio_weight: int | None = None,
102106
blkio_weight_device: list[ContainerWeightDevice] | None = None,
103-
device_read_bps: Incomplete | None = None,
104-
device_write_bps: Incomplete | None = None,
105-
device_read_iops: Incomplete | None = None,
106-
device_write_iops: Incomplete | None = None,
107+
device_read_bps: list[Mapping[str, str | int]] | None = None,
108+
device_write_bps: list[Mapping[str, str | int]] | None = None,
109+
device_read_iops: list[Mapping[str, str | int]] | None = None,
110+
device_write_iops: list[Mapping[str, str | int]] | None = None,
107111
oom_kill_disable: bool = False,
108112
shm_size: str | int | None = None,
109-
sysctls: dict[Incomplete, Incomplete] | None = None,
113+
sysctls: dict[str, str] | None = None,
110114
tmpfs: dict[str, str] | None = None,
111115
oom_score_adj: int | None = None,
112116
dns_opt: list[Incomplete] | None = None,
@@ -134,35 +138,37 @@ class HostConfig(dict[str, Incomplete]):
134138
cgroupns: Literal["private", "host"] | None = None,
135139
) -> None: ...
136140

137-
def host_config_type_error(param, param_value, expected): ...
138-
def host_config_version_error(param, version, less_than: bool = True): ...
139-
def host_config_value_error(param, param_value): ...
140-
def host_config_incompatible_error(param, param_value, incompatible_param): ...
141+
def host_config_type_error(param: str, param_value: object, expected: str) -> TypeError: ...
142+
def host_config_version_error(param: str, version: str, less_than: bool = True) -> errors.InvalidVersion: ...
143+
def host_config_value_error(param: str, param_value: object) -> ValueError: ...
144+
def host_config_incompatible_error(param: str, param_value: str, incompatible_param: str) -> errors.InvalidArgument: ...
141145

142146
class ContainerConfig(dict[str, Incomplete]):
143147
def __init__(
144148
self,
145149
version: str,
146-
image,
150+
image: str,
147151
command: str | list[str],
148152
hostname: str | None = None,
149153
user: str | int | None = None,
150154
detach: bool = False,
151155
stdin_open: bool = False,
152156
tty: bool = False,
153-
ports: dict[str, int | list[int] | tuple[str, int] | None] | None = None,
157+
# list is invariant, enumerating all possible union combination would be too complex for:
158+
# list[str | int | tuple[int | str, str] | tuple[int | str, ...]]
159+
ports: dict[str, dict[Incomplete, Incomplete]] | list[Any] | None = None,
154160
environment: dict[str, str] | list[str] | None = None,
155161
volumes: str | list[str] | None = None,
156162
network_disabled: bool = False,
157163
entrypoint: str | list[str] | None = None,
158164
working_dir: str | None = None,
159165
domainname: str | None = None,
160-
host_config: Incomplete | None = None,
166+
host_config: HostConfig | None = None,
161167
mac_address: str | None = None,
162168
labels: dict[str, str] | list[str] | None = None,
163169
stop_signal: str | None = None,
164-
networking_config: Incomplete | None = None,
165-
healthcheck: Incomplete | None = None,
170+
networking_config: NetworkingConfig | None = None,
171+
healthcheck: Healthcheck | None = None,
166172
stop_timeout: int | None = None,
167173
runtime: str | None = None,
168174
) -> None: ...
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
from _typeshed import Incomplete
2+
from collections.abc import Iterable
23

34
class EndpointConfig(dict[str, Incomplete]):
45
def __init__(
56
self,
6-
version,
7-
aliases: Incomplete | None = None,
8-
links: Incomplete | None = None,
9-
ipv4_address: Incomplete | None = None,
10-
ipv6_address: Incomplete | None = None,
11-
link_local_ips: Incomplete | None = None,
7+
version: str,
8+
aliases: list[Incomplete] | None = None,
9+
links: dict[str, str] | dict[str, None] | dict[str, str | None] | Iterable[tuple[str, str | None]] | None = None,
10+
ipv4_address: str | None = None,
11+
ipv6_address: str | None = None,
12+
link_local_ips: list[str] | None = None,
1213
driver_opt: Incomplete | None = None,
13-
mac_address: Incomplete | None = None,
14+
mac_address: str | None = None,
1415
) -> None: ...
1516

1617
class NetworkingConfig(dict[str, Incomplete]):
17-
def __init__(self, endpoints_config: Incomplete | None = None) -> None: ...
18+
def __init__(self, endpoints_config: EndpointConfig | None = None) -> None: ...
1819

1920
class IPAMConfig(dict[str, Incomplete]):
2021
def __init__(
21-
self, driver: str = "default", pool_configs: Incomplete | None = None, options: Incomplete | None = None
22+
self,
23+
driver: str = "default",
24+
pool_configs: list[IPAMPool] | None = None,
25+
options: dict[Incomplete, Incomplete] | None = None,
2226
) -> None: ...
2327

2428
class IPAMPool(dict[str, Incomplete]):
2529
def __init__(
2630
self,
27-
subnet: Incomplete | None = None,
28-
iprange: Incomplete | None = None,
29-
gateway: Incomplete | None = None,
30-
aux_addresses: Incomplete | None = None,
31+
subnet: str | None = None,
32+
iprange: str | None = None,
33+
gateway: str | None = None,
34+
aux_addresses: dict[str, str] | None = None,
3135
) -> None: ...

0 commit comments

Comments
 (0)