From f1a01938e5577d615685afd29a00d9fa4df40a20 Mon Sep 17 00:00:00 2001 From: Nicola Sella Date: Mon, 2 Dec 2024 19:53:31 +0100 Subject: [PATCH] Disambiguate shadowed builtins without API break This suppresses A003 for `list` builtin. `typing.List`, `typing.Dict`, `typing.Tuple` and `typing.Type` are deprecated. Removing these annotations breaks the calls to `list` when they are done within the same class scope, which makes them ambiguous. Typed returns `list` resolve to the function `list` defined in the class, shadowing the builtin function. This change is not great but a proper one would require changing the name of the class function `list` and breaking the API to be fixed. Example of where it breaks: podman/domains/images_manager.py class ImagesManager(...): def list(...): ... def pull( self, ... ) -> Image | list[Image], [[str]]: ... Here, the typed annotation of `pull` would resolve to the `list` method, rather than the builtin. Signed-off-by: Nicola Sella --- podman/domain/images_manager.py | 10 ++++++---- podman/domain/pods_manager.py | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/podman/domain/images_manager.py b/podman/domain/images_manager.py index 985213fe..794268b8 100644 --- a/podman/domain/images_manager.py +++ b/podman/domain/images_manager.py @@ -32,6 +32,8 @@ logger = logging.getLogger("podman.images") +type builtin_list = list + class ImagesManager(BuildMixin, Manager): """Specialized Manager for Image resources.""" @@ -287,7 +289,7 @@ def push( @staticmethod def _push_helper( - decode: bool, body: list[dict[str, Any]] + decode: bool, body: builtin_list[dict[str, Any]] ) -> Iterator[Union[str, dict[str, Any]]]: """Helper needed to allow push() to return either a generator or a str.""" for entry in body: @@ -303,7 +305,7 @@ def pull( tag: Optional[str] = None, all_tags: bool = False, **kwargs, - ) -> Union[Image, list[Image], Iterator[str]]: + ) -> Union[Image, builtin_list[Image], Iterator[str]]: """Request Podman service to pull image(s) from repository. Args: @@ -446,7 +448,7 @@ def remove( image: Union[Image, str], force: Optional[bool] = None, noprune: bool = False, # pylint: disable=unused-argument - ) -> list[dict[Literal["Deleted", "Untagged", "Errors", "ExitCode"], Union[str, int]]]: + ) -> builtin_list[dict[Literal["Deleted", "Untagged", "Errors", "ExitCode"], Union[str, int]]]: """Delete image from Podman service. Args: @@ -473,7 +475,7 @@ def remove( results.append({"ExitCode": body["ExitCode"]}) return results - def search(self, term: str, **kwargs) -> list[dict[str, Any]]: + def search(self, term: str, **kwargs) -> builtin_list[dict[str, Any]]: """Search Images on registries. Args: diff --git a/podman/domain/pods_manager.py b/podman/domain/pods_manager.py index 9b398a6d..b79c871d 100644 --- a/podman/domain/pods_manager.py +++ b/podman/domain/pods_manager.py @@ -11,6 +11,8 @@ logger = logging.getLogger("podman.pods") +type builtin_list = list + class PodsManager(Manager): """Specialized Manager for Pod resources.""" @@ -129,7 +131,7 @@ def remove(self, pod_id: Union[Pod, str], force: Optional[bool] = None) -> None: response = self.client.delete(f"/pods/{pod_id}", params={"force": force}) response.raise_for_status() - def stats(self, **kwargs) -> list[dict[str, Any]] | [list[dict[str, Any]]]: + def stats(self, **kwargs) -> builtin_list[dict[str, Any]] | [builtin_list[dict[str, Any]]]: """Resource usage statistics for the containers in pods. Keyword Args: