gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
541 stars 101 forks source link

`Container.image` could be an `Image` instance rather than string #543

Open LewisGaul opened 7 months ago

LewisGaul commented 7 months ago

The Container.image property is a string representing the image ID, rather than an Image instance. This is consistent with all fields of inspect output, which is simply parsed via pydantic, however in the case of the field being a simple ID we could use this to construct a python-on-whales object for the user's convenience.

     @property
-    def image(self) -> str:
-        return self._get_inspect_result().image
+    def image(self) -> python_on_whales.Image:
+        image_cli = python_on_whales.components.image.cli_wrapper.ImageCLI(self.client_config)
+        image_id = self._get_inspect_result().image
+        return image_cli.inspect(image_id)

The problem is backwards compatibility... Perhaps something for v1.0?

The same consideration exists for the new Container.pod and Pod.containers properties, see https://github.com/LewisGaul/python-on-whales/pull/4/commits/6ffcf45813006e90377f3054495ec16dfd879276.

gabrieldemarmiesse commented 7 months ago

Doing a docker.image.inspect() has a runtime cost. If we do it, we'll need to use the lazy loading features of python-on-whales.

I'll need to think about it, there are qui a few area that this can affect, because we could argue that the same could be done for volumes, networks, etc