From 66726b656ab8fd18a69771ff2ee2a3fd8ca959b0 Mon Sep 17 00:00:00 2001 From: Roy Moore Date: Sun, 8 Sep 2024 09:06:39 +0300 Subject: [PATCH] fix(core): Typing in auth (#691) Lets try and continue the journey from #305 (the goal is to get core marked as typed) ``` poetry run mypy --config-file pyproject.toml core/tests/test_auth.py core/testcontainers/core/auth.py Success: no issues found in 2 source files ``` --- core/testcontainers/core/auth.py | 15 +++++++++------ core/tests/test_auth.py | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/testcontainers/core/auth.py b/core/testcontainers/core/auth.py index 906dca1d..afabe9e9 100644 --- a/core/testcontainers/core/auth.py +++ b/core/testcontainers/core/auth.py @@ -2,7 +2,7 @@ import json as json from collections import namedtuple from logging import warning -from typing import Optional +from typing import Any, Optional DockerAuthInfo = namedtuple("DockerAuthInfo", ["registry", "username", "password"]) @@ -12,7 +12,7 @@ } -def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAuthInfo]: +def process_docker_auth_config_encoded(auth_config_dict: dict[str, dict[str, dict[str, Any]]]) -> list[DockerAuthInfo]: """ Process the auths config. @@ -30,8 +30,11 @@ def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAut auth_info: list[DockerAuthInfo] = [] auths = auth_config_dict.get("auths") + if not auths: + raise KeyError("No auths found in the docker auth config") + for registry, auth in auths.items(): - auth_str = auth.get("auth") + auth_str = str(auth.get("auth")) auth_str = base64.b64decode(auth_str).decode("utf-8") username, password = auth_str.split(":") auth_info.append(DockerAuthInfo(registry, username, password)) @@ -39,7 +42,7 @@ def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAut return auth_info -def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None: +def process_docker_auth_config_cred_helpers(auth_config_dict: dict[str, Any]) -> None: """ Process the credHelpers config. @@ -56,7 +59,7 @@ def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None: warning(_AUTH_WARNINGS.pop("credHelpers")) -def process_docker_auth_config_store(auth_config_dict: dict) -> None: +def process_docker_auth_config_store(auth_config_dict: dict[str, Any]) -> None: """ Process the credsStore config. @@ -74,7 +77,7 @@ def process_docker_auth_config_store(auth_config_dict: dict) -> None: def parse_docker_auth_config(auth_config: str) -> Optional[list[DockerAuthInfo]]: """Parse the docker auth config from a string and handle the different formats.""" try: - auth_config_dict: dict = json.loads(auth_config) + auth_config_dict: dict[str, Any] = json.loads(auth_config) if "credHelpers" in auth_config: process_docker_auth_config_cred_helpers(auth_config_dict) if "credsStore" in auth_config: diff --git a/core/tests/test_auth.py b/core/tests/test_auth.py index a7581f42..0e163981 100644 --- a/core/tests/test_auth.py +++ b/core/tests/test_auth.py @@ -7,6 +7,7 @@ def test_parse_docker_auth_config_encoded(): auth_config_json = '{"auths":{"https://index.docker.io/v1/":{"auth":"dXNlcm5hbWU6cGFzc3dvcmQ="}}}' auth_info = parse_docker_auth_config(auth_config_json) + assert auth_info assert len(auth_info) == 1 assert auth_info[0] == DockerAuthInfo( registry="https://index.docker.io/v1/", @@ -37,6 +38,7 @@ def test_parse_docker_auth_config_encoded_multiple(): } auth_config_json = json.dumps(auth_dict) auth_info = parse_docker_auth_config(auth_config_json) + assert auth_info assert len(auth_info) == 3 assert auth_info[0] == DockerAuthInfo( registry="localhost:5000",