Add kwarg types to Docker container run and create (#12216)

This commit is contained in:
Adam Dangoor
2024-06-27 09:11:47 +01:00
committed by GitHub
parent 2d9b0fd2a0
commit ea869e2959
3 changed files with 272 additions and 12 deletions

View File

@@ -1,6 +1,7 @@
# Internal-use module for types shared by multiple modules.
# This does not match a module in docker-py.
from pathlib import Path
from typing import TypedDict, type_check_only
from typing_extensions import NotRequired, TypeAlias
@@ -8,6 +9,11 @@ from typing_extensions import NotRequired, TypeAlias
# https://github.com/python/typing/issues/182#issuecomment-1320974824.
JSON: TypeAlias = dict[str, JSON] | list[JSON] | str | int | float | bool | None
@type_check_only
class ContainerWeightDevice(TypedDict):
Path: Path
Weight: int
# See https://docs.docker.com/engine/api/v1.42/#tag/Container/operation/ContainerWait
@type_check_only
class _WaitErrorDetails(TypedDict):

View File

@@ -2,8 +2,11 @@ import datetime
from _typeshed import Incomplete
from typing import Literal, NamedTuple, overload
from docker._types import WaitContainerResponse
from docker._types import ContainerWeightDevice, WaitContainerResponse
from docker.types import EndpointConfig
from docker.types.containers import DeviceRequest, LogConfig, Ulimit
from docker.types.daemon import CancellableStream
from docker.types.services import Mount
from .images import Image
from .resource import Collection, Model
@@ -113,9 +116,92 @@ class ContainerCollection(Collection[Container]):
stderr: bool = False,
remove: bool = False,
*,
auto_remove: bool = False,
blkio_weight_device: list[ContainerWeightDevice] | None = None,
blkio_weight: int | None = None,
cap_add: list[str] | None = None,
cap_drop: list[str] | None = None,
cgroup_parent: str | None = None,
cgroupns: Literal["private", "host"] | None = None,
cpu_count: int | None = None,
cpu_percent: int | None = None,
cpu_period: int | None = None,
cpu_quota: int | None = None,
cpu_rt_period: int | None = None,
cpu_rt_runtime: int | None = None,
cpu_shares: int | None = None,
cpuset_cpus: str | None = None,
cpuset_mems: str | None = None,
detach: Literal[False] = False,
**kwargs,
) -> bytes: ...
device_cgroup_rules: list[Incomplete] | None = None,
device_read_bps: list[Incomplete] | None = None,
device_read_iops: Incomplete | None = None,
device_write_bps: Incomplete | None = None,
device_write_iops: Incomplete | None = None,
devices: list[str] | None = None,
device_requests: list[DeviceRequest] | None = None,
dns: list[Incomplete] | None = None,
dns_opt: list[Incomplete] | None = None,
dns_search: list[Incomplete] | None = None,
domainname: str | list[Incomplete] | None = None,
entrypoint: str | list[str] | None = None,
environment: dict[str, str] | list[str] | None = None,
extra_hosts: dict[str, str] | None = None,
group_add: list[str | int] | None = None,
healthcheck: dict[Incomplete, Incomplete] | None = None,
hostname: str | None = None,
init: bool | None = None,
init_path: str | None = None,
ipc_mode: str | None = None,
isolation: str | None = None,
kernel_memory: str | int | None = None,
labels: dict[str, str] | list[str] | None = None,
links: dict[str, str | None] | None = None,
log_config: LogConfig | None = None,
lxc_conf: dict[Incomplete, Incomplete] | None = None,
mac_address: str | None = None,
mem_limit: str | int | None = None,
mem_reservation: str | int | None = None,
mem_swappiness: int | None = None,
memswap_limit: str | int | None = None,
mounts: list[Mount] | None = None,
name: str,
nano_cpus: int | None = None,
network: str | None = None,
network_disabled: bool = False,
network_mode: str | None = None,
networking_config: dict[str, EndpointConfig] | None = None,
oom_kill_disable: bool = False,
oom_score_adj: int | None = None,
pid_mode: str | None = None,
pids_limit: int | None = None,
platform: str | None = None,
ports: dict[str, int | None] | None = None,
privileged: bool = False,
publish_all_ports: bool = False,
read_only: bool | None = None,
restart_policy: dict[Incomplete, Incomplete] | None = None,
runtime: str | None = None,
security_opt: list[str] | None = None,
shm_size: str | int | None = None,
stdin_open: bool = False,
stop_signal: str | None = None,
storage_opt: dict[Incomplete, Incomplete] | None = None,
stream: bool = False,
sysctls: dict[Incomplete, Incomplete] | None = None,
tmpfs: dict[str, str] | None = None,
tty: bool = False,
ulimits: list[Ulimit] | None = None,
use_config_proxy: bool | None = None,
user: str | int | None = None,
userns_mode: str | None = None,
uts_mode: str | None = None,
version: str,
volume_driver: str | None = None,
volumes: dict[str, str] | list[str] | None = None,
volumes_from: list[str] | None = None,
working_dir: str | None = None,
) -> bytes: ... # TODO: This should return a stream, if `stream` is True
@overload
def run(
self,
@@ -125,10 +211,182 @@ class ContainerCollection(Collection[Container]):
stderr: bool = False,
remove: bool = False,
*,
auto_remove: bool = False,
blkio_weight_device: list[ContainerWeightDevice] | None = None,
blkio_weight: int | None = None,
cap_add: list[str] | None = None,
cap_drop: list[str] | None = None,
cgroup_parent: str | None = None,
cgroupns: Literal["private", "host"] | None = None,
cpu_count: int | None = None,
cpu_percent: int | None = None,
cpu_period: int | None = None,
cpu_quota: int | None = None,
cpu_rt_period: int | None = None,
cpu_rt_runtime: int | None = None,
cpu_shares: int | None = None,
cpuset_cpus: str | None = None,
cpuset_mems: str | None = None,
detach: Literal[True],
**kwargs,
device_cgroup_rules: list[Incomplete] | None = None,
device_read_bps: list[Incomplete] | None = None,
device_read_iops: Incomplete | None = None,
device_write_bps: Incomplete | None = None,
device_write_iops: Incomplete | None = None,
devices: list[str] | None = None,
device_requests: list[DeviceRequest] | None = None,
dns: list[Incomplete] | None = None,
dns_opt: list[Incomplete] | None = None,
dns_search: list[Incomplete] | None = None,
domainname: str | list[Incomplete] | None = None,
entrypoint: str | list[str] | None = None,
environment: dict[str, str] | list[str] | None = None,
extra_hosts: dict[str, str] | None = None,
group_add: list[str | int] | None = None,
healthcheck: dict[Incomplete, Incomplete] | None = None,
hostname: str | None = None,
init: bool | None = None,
init_path: str | None = None,
ipc_mode: str | None = None,
isolation: str | None = None,
kernel_memory: str | int | None = None,
labels: dict[str, str] | list[str] | None = None,
links: dict[str, str | None] | None = None,
log_config: LogConfig | None = None,
lxc_conf: dict[Incomplete, Incomplete] | None = None,
mac_address: str | None = None,
mem_limit: str | int | None = None,
mem_reservation: str | int | None = None,
mem_swappiness: int | None = None,
memswap_limit: str | int | None = None,
mounts: list[Mount] | None = None,
name: str,
nano_cpus: int | None = None,
network: str | None = None,
network_disabled: bool = False,
network_mode: str | None = None,
networking_config: dict[str, EndpointConfig] | None = None,
oom_kill_disable: bool = False,
oom_score_adj: int | None = None,
pid_mode: str | None = None,
pids_limit: int | None = None,
platform: str | None = None,
ports: dict[str, int | None] | None = None,
privileged: bool = False,
publish_all_ports: bool = False,
read_only: bool | None = None,
restart_policy: dict[Incomplete, Incomplete] | None = None,
runtime: str | None = None,
security_opt: list[str] | None = None,
shm_size: str | int | None = None,
stdin_open: bool = False,
stop_signal: str | None = None,
storage_opt: dict[Incomplete, Incomplete] | None = None,
stream: bool = False,
sysctls: dict[Incomplete, Incomplete] | None = None,
tmpfs: dict[str, str] | None = None,
tty: bool = False,
ulimits: list[Ulimit] | None = None,
use_config_proxy: bool | None = None,
user: str | int | None = None,
userns_mode: str | None = None,
uts_mode: str | None = None,
version: str,
volume_driver: str | None = None,
volumes: dict[str, str] | list[str] | None = None,
volumes_from: list[str] | None = None,
working_dir: str | None = None,
) -> Container: ...
def create( # type:ignore[override]
self,
image: str | Image,
command: str | list[str] | None = None,
*,
auto_remove: bool = False,
blkio_weight_device: list[ContainerWeightDevice] | None = None,
blkio_weight: int | None = None,
cap_add: list[str] | None = None,
cap_drop: list[str] | None = None,
cgroup_parent: str | None = None,
cgroupns: Literal["private", "host"] | None = None,
cpu_count: int | None = None,
cpu_percent: int | None = None,
cpu_period: int | None = None,
cpu_quota: int | None = None,
cpu_rt_period: int | None = None,
cpu_rt_runtime: int | None = None,
cpu_shares: int | None = None,
cpuset_cpus: str | None = None,
cpuset_mems: str | None = None,
detach: Literal[True],
device_cgroup_rules: list[Incomplete] | None = None,
device_read_bps: list[Incomplete] | None = None,
device_read_iops: Incomplete | None = None,
device_write_bps: Incomplete | None = None,
device_write_iops: Incomplete | None = None,
devices: list[str] | None = None,
device_requests: list[DeviceRequest] | None = None,
dns: list[Incomplete] | None = None,
dns_opt: list[Incomplete] | None = None,
dns_search: list[Incomplete] | None = None,
domainname: str | list[Incomplete] | None = None,
entrypoint: str | list[str] | None = None,
environment: dict[str, str] | list[str] | None = None,
extra_hosts: dict[str, str] | None = None,
group_add: list[str | int] | None = None,
healthcheck: dict[Incomplete, Incomplete] | None = None,
hostname: str | None = None,
init: bool | None = None,
init_path: str | None = None,
ipc_mode: str | None = None,
isolation: str | None = None,
kernel_memory: str | int | None = None,
labels: dict[str, str] | list[str] | None = None,
links: dict[str, str | None] | None = None,
log_config: LogConfig | None = None,
lxc_conf: dict[Incomplete, Incomplete] | None = None,
mac_address: str | None = None,
mem_limit: str | int | None = None,
mem_reservation: str | int | None = None,
mem_swappiness: int | None = None,
memswap_limit: str | int | None = None,
mounts: list[Mount] | None = None,
name: str,
nano_cpus: int | None = None,
network: str | None = None,
network_disabled: bool = False,
network_mode: str | None = None,
networking_config: dict[str, EndpointConfig] | None = None,
oom_kill_disable: bool = False,
oom_score_adj: int | None = None,
pid_mode: str | None = None,
pids_limit: int | None = None,
platform: str | None = None,
ports: dict[str, int | None] | None = None,
privileged: bool = False,
publish_all_ports: bool = False,
read_only: bool | None = None,
restart_policy: dict[Incomplete, Incomplete] | None = None,
runtime: str | None = None,
security_opt: list[str] | None = None,
shm_size: str | int | None = None,
stdin_open: bool = False,
stop_signal: str | None = None,
storage_opt: dict[Incomplete, Incomplete] | None = None,
stream: bool = False,
sysctls: dict[Incomplete, Incomplete] | None = None,
tmpfs: dict[str, str] | None = None,
tty: bool = False,
ulimits: list[Ulimit] | None = None,
use_config_proxy: bool | None = None,
user: str | int | None = None,
userns_mode: str | None = None,
uts_mode: str | None = None,
volume_driver: str | None = None,
volumes: dict[str, str] | list[str] | None = None,
volumes_from: list[str] | None = None,
working_dir: str | None = None,
) -> Container: ...
def create(self, image: str, command: str | list[str] | None = None, **kwargs) -> Container: ... # type:ignore[override]
def get(self, container_id: str) -> Container: ...
def list(
self,

View File

@@ -1,15 +1,11 @@
from _typeshed import Incomplete
from pathlib import Path
from typing import Literal, TypedDict, type_check_only
from typing import Literal
from docker._types import ContainerWeightDevice
from .base import DictType
from .services import Mount
@type_check_only
class ContainerWeightDevice(TypedDict):
Path: Path
Weight: int
class LogConfigTypesEnum:
JSON: Incomplete
SYSLOG: Incomplete