-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[docker] Fix Container.attach() return type
#15155
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
This comment has been minimized.
This comment has been minimized.
srittau
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good! A suggestion for the tests, though.
| def check_attach_stream(c: Container) -> None: | ||
| for line in c.attach(stdout=True, stderr=True, stream=True, logs=True): | ||
| line.decode("utf-8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test can be simplified and "typified", and extended:
| def check_attach_stream(c: Container) -> None: | |
| for line in c.attach(stdout=True, stderr=True, stream=True, logs=True): | |
| line.decode("utf-8") | |
| c: Container | |
| assert_type(c.attach(), bytes) | |
| assert_type(c.attach(stream=False), bytes) | |
| for line in c.attach(stream=True): | |
| assert_type(line, bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the review! The suggested code fails pyright CI because c: Container without assignment is seen as an unbound variable. Pyright reports: "c" is unbound (reportUnboundVariable). To fix this, we need to wrap it in a function with a parameter, which is how other tests handle cases where instances can't be easily constructed:
def check_attach(c: Container) -> None:
assert_type(c.attach(), bytes)
assert_type(c.attach(stream=False), bytes)
for line in c.attach(stream=True):
assert_type(line, bytes)Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
PR Summary
The
Container.attach()method was incorrectly typed as returningstr | tuple[str | None, str | None] | ...but it actually returnsbytes. Whenstream=True, it returns aCancellableStream[bytes]that yieldsbytesobjects on iteration. This caused false positive errors when users called.decode()on the output, since mypy thought the values werestr(which has no.decode()method). So this PR adds proper overloads for bothContainer.attach()andContainerApiMixin.attach()based on thestreamanddemuxparameters:stream=False, demux=False→bytesstream=False, demux=True→tuple[bytes | None, bytes | None]stream=True, demux=False→CancellableStream[bytes]stream=True, demux=True→CancellableStream[tuple[bytes | None, bytes | None]]Please note that tests follow the "less is more" rule of the the project so minimal.
Fixes #15143