From 3d44783ca8fed69cc8db5b1a25185063383f22dd Mon Sep 17 00:00:00 2001 From: Harry Lees Date: Fri, 20 Dec 2024 14:32:35 +0000 Subject: [PATCH 1/5] Add type annotations to `kombu/utils/encoding.py` The initial annotations provided aim to annotate the current implementation opposed to potentially refactoring to narrow down the original intention of the provided implementations. --- kombu/utils/encoding.py | 48 ++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py index 42bf2ce9f8..e15ac9cf5f 100644 --- a/kombu/utils/encoding.py +++ b/kombu/utils/encoding.py @@ -9,69 +9,84 @@ import sys import traceback +from typing import Any, Union, Literal, overload, TypeVar + + +T = TypeVar("T") #: safe_str takes encoding from this file by default. #: :func:`set_default_encoding_file` can used to set the #: default output file. -default_encoding_file = None +default_encoding_file: Union[str, None] = None -def set_default_encoding_file(file): +def set_default_encoding_file(file: str) -> None: """Set file used to get codec information.""" global default_encoding_file default_encoding_file = file -def get_default_encoding_file(): +def get_default_encoding_file() -> str | None: """Get file used to get codec information.""" return default_encoding_file if sys.platform.startswith('java'): # pragma: no cover - def default_encoding(file=None): + def default_encoding(file: object = None) -> Literal['utf-8']: """Get default encoding.""" return 'utf-8' else: - def default_encoding(file=None): + def default_encoding(file: object = None) -> str: """Get default encoding.""" file = file or get_default_encoding_file() return getattr(file, 'encoding', None) or sys.getfilesystemencoding() -def str_to_bytes(s): +@overload +def str_to_bytes(s: str) -> bytes: ... +@overload +def str_to_bytes(s: T) -> T: ... +def str_to_bytes(s: Any) -> Any: """Convert str to bytes.""" if isinstance(s, str): return s.encode() return s -def bytes_to_str(s): +@overload +def bytes_to_str(s: bytes) -> str: ... +@overload +def bytes_to_str(s: T) -> T: ... +def bytes_to_str(s: Any) -> Any: """Convert bytes to str.""" if isinstance(s, bytes): return s.decode(errors='replace') return s -def from_utf8(s, *args, **kwargs): +def from_utf8(s: str, *args: Any, **kwargs: Any) -> str: """Get str from utf-8 encoding.""" return s -def ensure_bytes(s): +def ensure_bytes(s: Union[str, bytes]) -> bytes: """Ensure s is bytes, not str.""" if not isinstance(s, bytes): return str_to_bytes(s) return s -def default_encode(obj): +def default_encode(obj: T) -> T: """Encode using default encoding.""" return obj -def safe_str(s, errors='replace'): +def safe_str( + s: object, + errors: str = 'replace', +) -> str: """Safe form of str(), void of unicode errors.""" s = bytes_to_str(s) if not isinstance(s, (str, bytes)): @@ -79,7 +94,11 @@ def safe_str(s, errors='replace'): return _safe_str(s, errors) -def _safe_str(s, errors='replace', file=None): +def _safe_str( + s: object, + errors: str = 'replace', + file: Any = None +) -> str: if isinstance(s, str): return s try: @@ -89,7 +108,10 @@ def _safe_str(s, errors='replace', file=None): type(s), exc, '\n'.join(traceback.format_stack())) -def safe_repr(o, errors='replace'): +def safe_repr( + o: object, + errors: str = 'replace', +) -> str: """Safe form of repr, void of Unicode errors.""" try: return repr(o) From 5d4a70b9fea7bb3c6af6dd968b8fb943978f022c Mon Sep 17 00:00:00 2001 From: Harry Lees Date: Fri, 20 Dec 2024 14:35:22 +0000 Subject: [PATCH 2/5] Enable mypy for `kombu/utils/encoding.py` --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 488d782fa5..c52c0cdbbd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,6 +52,7 @@ ignore_missing_imports = True files = kombu/abstract.py, kombu/utils/debug.py, + kombu/utils/encoding.py, kombu/utils/time.py, kombu/utils/uuid.py, t/unit/utils/test_uuid.py, From 905db0cf293d50f441b62d7a382d993ccf3a2916 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:41:27 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- kombu/utils/encoding.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py index e15ac9cf5f..32eb9941c5 100644 --- a/kombu/utils/encoding.py +++ b/kombu/utils/encoding.py @@ -9,15 +9,14 @@ import sys import traceback -from typing import Any, Union, Literal, overload, TypeVar - +from typing import Any, Literal, TypeVar, overload T = TypeVar("T") #: safe_str takes encoding from this file by default. #: :func:`set_default_encoding_file` can used to set the #: default output file. -default_encoding_file: Union[str, None] = None +default_encoding_file: str | None = None def set_default_encoding_file(file: str) -> None: @@ -71,7 +70,7 @@ def from_utf8(s: str, *args: Any, **kwargs: Any) -> str: return s -def ensure_bytes(s: Union[str, bytes]) -> bytes: +def ensure_bytes(s: str | bytes) -> bytes: """Ensure s is bytes, not str.""" if not isinstance(s, bytes): return str_to_bytes(s) From 2a911cd68306a769c00f87bfa744a4f8bf8cabfc Mon Sep 17 00:00:00 2001 From: Harry Lees Date: Fri, 20 Dec 2024 14:43:18 +0000 Subject: [PATCH 4/5] Add spacing between `@overload`'ed functions to fix E302 errors --- kombu/utils/encoding.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py index 32eb9941c5..2487e6f5f0 100644 --- a/kombu/utils/encoding.py +++ b/kombu/utils/encoding.py @@ -45,8 +45,12 @@ def default_encoding(file: object = None) -> str: @overload def str_to_bytes(s: str) -> bytes: ... + + @overload def str_to_bytes(s: T) -> T: ... + + def str_to_bytes(s: Any) -> Any: """Convert str to bytes.""" if isinstance(s, str): @@ -56,8 +60,12 @@ def str_to_bytes(s: Any) -> Any: @overload def bytes_to_str(s: bytes) -> str: ... + + @overload def bytes_to_str(s: T) -> T: ... + + def bytes_to_str(s: Any) -> Any: """Convert bytes to str.""" if isinstance(s, bytes): From cd01e280b7132707f3e56d4ed67e855de4c62d9e Mon Sep 17 00:00:00 2001 From: Harry Lees Date: Tue, 4 Mar 2025 17:37:13 +0000 Subject: [PATCH 5/5] Replace object annotations with typing.Any --- kombu/utils/encoding.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py index 2487e6f5f0..4adcb9a910 100644 --- a/kombu/utils/encoding.py +++ b/kombu/utils/encoding.py @@ -32,12 +32,12 @@ def get_default_encoding_file() -> str | None: if sys.platform.startswith('java'): # pragma: no cover - def default_encoding(file: object = None) -> Literal['utf-8']: + def default_encoding(file: Any = None) -> Literal['utf-8']: """Get default encoding.""" return 'utf-8' else: - def default_encoding(file: object = None) -> str: + def default_encoding(file: Any = None) -> str: """Get default encoding.""" file = file or get_default_encoding_file() return getattr(file, 'encoding', None) or sys.getfilesystemencoding() @@ -91,7 +91,7 @@ def default_encode(obj: T) -> T: def safe_str( - s: object, + s: Any, errors: str = 'replace', ) -> str: """Safe form of str(), void of unicode errors.""" @@ -102,7 +102,7 @@ def safe_str( def _safe_str( - s: object, + s: Any, errors: str = 'replace', file: Any = None ) -> str: @@ -116,7 +116,7 @@ def _safe_str( def safe_repr( - o: object, + o: Any, errors: str = 'replace', ) -> str: """Safe form of repr, void of Unicode errors."""