Skip to content

Commit 55b5521

Browse files
Fix type of Docker BuildError.build_log (#11917)
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) ```
1 parent 916e05a commit 55b5521

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

stubs/docker/docker/errors.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from _typeshed import Incomplete
2-
from collections.abc import Mapping
2+
from collections.abc import Iterator, Mapping
33
from typing import NoReturn
44

55
from docker.models.containers import Container
@@ -47,8 +47,8 @@ class StreamParseError(RuntimeError):
4747

4848
class BuildError(DockerException):
4949
msg: str
50-
build_log: str
51-
def __init__(self, reason: str, build_log: str) -> None: ...
50+
build_log: Iterator[dict[str, str]]
51+
def __init__(self, reason: str, build_log: Iterator[dict[str, str]]) -> None: ...
5252

5353
class ImageLoadError(DockerException): ...
5454

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
from _typeshed import Incomplete
2-
from collections.abc import Generator
1+
import json
2+
from collections.abc import Callable, Generator, Iterator
3+
from typing import Any
4+
from typing_extensions import TypeAlias
35

4-
json_decoder: Incomplete
6+
json_decoder: json.JSONDecoder
57

6-
def stream_as_text(stream) -> Generator[Incomplete, None, None]: ...
7-
def json_splitter(buffer): ...
8-
def json_stream(stream): ...
9-
def line_splitter(buffer, separator: str = "\n"): ...
10-
def split_buffer(stream, splitter: Incomplete | None = None, decoder=...) -> Generator[Incomplete, None, Incomplete]: ...
8+
# Type alias for JSON, explained at:
9+
# https://github.com/python/typing/issues/182#issuecomment-1320974824.
10+
_JSON: TypeAlias = dict[str, _JSON] | list[_JSON] | str | int | float | bool | None
11+
12+
def stream_as_text(stream: Iterator[str | bytes]) -> Generator[str, None, None]: ...
13+
def json_splitter(buffer: str) -> tuple[_JSON, str] | None: ...
14+
def json_stream(stream: Iterator[str]) -> Generator[_JSON, None, None]: ...
15+
def line_splitter(buffer: str, separator: str = "\n") -> tuple[str, str] | None: ...
16+
def split_buffer(
17+
stream: Iterator[str | bytes], splitter: Callable[[str], tuple[str, str]] | None = None, decoder: Callable[[str], Any] = ...
18+
) -> Generator[Any, None, None]: ...

0 commit comments

Comments
 (0)