Skip to content

Conversation

lev-blit
Copy link

The code that was causing issues before this was in create:

import docker

client = docker.DockerClient(base_url="my_docker_daemon_url")
image = client.images.build(...)[0]
container = client.containers.create(image)  # error: Missing named argument "detach" for "create" of "ContainerCollection"

added the fixes to run and parse_host since I saw them already, checked against https://github.com/docker/docker-py/blob/main/docker/models/containers.py and https://github.com/docker/docker-py/blob/main/docker/utils/utils.py

@lev-blit lev-blit changed the title fix unrequired literals to have a default value fix unrequired kwargs to have a default value Aug 30, 2025
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/cli/dev.py:319: error: Missing named argument "detach" for "create" of "ContainerCollection"  [call-arg]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:834: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:858: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:883: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/hybrid.py:935: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/bot.py:290: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]
- discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "name", "with_app_command"  [misc]
+ discord/ext/commands/bot.py:314: error: Overlap between argument names and ** TypedDict items: "with_app_command", "name"  [misc]

@AlexWaygood AlexWaygood changed the title fix unrequired kwargs to have a default value docker: fix unrequired kwargs to have a default value Aug 30, 2025
@@ -234,7 +234,7 @@ class ContainerCollection(Collection[Container]):
cpu_shares: int | None = None,
cpuset_cpus: str | None = None,
cpuset_mems: str | None = None,
detach: Literal[True],
detach: Literal[True] = True,
Copy link
Member

@brianschubert brianschubert Aug 31, 2025

Choose a reason for hiding this comment

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

This isn't quite right -- this is part of a pair of overloads that changes the return type depending on if detach=True is passed. Simplified:

@overload
def run(self, *, detach: Literal[False] = False) -> bytes:
@overload
def run(self, *, detach: Literal[True]) -> Container:

Supplying a default here would make the overloads overlap with incompatible return types. That's a problem, since type checkers and other tools won't know which return type to use when no argument for detach is provided.

(Ordinarily pyright would catch this, but we have that check disabled in typeshed since there's some cases where overlapping overloads are necessary; this isn't one of them).

@@ -325,7 +325,7 @@ class ContainerCollection(Collection[Container]):
cpu_shares: int | None = None,
cpuset_cpus: str | None = None,
cpuset_mems: str | None = None,
detach: Literal[True],
detach: Literal[True] = True,
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be:

Suggested change
detach: Literal[True] = True,
detach: bool = True,

Unlike run above, this method isn't overloaded over the type of detach. I also don't see anything in the source that suggests passing detach=False is invalid: https://github.com/docker/docker-py/blob/6e6a273573fe77f00776b30de0685162a102e43f/docker/models/containers.py#L918-L919

I suspect that both the type Literal[True] and the missing default value are accidental artifacts from when the keyword arguments were copied over from one of run's overloads in #12216.

Comment on lines +52 to 55
def parse_host(addr: None, is_win32: Literal[True] = True, tls: bool = False) -> Literal["npipe:////./pipe/docker_engine"]: ...
@overload
def parse_host(
addr: None, is_win32: Literal[False] = False, tls: bool = False
Copy link
Member

Choose a reason for hiding this comment

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

Same as run above; this would make the overloads overlap

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