From 794a22e22362227ccfc0b2acd18130196e25775d Mon Sep 17 00:00:00 2001 From: Roy Moore Date: Sun, 8 Sep 2024 09:06:49 +0300 Subject: [PATCH] fix(core): Typing in config + utils (#692) Supports: https://github.com/testcontainers/testcontainers-python/issues/305 Related : #691 ```bash poetry run mypy --config-file pyproject.toml core/testcontainers/core/config.py core/testcontainers/core/utils.py Success: no issues found in 2 source files ``` --- core/testcontainers/core/config.py | 8 ++++---- core/testcontainers/core/utils.py | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/core/testcontainers/core/config.py b/core/testcontainers/core/config.py index 7b727951..142dbc19 100644 --- a/core/testcontainers/core/config.py +++ b/core/testcontainers/core/config.py @@ -30,7 +30,7 @@ def read_tc_properties() -> dict[str, str]: tc_files = [item for item in [TC_GLOBAL] if exists(item)] if not tc_files: return {} - settings = {} + settings: dict[str, str] = {} for file in tc_files: with open(file) as contents: @@ -60,14 +60,14 @@ class TestcontainersConfiguration: """ @property - def docker_auth_config(self): + def docker_auth_config(self) -> Optional[str]: config = self._docker_auth_config if config and "DOCKER_AUTH_CONFIG" in _WARNINGS: warning(_WARNINGS.pop("DOCKER_AUTH_CONFIG")) return config @docker_auth_config.setter - def docker_auth_config(self, value: str): + def docker_auth_config(self, value: str) -> None: if "DOCKER_AUTH_CONFIG" in _WARNINGS: warning(_WARNINGS.pop("DOCKER_AUTH_CONFIG")) self._docker_auth_config = value @@ -76,7 +76,7 @@ def tc_properties_get_tc_host(self) -> Union[str, None]: return self.tc_properties.get("tc.host") @property - def timeout(self): + def timeout(self) -> int: return self.max_tries * self.sleep_time diff --git a/core/testcontainers/core/utils.py b/core/testcontainers/core/utils.py index 5ca1c2f7..4a7bec4b 100644 --- a/core/testcontainers/core/utils.py +++ b/core/testcontainers/core/utils.py @@ -3,6 +3,7 @@ import platform import subprocess import sys +from typing import Any, Optional LINUX = "linux" MAC = "mac" @@ -18,7 +19,7 @@ def setup_logger(name: str) -> logging.Logger: return logger -def os_name() -> str: +def os_name() -> Optional[str]: pl = sys.platform if pl == "linux" or pl == "linux2": return LINUX @@ -26,6 +27,7 @@ def os_name() -> str: return MAC elif pl == "win32": return WIN + return None def is_mac() -> bool: @@ -53,7 +55,7 @@ def inside_container() -> bool: return os.path.exists("/.dockerenv") -def default_gateway_ip() -> str: +def default_gateway_ip() -> Optional[str]: """ Returns gateway IP address of the host that testcontainer process is running on @@ -66,11 +68,12 @@ def default_gateway_ip() -> str: ip_address = process.communicate()[0] if ip_address and process.returncode == 0: return ip_address.decode("utf-8").strip().strip("\n") + return None except subprocess.SubprocessError: return None -def raise_for_deprecated_parameter(kwargs: dict, name: str, replacement: str) -> dict: +def raise_for_deprecated_parameter(kwargs: dict[Any, Any], name: str, replacement: str) -> dict[Any, Any]: """ Raise an error if a dictionary of keyword arguments contains a key and suggest the replacement. """