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
b6464dbed9/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)
```
This commit is contained in:
Adam Dangoor
2024-05-18 13:26:17 +01:00
committed by GitHub
parent 916e05ae33
commit 55b552121b
2 changed files with 19 additions and 11 deletions

View File

@@ -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): ...

View File

@@ -1,10 +1,18 @@
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]: ...