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

Add return type to Docker Container.logs #11888

Merged
merged 5 commits into from
May 12, 2024

Conversation

adamtheturtle
Copy link
Contributor

The documentation at
https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.logs says "generator or str", but docker/docker-py#2240 will change this to say "generator of bytes or bytes".

The documentation at
https://docker-py.readthedocs.io/en/stable/containers.html#docker.models.containers.Container.logs
says "generator or str", but docker/docker-py#2240
will change this to say "generator of bytes or bytes".

This comment has been minimized.

@@ -77,7 +78,7 @@ class ContainerApiMixin:
since: Incomplete | None = None,
follow: Incomplete | None = None,
until: Incomplete | None = None,
): ...
) -> Generator[bytes, None, None] | bytes: ...
Copy link
Collaborator

@srittau srittau May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it would make sense to use an overload based on stream and demux:

  • Return CancellableStream if stream is True.
  • Return Generator[tuple[bytes | None, bytes | None], None, None] if demux is True.
  • Return Generator[bytes, None, None] otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I'll have a go at creating an overload - though this is something I have not done before.

I believe demux is not related to this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srittau - I have made an attempt to do this here (but not yet in the other file). I found it challenging to know what to do wrt e.g. an argument with a default followed by an argument without a default. I'd appreciate some tips / specific guidance on this change, and then I can apply it to the other file and hopefully to other changes I make to this repository.

Copy link
Collaborator

@srittau srittau May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguments with defaults following arguments with defaults are a bit cumbersome, unfortunately. You need two overloads for that, one where all previous parameters are required, and one using the star import.

    @overload
    def logs(
        self,
        container,
        stdout: bool = True,
        stderr: bool = True,
        *,
        stream: True,
        timestamps: bool = False,
        tail: str = "all",
        since: Incomplete | None = None,
        follow: Incomplete | None = None,
        until: Incomplete | None = None,
    ) -> CancellableStream: ...
    @overload
    def logs(
        self,
        container,
        stdout: bool,
        stderr: bool,
        stream: True,
        timestamps: bool = False,
        tail: str = "all",
        since: Incomplete | None = None,
        follow: Incomplete | None = None,
        until: Incomplete | None = None,
    ) -> CancellableStream: ...
    @overload
    def logs(
        self,
        container,
        stdout: bool = True,
        stderr: bool = True,
        stream: False = False,
        timestamps: bool = False,
        tail: str = "all",
        since: Incomplete | None = None,
        follow: Incomplete | None = None,
        until: Incomplete | None = None,
    ) -> bytes: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I just noticed: tail should be annotated as Literal["all"] | int, but that may be outside the scope of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I just noticed: tail should be annotated as Literal["all"] | int, but that may be outside the scope of this PR.

I've submitted that change in #11906 and I will block this PR on that one to avoid conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srittau I'm working on the assumption that where you put e.g. stream: True,, that should be stream: Literal[True],

This comment has been minimized.

container,
stdout: bool = True,
stderr: bool = True,
*,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This * in particular I wasn't sure about. Maybe instead I should remove the = True on stdout and stderr?

This comment has been minimized.

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit 932311d into python:main May 12, 2024
42 checks passed
@adamtheturtle adamtheturtle deleted the types-docker-container-logs branch May 12, 2024 11:41
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

Successfully merging this pull request may close these issues.

2 participants