Skip to content

Commit

Permalink
Add type validation
Browse files Browse the repository at this point in the history
Signed-off-by: Francis Bergin <[email protected]>
  • Loading branch information
Francis Bergin committed Jun 25, 2023
1 parent 464c105 commit 7354159
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ podman:

.PHONY: lint
lint: tox
$(PYTHON) -m tox -e black,pylint
$(PYTHON) -m tox -e black,mypy,pylint

.PHONY: tests
tests: tox
Expand Down
4 changes: 2 additions & 2 deletions podman/api/adapter_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Utility functions for working with Adapters."""
from typing import NamedTuple, Mapping
from typing import Dict, NamedTuple, Type


def _key_normalizer(key_class: NamedTuple, request_context: Mapping) -> Mapping:
def _key_normalizer(key_class: Type, request_context: Dict) -> NamedTuple:
"""Create a pool key out of a request context dictionary.
According to RFC 3986, both the scheme and host are case-insensitive.
Expand Down
2 changes: 1 addition & 1 deletion podman/api/cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
from functools import cached_property # pylint: disable=unused-import
except ImportError:

def cached_property(fn):
def cached_property(fn): # type: ignore
return property(functools.lru_cache()(fn))
2 changes: 1 addition & 1 deletion podman/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _normalize_url(base_url: str) -> urllib.parse.ParseResult:
def delete(
self,
path: Union[str, bytes],
params: Union[None, bytes, Mapping[str, str]] = None,
params: Union[None, bytes, Mapping[str, Any]] = None,
headers: Optional[Mapping[str, str]] = None,
timeout: _Timeout = None,
stream: Optional[bool] = False,
Expand Down
5 changes: 3 additions & 2 deletions podman/api/http_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Dict, List, Mapping, Optional, Union, Any


def prepare_filters(filters: Union[str, List[str], Mapping[str, str]]) -> Optional[str]:
def prepare_filters(filters: Union[str, List[str], Mapping[str, str], None]) -> Optional[str]:
"""Return filters as an URL quoted JSON Dict[str, List[Any]]."""

if filters is None or len(filters) == 0:
Expand Down Expand Up @@ -82,6 +82,7 @@ def _filter_values(mapping: Mapping[str, Any], recursion=False) -> Dict[str, Any
):
continue

proposal: Any
# depending on type we need details...
if isinstance(value, collections.abc.Mapping):
proposal = _filter_values(value, recursion=True)
Expand All @@ -98,5 +99,5 @@ def _filter_values(mapping: Mapping[str, Any], recursion=False) -> Dict[str, Any
return canonical


def encode_auth_header(auth_config: Dict[str, str]) -> str:
def encode_auth_header(auth_config: Dict[str, str]) -> bytes:
return base64.b64encode(json.dumps(auth_config).encode('utf-8'))
5 changes: 2 additions & 3 deletions podman/api/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ def decode_header(value: Optional[str]) -> Dict[str, Any]:
if value is None:
return {}

value = base64.b64decode(value)
text = value.decode("utf-8")
text = base64.b64decode(value).decode("utf-8")
return json.loads(text)


Expand All @@ -54,7 +53,7 @@ def prepare_timestamp(value: Union[datetime, int, None]) -> Optional[int]:
raise ValueError(f"Type '{type(value)}' is not supported by prepare_timestamp()")


def prepare_cidr(value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]) -> (str, str):
def prepare_cidr(value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]) -> Tuple[str, str]:
"""Returns network address and Base64 encoded netmask from CIDR.
The return values are dictated by the Go JSON decoder.
Expand Down
2 changes: 1 addition & 1 deletion podman/api/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def connect(self, **kwargs): # pylint: disable=unused-argument

super().connect(str(self.local_sock))

def send(self, data: bytes, flags=None) -> int: # pylint: disable=unused-argument
def send(self, data: bytes, flags=None) -> int: # type: ignore[override] # pylint: disable=unused-argument
"""Write data to SSH forwarded UNIX domain socket.
Args:
Expand Down
19 changes: 10 additions & 9 deletions podman/api/tar_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tarfile
import tempfile
from fnmatch import fnmatch
from typing import BinaryIO, List, Optional
from typing import List, Optional, IO, Union

import sys

Expand Down Expand Up @@ -52,8 +52,8 @@ def prepare_containerfile(anchor: str, dockerfile: str) -> str:


def create_tar(
anchor: str, name: str = None, exclude: List[str] = None, gzip: bool = False
) -> BinaryIO:
anchor: str, name: Optional[str] = None, exclude: Optional[List[str]] = None, gzip: bool = False
) -> IO[bytes]:
"""Create a tarfile from context_dir to send to Podman service.
Args:
Expand Down Expand Up @@ -96,11 +96,12 @@ def add_filter(info: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:

return info

path: Union[tempfile._TemporaryFileWrapper, pathlib.Path]
if name is None:
# pylint: disable=consider-using-with
name = tempfile.NamedTemporaryFile(prefix="podman_context", suffix=".tar")
path = tempfile.NamedTemporaryFile(prefix="podman_context", suffix=".tar")
else:
name = pathlib.Path(name)
path = pathlib.Path(name)

if exclude is None:
exclude = []
Expand All @@ -109,16 +110,16 @@ def add_filter(info: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:

# FIXME caller needs to add this...
# exclude.append(".dockerignore")
exclude.append(name.name)
exclude.append(path.name)

mode = "w:gz" if gzip else "w"
with tarfile.open(name.name, mode) as tar:
with tarfile.open(path.name, mode) as tar:
tar.add(anchor, arcname="", recursive=True, filter=add_filter)

return open(name.name, "rb") # pylint: disable=consider-using-with
return open(path.name, "rb") # pylint: disable=consider-using-with


def _exclude_matcher(path: str, exclude: List[str]) -> bool:
def _exclude_matcher(path: str, exclude: Optional[List[str]]) -> bool:
"""Returns True if path matches an entry in exclude.
Note:
Expand Down
1 change: 1 addition & 0 deletions podman/api/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Code is backup for missing typing_extensions...
# pylint: disable-all
# mypy: ignore-errors

import abc
import collections
Expand Down
6 changes: 3 additions & 3 deletions podman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ def from_env(
Raises:
ValueError when required environment variable is not set
"""
environment = environment or os.environ
environment = environment or os.environ # type: ignore[assignment]
credstore_env = credstore_env or {}

if version == "auto":
version = None
version = None # type: ignore[assignment]

host = environment.get("CONTAINER_HOST") or environment.get("DOCKER_HOST") or None
host = environment.get("CONTAINER_HOST") or environment.get("DOCKER_HOST") or None # type: ignore[union-attr]
if host is None:
raise ValueError("CONTAINER_HOST or DOCKER_HOST must be set to URL of podman service.")

Expand Down
4 changes: 2 additions & 2 deletions podman/domain/networks_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import logging
import sys
from contextlib import suppress
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

from podman import api
from podman.api import http_utils
Expand Down Expand Up @@ -186,7 +186,7 @@ def prune(

return {"NetworksDeleted": deleted, "SpaceReclaimed": 0}

def remove(self, name: [Network, str], force: Optional[bool] = None) -> None:
def remove(self, name: Union[Network, str], force: Optional[bool] = None) -> None:
"""Remove Network resource.
Args:
Expand Down
3 changes: 2 additions & 1 deletion podman/tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
import shutil
import typing
import uuid

import fixtures
Expand All @@ -33,7 +34,7 @@ class IntegrationTest(fixtures.TestWithFixtures):
results and logging captured by the unittest module test runner.
"""

podman: str = None
podman: typing.Optional[str] = None

@classmethod
def setUpClass(cls) -> None:
Expand Down
2 changes: 1 addition & 1 deletion podman/tests/integration/test_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections.abc import Iterator
except:
# Python < 3.10
from collections import Iterator
from collections import Iterator # type: ignore[attr-defined]

import podman.tests.integration.base as base
from podman import PodmanClient
Expand Down
4 changes: 2 additions & 2 deletions podman/tests/integration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(
log_level: str = "WARNING",
) -> None:
"""create a launcher and build podman command"""
podman_exe: str = podman_path
podman_exe = podman_path
if not podman_exe:
podman_exe = shutil.which('podman')
if podman_exe is None:
Expand All @@ -49,7 +49,7 @@ def __init__(
self.socket_file: str = socket_uri.replace('unix://', '')
self.log_level = log_level

self.proc = None
self.proc: Optional[subprocess.Popen] = None
self.reference_id = hash(time.monotonic())

self.cmd: List[str] = []
Expand Down
2 changes: 1 addition & 1 deletion podman/tests/unit/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Iterable
except:
# Python < 3.10
from collections import Iterable
from collections import Iterable # type: ignore[attr-defined]
from unittest.mock import patch

import requests_mock
Expand Down
2 changes: 1 addition & 1 deletion podman/tests/unit/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections.abc import Iterable
except:
# Python < 3.10
from collections import Iterable
from collections import Iterable # type: ignore[attr-defined]

import requests_mock

Expand Down
2 changes: 1 addition & 1 deletion podman/tests/unit/test_containersmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Iterator
except:
# Python < 3.10
from collections import Iterator
from collections import Iterator # type: ignore[attr-defined]

from unittest.mock import DEFAULT, patch

Expand Down
2 changes: 1 addition & 1 deletion podman/tests/unit/test_imagesmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections.abc import Iterable
except:
# Python < 3.10
from collections import Iterable
from collections import Iterable # type: ignore[attr-defined]

import requests_mock

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ exclude = '''
| hack
)/
'''
[tool.mypy]
install_types = true
non_interactive = true
ignore_missing_imports = true
[tool.isort]
profile = "black"
line_length = 100
Expand All @@ -30,4 +34,3 @@ log_cli = true
log_cli_level = "DEBUG"
log_cli_format = "%(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"

5 changes: 5 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ commands =
deps = black
commands =
black {posargs} .

[testenv:mypy]
deps = mypy
commands =
mypy --package podman

0 comments on commit 7354159

Please sign in to comment.