Skip to content

Commit fd155cb

Browse files
authored
Improve docker.utils (#13808)
1 parent b0238c5 commit fd155cb

File tree

10 files changed

+188
-112
lines changed

10 files changed

+188
-112
lines changed

stubs/docker/docker/__init__.pyi

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from typing import Final
2+
13
from .api import APIClient as APIClient
24
from .client import DockerClient as DockerClient, from_env as from_env
35
from .context import Context as Context, ContextAPI as ContextAPI
46
from .tls import TLSConfig as TLSConfig
57
from .version import __version__ as __version__
68

7-
__title__: str
9+
__title__: Final[str]

stubs/docker/docker/auth.pyi

+38-25
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import FileDescriptorOrPath, Incomplete, ReadableBuffer
2+
from collections.abc import Mapping, MutableMapping
3+
from logging import Logger
4+
from typing import Final
5+
from typing_extensions import Self
26

3-
INDEX_NAME: str
4-
INDEX_URL: Incomplete
5-
TOKEN_USERNAME: str
6-
log: Incomplete
7+
INDEX_NAME: Final[str]
8+
INDEX_URL: Final[str]
9+
TOKEN_USERNAME: Final[str]
10+
log: Logger
711

8-
def resolve_repository_name(repo_name): ...
9-
def resolve_index_name(index_name): ...
10-
def get_config_header(client, registry): ...
11-
def split_repo_name(repo_name): ...
12-
def get_credential_store(authconfig, registry): ...
12+
def resolve_repository_name(repo_name: str) -> tuple[str, str]: ...
13+
def resolve_index_name(index_name: str) -> str: ...
14+
def get_config_header(client, registry) -> bytes | None: ...
15+
def split_repo_name(repo_name: str) -> tuple[str, str]: ...
16+
def get_credential_store(authconfig: AuthConfig | MutableMapping[str, Incomplete], registry: str | None): ...
1317

1418
class AuthConfig(dict[str, Incomplete]):
15-
def __init__(self, dct, credstore_env: Incomplete | None = None) -> None: ...
19+
def __init__(self, dct: MutableMapping[str, Incomplete], credstore_env: Incomplete | None = None) -> None: ...
1620
@classmethod
17-
def parse_auth(cls, entries, raise_on_error: bool = False): ...
21+
def parse_auth(
22+
cls, entries: Mapping[str, dict[Incomplete, Incomplete]], raise_on_error: bool = False
23+
) -> dict[str, Incomplete]: ...
1824
@classmethod
19-
def load_config(cls, config_path, config_dict, credstore_env: Incomplete | None = None): ...
25+
def load_config(
26+
cls,
27+
config_path: FileDescriptorOrPath | None,
28+
config_dict: dict[str, Incomplete] | None,
29+
credstore_env: Incomplete | None = None,
30+
) -> Self: ...
2031
@property
21-
def auths(self): ...
32+
def auths(self) -> dict[str, Incomplete]: ...
2233
@property
2334
def creds_store(self): ...
2435
@property
2536
def cred_helpers(self): ...
2637
@property
27-
def is_empty(self): ...
28-
def resolve_authconfig(self, registry: Incomplete | None = None): ...
29-
def get_credential_store(self, registry): ...
38+
def is_empty(self) -> bool: ...
39+
def resolve_authconfig(self, registry: str | None = None): ...
40+
def get_credential_store(self, registry: str | None): ...
3041
def get_all_credentials(self): ...
31-
def add_auth(self, reg, data) -> None: ...
42+
def add_auth(self, reg: str, data) -> None: ...
3243

33-
def resolve_authconfig(authconfig, registry: Incomplete | None = None, credstore_env: Incomplete | None = None): ...
34-
def convert_to_hostname(url): ...
35-
def decode_auth(auth): ...
36-
def encode_header(auth): ...
37-
def parse_auth(entries, raise_on_error: bool = False): ...
44+
def resolve_authconfig(authconfig, registry: str | None = None, credstore_env: Incomplete | None = None): ...
45+
def convert_to_hostname(url: str) -> str: ...
46+
def decode_auth(auth: str | ReadableBuffer) -> tuple[str, str]: ...
47+
def encode_header(auth) -> bytes: ...
48+
def parse_auth(entries: Mapping[str, dict[Incomplete, Incomplete]], raise_on_error: bool = False): ...
3849
def load_config(
39-
config_path: Incomplete | None = None, config_dict: Incomplete | None = None, credstore_env: Incomplete | None = None
40-
): ...
50+
config_path: FileDescriptorOrPath | None = None,
51+
config_dict: dict[str, Incomplete] | None = None,
52+
credstore_env: Incomplete | None = None,
53+
) -> AuthConfig: ...

