Skip to content

Commit

Permalink
Add type hint for safe_str
Browse files Browse the repository at this point in the history
  • Loading branch information
cccs-jh committed Dec 1, 2023
1 parent 4636f6d commit 97a1e11
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions assemblyline/common/str_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import chardet
import re
from copy import copy
from typing import Union
from typing import Literal, Union, overload

import chardet


def remove_bidir_unicode_controls(in_str):
Expand Down Expand Up @@ -108,6 +109,14 @@ def escape_str_strict(s: bytes, reversible=True) -> str:
return escaped.decode('utf-8')


@overload
def safe_str(s: object, force_str: Literal[True]) -> str: ...


@overload
def safe_str(s: Union[str, bytes], force_str: Literal[False] = False) -> str: ...


def safe_str(s, force_str=False):
return escape_str(s, reversible=False, force_str=force_str)

Expand All @@ -117,7 +126,7 @@ def is_safe_str(s) -> bool:


# noinspection PyBroadException
def translate_str(s, min_confidence=0.7) -> dict:
def translate_str(s: Union[str, bytes], min_confidence=0.7) -> dict:
if not isinstance(s, (str, bytes)):
raise TypeError(f'Expected str or bytes got {type(s)}')

Expand All @@ -131,7 +140,7 @@ def translate_str(s, min_confidence=0.7) -> dict:

if r['confidence'] > 0 and r['confidence'] >= min_confidence:
try:
t = s.decode(r['encoding'])
t: Union[bytes, str] = s.decode(r['encoding'])
except Exception:
t = s
else:
Expand Down

0 comments on commit 97a1e11

Please sign in to comment.