From 816c998ef70ddb645cc2e3205f632708e365374d Mon Sep 17 00:00:00 2001 From: pinterior Date: Sat, 18 May 2024 12:56:33 +0900 Subject: [PATCH] use TypedDict for return type of legacy detect() (#469) --------- Co-authored-by: TAHRI Ahmed R. --- charset_normalizer/legacy.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/charset_normalizer/legacy.py b/charset_normalizer/legacy.py index 43aad21a..3f6d4907 100644 --- a/charset_normalizer/legacy.py +++ b/charset_normalizer/legacy.py @@ -1,13 +1,24 @@ -from typing import Any, Dict, Optional, Union +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Optional 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 TYPE_CHECKING: + from typing_extensions import TypedDict + + class ResultDict(TypedDict): + encoding: Optional[str] + language: str + confidence: Optional[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.