From 89222b7b7908163571e199c595b6e50b5b37c8e4 Mon Sep 17 00:00:00 2001 From: pinterior Date: Tue, 14 May 2024 20:11:39 +0900 Subject: [PATCH 1/3] use TypedDict for return type of legacy detect() --- charset_normalizer/legacy.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/charset_normalizer/legacy.py b/charset_normalizer/legacy.py index 43aad21a..fdc264a0 100644 --- a/charset_normalizer/legacy.py +++ b/charset_normalizer/legacy.py @@ -1,13 +1,26 @@ +import sys from typing import Any, Dict, Optional, Union from warnings import warn from .api import from_bytes from .constant import CHARDET_CORRESPONDENCE +# TODO: remove this check when dropping Python 3.7 support +if sys.version_info >= (3, 8): + from typing import TypedDict + + class ResultDict(TypedDict): + encoding: Optional[str] + language: str + confidence: Optional[float] + +else: + ResultDict = Dict[str, Optional[Union[str, float]]] + def detect( byte_str: bytes, should_rename_legacy: bool = False, **kwargs: Any -) -> Dict[str, Optional[Union[str, float]]]: +) -> ResultDict: """ chardet legacy method Detect the encoding of the given byte string. It should be mostly backward-compatible. From 83473fc371a72b02bb75d10592a14932e9c71a77 Mon Sep 17 00:00:00 2001 From: pinterior Date: Wed, 15 May 2024 19:37:23 +0900 Subject: [PATCH 2/3] use typing.TYPE_CHECKING (from code review) Co-authored-by: TAHRI Ahmed R. --- charset_normalizer/legacy.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/charset_normalizer/legacy.py b/charset_normalizer/legacy.py index fdc264a0..b4521cff 100644 --- a/charset_normalizer/legacy.py +++ b/charset_normalizer/legacy.py @@ -1,21 +1,19 @@ -import sys -from typing import Any, Dict, Optional, Union +from __future__ import annotations +from typing import Any, Dict, Optional, Union, TYPE_CHECKING from warnings import warn from .api import from_bytes from .constant import CHARDET_CORRESPONDENCE # TODO: remove this check when dropping Python 3.7 support -if sys.version_info >= (3, 8): - from typing import TypedDict +if TYPE_CHECKING: + from typing_extensions import TypedDict class ResultDict(TypedDict): encoding: Optional[str] language: str confidence: Optional[float] -else: - ResultDict = Dict[str, Optional[Union[str, float]]] def detect( From e48b609feb9db92362abef4b22da22d2a08af085 Mon Sep 17 00:00:00 2001 From: pinterior Date: Wed, 15 May 2024 19:44:02 +0900 Subject: [PATCH 3/3] trivial styling fix --- charset_normalizer/legacy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/charset_normalizer/legacy.py b/charset_normalizer/legacy.py index b4521cff..3f6d4907 100644 --- a/charset_normalizer/legacy.py +++ b/charset_normalizer/legacy.py @@ -1,5 +1,6 @@ from __future__ import annotations -from typing import Any, Dict, Optional, Union, TYPE_CHECKING + +from typing import TYPE_CHECKING, Any, Optional from warnings import warn from .api import from_bytes @@ -15,7 +16,6 @@ class ResultDict(TypedDict): confidence: Optional[float] - def detect( byte_str: bytes, should_rename_legacy: bool = False, **kwargs: Any ) -> ResultDict: