-
Notifications
You must be signed in to change notification settings - Fork 107
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
Allow List[str]
in DockerContainerListFilters['filters']
and format_dict_for_cli()
#590
Conversation
…mat_dict_for_cli()`
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.
Hey @LooLzzz thanks for the contribution :) you've got some failures in the CI, but there's also a comment I had about the approach which it'd be good to get thoughts from @gabrieldemarmiesse on.
@@ -58,7 +58,7 @@ | |||
{ | |||
"id": str, | |||
"name": str, | |||
"label": str, | |||
"label": Union[str, List[str]], |
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.
The problem I have with this is it special-cases label
, when actually it's presumably valid to specify any of the filter types more than once? It seems like conceptually what we want is to allow specifying the same key twice, but that's not possible with normal dicts in python of course.
Any thoughts @gabrieldemarmiesse on how to resolve this? One option would be to accept a list of 2-tuples instead of a dict, i.e. something like docker.container.list(filters: Sequence[DockerContainerListFilters])
where DockerContainerListFilters = tuple[Literal["id"], str] | tuple[Literal["name"], str] | ...
.
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.
I think to be backward compatible (and also because it's easy to write), we can apply the Union[str, List[str]]
to all values in the TypedDict. So what's done in this PR, but for all elements. What do you think?
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.
What I don't like about that is it's not truly mirroring the CLI (you don't specify --filter label=foo,bar
), so it feels like sacrificing the quality of the API for minor backwards compatibility (could easily enough continue to support the old form until v1.0 if we wanted). Perhaps not all fields would make sense to be specified more than once, is that something we'd need to think about in your suggested approach?
A couple of other notes on this API:
- Why is there a
name
field, should that bereference
? (https://docs.docker.com/reference/cli/docker/image/ls/#filter, https://docs.podman.io/en/stable/markdown/podman-images.1.html#filter-f-filter) - Podman also supports
--filter label!=foo
, which the current API does not support
[EDIT: realised I was looking at the image list APIs rather than container list, oops]
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.
That would be worth a redesign from scratch possibly... As we're pre-1.0 I believe we can afford it
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.
Maybe chained calls would suite this API better?
Many ORMs are using this style of API
docker.container.list()
.filter({'label': foo})
.filter({'label': bar})
The actual evaluation is only performed when some magic method is called,
like __repr__
/ __str__
/ __list__
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.
The lazy evaluation approach has downsides that make me hesitant:
- implementation complexity
- cognitive overhead caused by the fact that under certain APIs the actual command gets run at expected times
I still think we should go for:
docker.container.list(filters=[
("label", "foo"),
("label", "bar=1"),
("label!", "foo=0"),
("status", "created"),
("status", "running"),
("name", "prefix-*"),
])
(Note that the CLI is a bit confusing here in that label filters are ANDed together whereas status filters are ORed together).
Tracking the core of the issue at #629 |
Closing in favour of #635 - thanks for bringing this issue up, it'll be good to have this solved properly. |
The docker cli allows for multiple
--filter
args to be passed,this PR aims to imitate that.
https://docs.docker.com/reference/cli/docker/container/ls/#filter