diff --git a/mercurial/encoding.py b/mercurial/encoding.py --- a/mercurial/encoding.py +++ b/mercurial/encoding.py @@ -40,6 +40,16 @@ asciiupper = charencode.asciiupper unichr = chr +if typing.TYPE_CHECKING: + # TODO: make a stub file for .cext.charencode, and import here + from .pure.charencode import ( + asciilower, + asciiupper, + isasciistr, + jsonescapeu8fast as _jsonescapeu8fast, + ) + + # These unicode characters are ignored by HFS+ (Apple Technote 1150, # "Unicode Subtleties"), so we need to ignore them in some places for # sanity. @@ -524,7 +534,7 @@ class normcasespecs: other = 0 -def jsonescape(s: Any, paranoid: Any = False) -> Any: +def jsonescape(s: bytes, paranoid: bool = False) -> bytes: """returns a string suitable for JSON JSON is problematic for us because it doesn't support non-Unicode diff --git a/mercurial/pure/charencode.py b/mercurial/pure/charencode.py --- a/mercurial/pure/charencode.py +++ b/mercurial/pure/charencode.py @@ -11,7 +11,7 @@ import array from .. import pycompat -def isasciistr(s): +def isasciistr(s: bytes) -> bool: try: s.decode('ascii') return True @@ -19,7 +19,7 @@ def isasciistr(s): return False -def asciilower(s): +def asciilower(s: bytes) -> bytes: """convert a string to lowercase if ASCII Raises UnicodeDecodeError if non-ASCII characters are found.""" @@ -27,7 +27,7 @@ def asciilower(s): return s.lower() -def asciiupper(s): +def asciiupper(s: bytes) -> bytes: """convert a string to uppercase if ASCII Raises UnicodeDecodeError if non-ASCII characters are found.""" @@ -52,7 +52,7 @@ def asciiupper(s): _jsonmap.extend(pycompat.bytechr(x) for x in range(128, 256)) -def jsonescapeu8fast(u8chars, paranoid): +def jsonescapeu8fast(u8chars: bytes, paranoid: bool) -> bytes: """Convert a UTF-8 byte string to JSON-escaped form (fast path) Raises ValueError if non-ASCII characters have to be escaped. @@ -70,7 +70,7 @@ def jsonescapeu8fast(u8chars, paranoid): _utf8strict = r'surrogatepass' -def jsonescapeu8fallback(u8chars, paranoid): +def jsonescapeu8fallback(u8chars: bytes, paranoid: bool) -> bytes: """Convert a UTF-8 byte string to JSON-escaped form (slow path) Escapes all non-ASCII characters no matter if paranoid is False.