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

✨ Added argument client_type to add custom logic later #524

Merged
merged 1 commit into from
Jan 9, 2024

Conversation

gabrieldemarmiesse
Copy link
Owner

@gabrieldemarmiesse gabrieldemarmiesse commented Jan 9, 2024

@LewisGaul that should help to do the xfail for the PR #523

The idea is that it's not required to specify it by a user, but if python-on-whales needs it at some point, we just ask the user to add it to the DockerCLient constructor.

We could call "something version" instead and try to parse the output, but this option is more explicit and faster because it doesn't require a call at every program start.

@LewisGaul
Copy link
Collaborator

I'm fine with this, although I think it might be preferable if an approach using <ctr_exe> --version could be used.

We could call "something version" instead and try to parse the output, but this option is more explicit and faster because it doesn't require a call at every program start.

Yeah this is a reasonable concern, although client_type could be a property that only runs the command on demand, perhaps caching the result for use by the version property too. Also should be very fast to run <exe> --version!

@@ -65,6 +65,7 @@ class ClientConfig:
compose_project_directory: Optional[ValidPath] = None
compose_compatibility: Optional[bool] = None
client_call: List[str] = field(default_factory=lambda: ["docker"])
client_type: Literal["docker", "podman", "nerdctl", "unknown"] = "unknown"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe should use an enum to make it clearer what's supported?

Copy link
Owner Author

@gabrieldemarmiesse gabrieldemarmiesse Jan 9, 2024

Choose a reason for hiding this comment

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

I know enum is better supported than Literal in static type checkers, but it's mostly the fault of type checkers. I expect support will get better in the future. Using strings is more beginner-friendly and works the same with a dynamic type checker like beartype.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Using enums is more friendly at least to certain users who prefer to use types as guidance for what values are allowed rather than docs/IDE support, but I'm fine with either way.

Copy link
Owner Author

Choose a reason for hiding this comment

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

The codebase uses string everywhere for this use case, there are already litterals here and there. So let's use Literal to be consistent with the rest of the codebase.

@gabrieldemarmiesse
Copy link
Owner Author

About your comment with "--version", I think we can always add this logic later if we want to. Parsing a output well is hard and not fool proof as the output might change with upgrade. We can always do a call to --version if the client_type is unknown and we want to avoid the user having to type client_type=.... For me this PR and a call to --version are not incompatible.

@@ -102,6 +109,7 @@ def __init__(
compose_compatibility: Optional[bool] = None,
client_binary: str = "docker",
client_call: List[str] = ["docker"],
client_type: Literal["docker", "podman", "nerdctl", "unknown"] = "unknown",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this intended as a public attribute of the class? (it would be helpful if it was). If so does it need adding to docs somewhere?

Copy link
Owner Author

Choose a reason for hiding this comment

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

It could be in the future. For the moment, it's not necessary for the goal of this PR.

@@ -21,7 +21,7 @@
from .exceptions import DockerException

# alias
docker = DockerClient()
docker = DockerClient(client_type="docker")
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is because client_call defaults to ["docker"] then why not have this default be set in DockerClient__init__()? Your point about the ctr exe possibly being a symlink to something else still stands such that this may be incorrect (unless it actually gets to the point where POW downloads the client).

Copy link
Owner Author

@gabrieldemarmiesse gabrieldemarmiesse Jan 9, 2024

Choose a reason for hiding this comment

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

That's true, but then the "unknown" option would never be used, and it has a purpose. It means "as a user I didn't think about it". When people do from python_on_whales import docker, we can reasonably assume that they really mean docker, even if you're right, a symlink would be annying here.

I try to follow the motto "easy things should be easy, hard things should be possible". If there is no "unknown", then it's impossible for the library to tell you that it's assuming incorrectly the type of client.

To be clear, I don't think users are reading the whole documentation when using POW, so I have to handle the case of backward compatibility + the fact that some users will use DockerClient without reading the description of every argument.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that's fine, using python_on_whales.docker just comes with the caveat of assuming if you have docker on your system it actually is a docker client, I don't have an issue with that.

@gabrieldemarmiesse
Copy link
Owner Author

Can we merge as-is? We can always improve later on if we want, by making the attribute public or by doing the --version thing.

@gabrieldemarmiesse gabrieldemarmiesse merged commit 6d68e1e into master Jan 9, 2024
35 checks passed
@gabrieldemarmiesse gabrieldemarmiesse deleted the 4a7ae510-f6af-4af6-a31d-623bfc30e5e2 branch January 9, 2024 16:54
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