Skip to content

Commit

Permalink
Disambiguate shadowed builtins without API break
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
inknos committed Dec 3, 2024
1 parent a49e497 commit f1a0193
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 6 additions & 4 deletions podman/domain/images_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

logger = logging.getLogger("podman.images")

type builtin_list = list


class ImagesManager(BuildMixin, Manager):
"""Specialized Manager for Image resources."""
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion podman/domain/pods_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

logger = logging.getLogger("podman.pods")

type builtin_list = list


class PodsManager(Manager):
"""Specialized Manager for Pod resources."""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f1a0193

Please sign in to comment.