Skip to content

Commit

Permalink
Merge branch 'package_typing' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cccs-jh committed Dec 4, 2023
2 parents e48b006 + 97a1e11 commit dbadaab
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 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
Empty file added assemblyline/py.typed
Empty file.
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from setuptools import setup, find_packages, Extension
from setuptools import Extension, find_packages, setup

try:
# noinspection PyUnresolvedReferences,PyPackageRequirements
Expand Down Expand Up @@ -115,6 +115,7 @@
"*.pxd",
"*.lark",
"VERSION",
]
],
"assemblyline": ["py.typed"]
}
)

0 comments on commit dbadaab

Please sign in to comment.