From 116247a35f138264ce5b90273cddfd49c6efd812 Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Wed, 15 May 2024 20:07:36 +0100 Subject: [PATCH 1/2] Fix type of Docker BuildError.build_log In working this out I also had a go at changing the json_stream functions used to create every BuildError in docker-py. There are two `BuildError`s raised in docker-py, both in https://github.com/docker/docker-py/blob/b6464dbed92b14b2c61d5ee49805fce041a3e083/docker/models/images.py#L304-L315 ```python result_stream, internal_stream = itertools.tee(json_stream(resp)) for chunk in internal_stream: if 'error' in chunk: raise BuildError(chunk['error'], result_stream) if 'stream' in chunk: match = re.search( r'(^Successfully built |sha256:)([0-9a-f]+)$', chunk['stream'] ) if match: image_id = match.group(2) last_event = chunk if image_id: return (self.get(image_id), result_stream) raise BuildError(last_event or 'Unknown', result_stream) ``` --- stubs/docker/docker/errors.pyi | 6 +++--- stubs/docker/docker/utils/json_stream.pyi | 26 ++++++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/stubs/docker/docker/errors.pyi b/stubs/docker/docker/errors.pyi index fa443f0ff0e3..49e22900297a 100644 --- a/stubs/docker/docker/errors.pyi +++ b/stubs/docker/docker/errors.pyi @@ -1,5 +1,5 @@ from _typeshed import Incomplete -from collections.abc import Mapping +from collections.abc import Iterator, Mapping from typing import NoReturn from docker.models.containers import Container @@ -47,8 +47,8 @@ class StreamParseError(RuntimeError): class BuildError(DockerException): msg: str - build_log: str - def __init__(self, reason: str, build_log: str) -> None: ... + build_log: Iterator[dict[str, str]] + def __init__(self, reason: str, build_log: Iterator[dict[str, str]]) -> None: ... class ImageLoadError(DockerException): ... diff --git a/stubs/docker/docker/utils/json_stream.pyi b/stubs/docker/docker/utils/json_stream.pyi index a5c4ce4010a3..075b629ac077 100644 --- a/stubs/docker/docker/utils/json_stream.pyi +++ b/stubs/docker/docker/utils/json_stream.pyi @@ -1,10 +1,20 @@ -from _typeshed import Incomplete -from collections.abc import Generator +import json +from collections.abc import Callable, Generator, Iterator +from typing import Any +from typing_extensions import TypeAlias -json_decoder: Incomplete +json_decoder: json.JSONDecoder -def stream_as_text(stream) -> Generator[Incomplete, None, None]: ... -def json_splitter(buffer): ... -def json_stream(stream): ... -def line_splitter(buffer, separator: str = "\n"): ... -def split_buffer(stream, splitter: Incomplete | None = None, decoder=...) -> Generator[Incomplete, None, Incomplete]: ... +# Type alias for JSON, explained at: +# https://github.com/python/typing/issues/182#issuecomment-1320974824. +_JSON: TypeAlias = dict[str, _JSON] | list[_JSON] | str | int | float | bool | None + +def stream_as_text(stream: Iterator[str | bytes]) -> Generator[str, None, None]: ... +def json_splitter(buffer: str) -> tuple[_JSON, str] | None: ... +def json_stream(stream: Iterator[str]) -> Generator[_JSON, None, None]: ... +def line_splitter(buffer: str, separator: str = "\n") -> tuple[str, str] | None: ... +def split_buffer( + stream: Iterator[str | bytes], + splitter: Callable[[str], tuple[str, str]] | None = None, + decoder: Callable[[str], Any] = ..., +) -> Generator[Any, None, None]: ... From 5142c3736e8d2955d16ac70982f5efcaa5cf34c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 19:50:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stubs/docker/docker/utils/json_stream.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stubs/docker/docker/utils/json_stream.pyi b/stubs/docker/docker/utils/json_stream.pyi index 075b629ac077..39ddd9d18cb9 100644 --- a/stubs/docker/docker/utils/json_stream.pyi +++ b/stubs/docker/docker/utils/json_stream.pyi @@ -14,7 +14,5 @@ def json_splitter(buffer: str) -> tuple[_JSON, str] | None: ... def json_stream(stream: Iterator[str]) -> Generator[_JSON, None, None]: ... def line_splitter(buffer: str, separator: str = "\n") -> tuple[str, str] | None: ... def split_buffer( - stream: Iterator[str | bytes], - splitter: Callable[[str], tuple[str, str]] | None = None, - decoder: Callable[[str], Any] = ..., + stream: Iterator[str | bytes], splitter: Callable[[str], tuple[str, str]] | None = None, decoder: Callable[[str], Any] = ... ) -> Generator[Any, None, None]: ...