Skip to content

Commit c607901

Browse files
committed
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) ```
1 parent a86115a commit c607901

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

stubs/docker/docker/errors.pyi

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

55
from docker.models.containers import Container
66
from requests import HTTPError, Response
@@ -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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
from _typeshed import Incomplete
1+
import json
22
from collections.abc import Generator
3+
from typing import Any, Callable, Iterator, TypeAlias
34

4-
json_decoder: Incomplete
5+
json_decoder: json.JSONDecoder
56

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

0 commit comments

Comments
 (0)