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 types for Docker client from_env kwargs #11989

Merged
merged 5 commits into from
May 22, 2024
Merged

Conversation

adamtheturtle
Copy link
Contributor

No description provided.

version: NotRequired[str]
timeout: NotRequired[int]
max_pool_size: NotRequired[int]
environment: NotRequired[_Environ]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose to create / use the _Environ protocol here.
The documentation says:

            environment (dict): The environment to read environment variables
                from. Default: the value of ``os.environ``

And the implementation includes:

    if not environment:
        environment = os.environ

I therefore considered dict[str, str], but I thought it best to allow users to pass in os.environ.

This comment has been minimized.

class DockerClient:
api: APIClient
def __init__(self, *args, **kwargs) -> None: ...
@classmethod
def from_env(cls, **kwargs) -> DockerClient: ...
def from_env(cls, **kwargs: Unpack[_FromEnvDict]) -> DockerClient: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's probably easier to just list the allowed arguments here, instead of using Unpack. This is how we usually handle cases like this in typeshed.

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 for the speedy response (as often!).

Would that mean, for example: def from_env(cls, version: str | None = None, ...)?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Exactly.

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 made this change - however, unlike my sample above, I used a * to mark the arguments as keyword-only. This was to avoid errors like error: docker.client.DockerClient.from_env is inconsistent, runtime does not have argument "use_ssh_client". Maybe you forgot to make it keyword-only in the stub? in CI.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point, using the star is correct, of course.

@@ -12,11 +13,25 @@ from docker.models.services import ServiceCollection
from docker.models.swarm import Swarm
from docker.models.volumes import VolumeCollection

@type_check_only
class _Environ(Protocol):
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 is copied from:

typeshed/stdlib/cgi.pyi

Lines 35 to 37 in a375953

class _Environ(Protocol):
def __getitem__(self, k: str, /) -> str: ...
def keys(self) -> Iterable[str]: ...

adamtheturtle and others added 2 commits May 21, 2024 06:33
Without this, we get errors like:

```
error: docker.client.DockerClient.from_env is inconsistent, runtime does not have argument "use_ssh_client". Maybe you forgot to make it keyword-only in the stub?
```

This comment has been minimized.

stubs/docker/docker/client.pyi Outdated Show resolved Hide resolved
Co-authored-by: Sebastian Rittau <srittau@rittau.biz>
Copy link
Contributor

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

@srittau srittau merged commit e1bb4ed into python:main May 22, 2024
48 checks passed
@adamtheturtle adamtheturtle deleted the from-env branch May 22, 2024 22:26
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