Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shomeax opened this issue Nov 18, 2024 · 0 comments

Comments

@shomeax
Copy link

shomeax commented Nov 18, 2024

 % 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]
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant