[go: up one dir, main page]

Skip to content

Commit

Permalink
Revert "Distinguish between final and intermediate ResultDict (#266)"
Browse files Browse the repository at this point in the history
This reverts commit 023e7ea.
  • Loading branch information
dan-blanchard committed Dec 1, 2022
1 parent 7264282 commit 041875f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
8 changes: 4 additions & 4 deletions chardet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .charsetgroupprober import CharSetGroupProber
from .charsetprober import CharSetProber
from .enums import InputState
from .resultdict import FinalResultDict, IntermediateResultDict
from .resultdict import ResultDict
from .universaldetector import UniversalDetector
from .version import VERSION, __version__

Expand All @@ -29,7 +29,7 @@

def detect(
byte_str: Union[bytes, bytearray], should_rename_legacy: bool = False
) -> FinalResultDict:
) -> ResultDict:
"""
Detect the encoding of the given byte string.
Expand All @@ -54,7 +54,7 @@ def detect_all(
byte_str: Union[bytes, bytearray],
ignore_threshold: bool = False,
should_rename_legacy: bool = False,
) -> List[IntermediateResultDict]:
) -> List[ResultDict]:
"""
Detect all the possible encodings of the given byte string.
Expand All @@ -80,7 +80,7 @@ def detect_all(
detector.close()

if detector.input_state == InputState.HIGH_BYTE:
results: List[IntermediateResultDict] = []
results: List[ResultDict] = []
probers: List[CharSetProber] = []
for prober in detector.charset_probers:
if isinstance(prober, CharSetGroupProber):
Expand Down
10 changes: 2 additions & 8 deletions chardet/resultdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
# for Python 3.7.
from typing import TypedDict

class FinalResultDict(TypedDict):
encoding: str
confidence: float
language: str

class IntermediateResultDict(TypedDict):
class ResultDict(TypedDict):
encoding: Optional[str]
confidence: float
language: Optional[str]

else:
FinalResultDict = dict
IntermediateResultDict = dict
ResultDict = dict
12 changes: 6 additions & 6 deletions chardet/universaldetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class a user of ``chardet`` should use.
import codecs
import logging
import re
from typing import List, Optional, Union, cast
from typing import List, Optional, Union

from .charsetgroupprober import CharSetGroupProber
from .charsetprober import CharSetProber
Expand All @@ -48,7 +48,7 @@ class a user of ``chardet`` should use.
from .latin1prober import Latin1Prober
from .macromanprober import MacRomanProber
from .mbcsgroupprober import MBCSGroupProber
from .resultdict import FinalResultDict, IntermediateResultDict
from .resultdict import ResultDict
from .sbcsgroupprober import SBCSGroupProber
from .utf1632prober import UTF1632Prober

Expand Down Expand Up @@ -105,7 +105,7 @@ def __init__(
self._esc_charset_prober: Optional[EscCharSetProber] = None
self._utf1632_prober: Optional[UTF1632Prober] = None
self._charset_probers: List[CharSetProber] = []
self.result: IntermediateResultDict = {
self.result: ResultDict = {
"encoding": None,
"confidence": 0.0,
"language": None,
Expand Down Expand Up @@ -282,7 +282,7 @@ def feed(self, byte_str: Union[bytes, bytearray]) -> None:
if self.WIN_BYTE_DETECTOR.search(byte_str):
self._has_win_bytes = True

def close(self) -> FinalResultDict:
def close(self) -> ResultDict:
"""
Stop analyzing the current document and come up with a final
prediction.
Expand All @@ -292,7 +292,7 @@ def close(self) -> FinalResultDict:
"""
# Don't bother with checks if we're already done
if self.done:
return cast(FinalResultDict, self.result)
return self.result
self.done = True

if not self._got_data:
Expand Down Expand Up @@ -359,4 +359,4 @@ def close(self) -> FinalResultDict:
group_prober.language,
group_prober.get_confidence(),
)
return cast(FinalResultDict, self.result)
return self.result

0 comments on commit 041875f

Please sign in to comment.