Skip to content

Commit 66726b6

Browse files
authored
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 ```
1 parent 62bd0de commit 66726b6

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

core/testcontainers/core/auth.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json as json
33
from collections import namedtuple
44
from logging import warning
5-
from typing import Optional
5+
from typing import Any, Optional
66

77
DockerAuthInfo = namedtuple("DockerAuthInfo", ["registry", "username", "password"])
88

@@ -12,7 +12,7 @@
1212
}
1313

1414

15-
def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAuthInfo]:
15+
def process_docker_auth_config_encoded(auth_config_dict: dict[str, dict[str, dict[str, Any]]]) -> list[DockerAuthInfo]:
1616
"""
1717
Process the auths config.
1818
@@ -30,16 +30,19 @@ def process_docker_auth_config_encoded(auth_config_dict: dict) -> list[DockerAut
3030
auth_info: list[DockerAuthInfo] = []
3131

3232
auths = auth_config_dict.get("auths")
33+
if not auths:
34+
raise KeyError("No auths found in the docker auth config")
35+
3336
for registry, auth in auths.items():
34-
auth_str = auth.get("auth")
37+
auth_str = str(auth.get("auth"))
3538
auth_str = base64.b64decode(auth_str).decode("utf-8")
3639
username, password = auth_str.split(":")
3740
auth_info.append(DockerAuthInfo(registry, username, password))
3841

3942
return auth_info
4043

4144

42-
def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None:
45+
def process_docker_auth_config_cred_helpers(auth_config_dict: dict[str, Any]) -> None:
4346
"""
4447
Process the credHelpers config.
4548
@@ -56,7 +59,7 @@ def process_docker_auth_config_cred_helpers(auth_config_dict: dict) -> None:
5659
warning(_AUTH_WARNINGS.pop("credHelpers"))
5760

5861

59-
def process_docker_auth_config_store(auth_config_dict: dict) -> None:
62+
def process_docker_auth_config_store(auth_config_dict: dict[str, Any]) -> None:
6063
"""
6164
Process the credsStore config.
6265
@@ -74,7 +77,7 @@ def process_docker_auth_config_store(auth_config_dict: dict) -> None:
7477
def parse_docker_auth_config(auth_config: str) -> Optional[list[DockerAuthInfo]]:
7578
"""Parse the docker auth config from a string and handle the different formats."""
7679
try:
77-
auth_config_dict: dict = json.loads(auth_config)
80+
auth_config_dict: dict[str, Any] = json.loads(auth_config)
7881
if "credHelpers" in auth_config:
7982
process_docker_auth_config_cred_helpers(auth_config_dict)
8083
if "credsStore" in auth_config:

core/tests/test_auth.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
def test_parse_docker_auth_config_encoded():
88
auth_config_json = '{"auths":{"https://index.docker.io/v1/":{"auth":"dXNlcm5hbWU6cGFzc3dvcmQ="}}}'
99
auth_info = parse_docker_auth_config(auth_config_json)
10+
assert auth_info
1011
assert len(auth_info) == 1
1112
assert auth_info[0] == DockerAuthInfo(
1213
registry="https://index.docker.io/v1/",
@@ -37,6 +38,7 @@ def test_parse_docker_auth_config_encoded_multiple():
3738
}
3839
auth_config_json = json.dumps(auth_dict)
3940
auth_info = parse_docker_auth_config(auth_config_json)
41+
assert auth_info
4042
assert len(auth_info) == 3
4143
assert auth_info[0] == DockerAuthInfo(
4244
registry="localhost:5000",

0 commit comments

Comments
 (0)