docker-stubs incorrectly annotates `stderr` attribute of `ContainerError` as `str`, whilst it is actually `bytes`
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 1.7k
- Avg merge
- 13d 8h
- Merged PRs (30d)
- 2
Description
% 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]
...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The relevant declarations are in errors.pyi, and the runtime value is passed from container.logs in containers.py. Compare the annotation and constructor signature with the shown call path, then verify the example with mypy. Done means ContainerError.stderr is accepted as bytes or None without the false-positive decode error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, python
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100