stubs/docker/docker/utils/build.pyi

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
from _typeshed import Incomplete
1+
import io
2+
from _typeshed import Incomplete, StrOrBytesPath, StrPath
3+
from collections.abc import Generator, Iterable, MutableSequence
4+
from os import PathLike
5+
from tarfile import _Fileobj
6+
from tempfile import _TemporaryFileWrapper
27

38
def match_tag(tag: str) -> bool: ...
49
def tar(
5-
path,
6-
exclude: Incomplete | None = None,
7-
dockerfile: Incomplete | None = None,
8-
fileobj: Incomplete | None = None,
10+
path: PathLike[str],
11+
exclude: list[str] | None = None,
12+
dockerfile: tuple[str | None, str | None] | None = None,
13+
fileobj: _Fileobj | None = None,
914
gzip: bool = False,
10-
): ...
11-
def exclude_paths(root, patterns, dockerfile: Incomplete | None = None): ...
12-
def build_file_list(root): ...
15+
) -> _TemporaryFileWrapper[bytes] | _Fileobj: ...
16+
def exclude_paths(root: StrPath, patterns: MutableSequence[str], dockerfile: str | None = None) -> set[str]: ...
17+
def build_file_list(root: str) -> list[str]: ...
1318
def create_archive(
14-
root,
15-
files: Incomplete | None = None,
16-
fileobj: Incomplete | None = None,
19+
root: str,
20+
files: Iterable[str] | None = None,
21+
fileobj: _Fileobj | None = None,
1722
gzip: bool = False,
1823
extra_files: Incomplete | None = None,
19-
): ...
20-
def mkbuildcontext(dockerfile): ...
21-
def split_path(p): ...
22-
def normalize_slashes(p): ...
23-
def walk(root, patterns, default: bool = True): ...
24+
) -> _TemporaryFileWrapper[bytes] | _Fileobj: ...
25+
def mkbuildcontext(dockerfile: io.IOBase | StrOrBytesPath) -> _TemporaryFileWrapper[bytes]: ...
26+
def split_path(p: str) -> list[str]: ...
27+
def normalize_slashes(p: str) -> str: ...
28+
def walk(root: StrPath, patterns: Iterable[str], default: bool = True) -> Generator[str]: ...
2429

2530
class PatternMatcher:
26-
patterns: Incomplete
27-
def __init__(self, patterns) -> None: ...
28-
def matches(self, filepath): ...
29-
def walk(self, root): ...
31+
patterns: list[Pattern]
32+
def __init__(self, patterns: Iterable[str]) -> None: ...
33+
def matches(self, filepath: PathLike[str]) -> bool: ...
34+
def walk(self, root: StrPath) -> Generator[str]: ...
3035

3136
class Pattern:
3237
exclusion: bool
33-
dirs: Incomplete
34-
cleaned_pattern: Incomplete
35-
def __init__(self, pattern_str) -> None: ...
38+
dirs: list[str]
39+
cleaned_pattern: str
40+
def __init__(self, pattern_str: str) -> None: ...
3641
@classmethod
37-
def normalize(cls, p): ...
38-
def match(self, filepath): ...
42+
def normalize(cls, p: str) -> list[str]: ...
43+
def match(self, filepath: str) -> bool: ...

stubs/docker/docker/utils/config.pyi

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from _typeshed import Incomplete
1+
from _typeshed import FileDescriptorOrPath
2+
from logging import Logger
3+
from typing import Final
24

3-
DOCKER_CONFIG_FILENAME: Incomplete
4-
LEGACY_DOCKER_CONFIG_FILENAME: str
5-
log: Incomplete
5+
DOCKER_CONFIG_FILENAME: Final[str]
6+
LEGACY_DOCKER_CONFIG_FILENAME: Final[str]
7+
log: Logger
68

7-
def find_config_file(config_path: Incomplete | None = None): ...
8-
def config_path_from_environment(): ...
9-
def home_dir(): ...
10-
def load_general_config(config_path: Incomplete | None = None): ...
9+
def find_config_file(config_path: FileDescriptorOrPath | None = None) -> FileDescriptorOrPath | None: ...
10+
def config_path_from_environment() -> str | None: ...
11+
def home_dir() -> str: ...
12+
def load_general_config(config_path: FileDescriptorOrPath | None = None): ...
+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
def check_resource(resource_name): ...
2-
def minimum_version(version): ...
3-
def update_headers(f): ...
1+
from _typeshed import Incomplete
2+
from collections.abc import Callable
3+
4+
def check_resource(resource_name: str): ...
5+
def minimum_version(version: str): ...
6+
def update_headers(f: Callable[..., Incomplete]): ...

stubs/docker/docker/utils/fnmatch.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__all__ = ["fnmatch", "fnmatchcase", "translate"]
22

3-
def fnmatch(name, pat): ...
4-
def fnmatchcase(name, pat): ...
5-
def translate(pat): ...
3+
def fnmatch(name: str, pat: str) -> bool: ...
4+
def fnmatchcase(name: str, pat: str) -> bool: ...
5+
def translate(pat: str) -> str: ...

stubs/docker/docker/utils/json_stream.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ from docker._types import JSON
66

77
json_decoder: json.JSONDecoder
88

9-
def stream_as_text(stream: Iterator[str | bytes]) -> Generator[str, None, None]: ...
9+
def stream_as_text(stream: Iterator[str | bytes]) -> Generator[str]: ...
1010
def json_splitter(buffer: str) -> tuple[JSON, str] | None: ...
11-
def json_stream(stream: Iterator[str]) -> Generator[JSON, None, None]: ...
11+
def json_stream(stream: Iterator[str]) -> Generator[JSON]: ...
1212
def line_splitter(buffer: str, separator: str = "\n") -> tuple[str, str] | None: ...
1313
def split_buffer(
1414
stream: Iterator[str | bytes], splitter: Callable[[str], tuple[str, str]] | None = None, decoder: Callable[[str], Any] = ...
15-
) -> Generator[Any, None, None]: ...
15+
) -> Generator[Any]: ...

stubs/docker/docker/utils/ports.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import re
12
from _typeshed import Incomplete
3+
from typing import Final
24

3-
PORT_SPEC: Incomplete
5+
PORT_SPEC: Final[re.Pattern[str]]
46

57
def add_port_mapping(port_bindings, internal_port, external) -> None: ...
68
def add_port(port_bindings, internal_port_range, external_range) -> None: ...
7-
def build_port_bindings(ports): ...
9+
def build_port_bindings(ports) -> dict[Incomplete, Incomplete]: ...
810
def port_range(start, end, proto, randomly_available_port: bool = False): ...
9-
def split_port(port): ...
11+
def split_port(port: object) -> tuple[Incomplete, Incomplete]: ...

stubs/docker/docker/utils/socket.pyi

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Generator
1+
from _typeshed import Incomplete, ReadableBuffer
2+
from collections.abc import Generator, Iterable
3+
from typing import Final, Literal, TypeVar, overload
34

4-
STDOUT: int
5-
STDERR: int
5+
_T = TypeVar("_T")
6+
7+
STDOUT: Final = 1
8+
STDERR: Final = 2
69

710
class SocketError(Exception): ...
811

9-
NPIPE_ENDED: int
12+
NPIPE_ENDED: Final = 109
1013

1114
def read(socket, n: int = 4096): ...
12-
def read_exactly(socket, n): ...
13-
def next_frame_header(socket): ...
15+
def read_exactly(socket, n: int) -> bytes: ...
16+
def next_frame_header(socket) -> tuple[Incomplete, int]: ...
1417
def frames_iter(socket, tty): ...
15-
def frames_iter_no_tty(socket) -> Generator[Incomplete, None, None]: ...
16-
def frames_iter_tty(socket) -> Generator[Incomplete, None, None]: ...
17-
def consume_socket_output(frames, demux: bool = False): ...
18-
def demux_adaptor(stream_id, data): ...
18+
def frames_iter_no_tty(socket) -> Generator[tuple[str | Incomplete, str | bytes | Incomplete]]: ...
19+
def frames_iter_tty(socket) -> Generator[Incomplete]: ...
20+
@overload
21+
def consume_socket_output(
22+
frames: Iterable[tuple[Incomplete, Incomplete]], demux: Literal[True]
23+
) -> tuple[Incomplete, Incomplete]: ...
24+
@overload
25+
def consume_socket_output(frames: Iterable[ReadableBuffer], demux: Literal[False] = False) -> bytes: ...
26+
@overload
27+
def demux_adaptor(stream_id: Literal[1], data: _T) -> tuple[_T, None]: ...
28+
@overload
29+
def demux_adaptor(stream_id: Literal[2], data: _T) -> tuple[None, _T]: ...

stubs/docker/docker/utils/utils.pyi

+68-30
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,72 @@
1-
from _typeshed import Incomplete
2-
from typing import NamedTuple
1+
import datetime
2+
from _typeshed import FileDescriptorOrPath, Incomplete, ReadableBuffer
3+
from collections.abc import Iterable, Mapping
4+
from shlex import _ShlexInstream
5+
from typing import Literal, NamedTuple, NoReturn, TypedDict, TypeVar, overload, type_check_only
6+
from typing_extensions import deprecated
7+
8+
from ..tls import TLSConfig
9+
10+
_T = TypeVar("_T")
11+
_K = TypeVar("_K")
12+
_V = TypeVar("_V")
13+
14+
@type_check_only
15+
class _EnvKWArgs(TypedDict, total=False):
16+
base_url: str
17+
tls: TLSConfig
318

419
class URLComponents(NamedTuple):
5-
scheme: Incomplete
6-
netloc: Incomplete
7-
url: Incomplete
8-
params: Incomplete
9-
query: Incomplete
10-
fragment: Incomplete
20+
scheme: str | None
21+
netloc: str | None
22+
url: str
23+
params: str | None
24+
query: str | None
25+
fragment: str | None
1126

12-
def create_ipam_pool(*args, **kwargs) -> None: ...
13-
def create_ipam_config(*args, **kwargs) -> None: ...
14-
def decode_json_header(header): ...
15-
def compare_version(v1, v2): ...
16-
def version_lt(v1, v2): ...
17-
def version_gte(v1, v2): ...
18-
def convert_port_bindings(port_bindings): ...
19-
def convert_volume_binds(binds): ...
20-
def convert_tmpfs_mounts(tmpfs): ...
21-
def convert_service_networks(networks): ...
22-
def parse_repository_tag(repo_name): ...
23-
def parse_host(addr, is_win32: bool = False, tls: bool = False): ...
24-
def parse_devices(devices): ...
25-
def kwargs_from_env(environment: Incomplete | None = None): ...
26-
def convert_filters(filters): ...
27-
def datetime_to_timestamp(dt): ...
28-
def parse_bytes(s): ...
27+
@deprecated("utils.create_ipam_pool has been removed. Please use a docker.types.IPAMPool object instead.")
28+
def create_ipam_pool(*args, **kwargs) -> NoReturn: ...
29+
@deprecated("utils.create_ipam_config has been removed. Please use a docker.types.IPAMConfig object instead.")
30+
def create_ipam_config(*args, **kwargs) -> NoReturn: ...
31+
def decode_json_header(header: str | ReadableBuffer): ...
32+
def compare_version(v1: str, v2: str) -> Literal[0, -1, 1]: ...
33+
def version_lt(v1: str, v2: str) -> bool: ...
34+
def version_gte(v1: str, v2: str) -> bool: ...
35+
def convert_port_bindings(
36+
port_bindings: Mapping[object, Incomplete], # keys are converted using str()
37+
) -> dict[str, list[dict[str, str]]]: ...
38+
@overload
39+
def convert_volume_binds(binds: list[_T]) -> list[_T]: ...
40+
@overload
41+
def convert_volume_binds(binds: Mapping[str | bytes, Incomplete]) -> list[str]: ...
42+
@overload
43+
def convert_tmpfs_mounts(tmpfs: dict[_K, _V]) -> dict[_K, _V]: ...
44+
@overload
45+
def convert_tmpfs_mounts(tmpfs: list[str]) -> dict[str, str]: ...
46+
@overload
47+
def convert_service_networks(networks: None) -> None: ...
48+
@overload
49+
def convert_service_networks(networks: list[str] | list[dict[str, str]] | list[str | dict[str, str]]) -> list[dict[str, str]]: ...
50+
def parse_repository_tag(repo_name: str) -> tuple[str, str | None]: ...
51+
@overload
52+
def parse_host(addr: None, is_win32: Literal[True], tls: bool = False) -> Literal["npipe:////./pipe/docker_engine"]: ...
53+
@overload
54+
def parse_host(
55+
addr: None, is_win32: Literal[False] = False, tls: bool = False
56+
) -> Literal["http+unix:///var/run/docker.sock"]: ...
57+
@overload
58+
def parse_host(addr: str | None, is_win32: bool = False, tls: bool = False) -> str | bytes: ...
59+
def parse_devices(devices: Iterable[str | dict[str, Incomplete]]) -> list[dict[str, Incomplete]]: ...
60+
def kwargs_from_env(environment: Mapping[str, Incomplete] | None = None) -> _EnvKWArgs: ...
61+
def convert_filters(filters) -> str: ...
62+
def datetime_to_timestamp(dt: datetime.datetime) -> int: ...
63+
def parse_bytes(s: float | str) -> float: ...
2964
def normalize_links(links): ...
30-
def parse_env_file(env_file): ...
31-
def split_command(command): ...
32-
def format_environment(environment): ...
33-
def format_extra_hosts(extra_hosts, task: bool = False): ...
34-
def create_host_config(self, *args, **kwargs) -> None: ...
65+
def parse_env_file(env_file: FileDescriptorOrPath) -> dict[str, str]: ...
66+
def split_command(command: str | _ShlexInstream) -> list[str]: ...
67+
def format_environment(environment: Mapping[str, object | None]) -> list[str]: ...
68+
def format_extra_hosts(
69+
extra_hosts: Mapping[object, object], task: bool = False # keys and values are converted to str
70+
) -> list[str]: ...
71+
@deprecated("utils.create_host_config has been removed. Please use a docker.types.HostConfig object instead.")
72+
def create_host_config(self, *args, **kwargs) -> NoReturn: ...

0 commit comments

Comments
 (0)