Skip to content

docker-stubs incorrectly annotates stderr attribute of ContainerError as str, whilst it is actually bytes #3301

@shomeax

Description

@shomeax
 % pip list | grep docker
docker                        7.1.0
types-docker                  7.1.0.20240827

from errors.pyi:

class ContainerError(DockerException):
    container: Container
    exit_status: Incomplete
    command: Incomplete
    image: Incomplete
    stderr: str | None
    def __init__(self, container: Container, exit_status, command, image, stderr: str | None) -> None: ...

While actually, the only source of ContainerError is the following code in containers.py:

...

        out = None
        if logging_driver == 'json-file' or logging_driver == 'journald':
            out = container.logs(
                stdout=stdout, stderr=stderr, stream=True, follow=True
            )

        exit_status = container.wait()['StatusCode']
        if exit_status != 0:
            out = None
            if not kwargs.get('auto_remove'):
                out = container.logs(stdout=False, stderr=True)

        if remove:
            container.remove()
        if exit_status != 0:
            raise ContainerError(
                container, exit_status, command, image, out
            )
...

But, container.logs returns bytes, not string, BUG.

That causes mypy false positive in perfectly correct code:

    try:
        result = client.containers.run(
            "alpine",
            f'sh -c "echo hello"',
            remove=True,
            stdout=True,
            stderr=True,
        )
        for line in result.decode("utf-8").split("\n"):
            print(line)
    except ContainerError as exc:
        if exc.stderr:
            for line in exc.stderr.decode("utf-8").split("\n"):
                print(line)
% mypy .
...
test.py: error: "str" has no attribute "decode"; maybe "encode"?  [attr-defined]
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions