D7275: encoding: add comment-based type hints for pytype

durin42 (Augie Fackler) phabricator at mercurial-scm.org
Wed Nov 6 22:58:25 UTC 2019


durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7275

AFFECTED FILES
  mercurial/encoding.py

CHANGE DETAILS

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -20,6 +20,12 @@
 
 from .pure import charencode as charencodepure
 
+if False:
+    from typing import Any, Callable, List, Type, TypeVar, Union, Text
+
+    _T0 = TypeVar('_T0')
+    _Tlocalstr = TypeVar('_Tlocalstr', bound=localstr)
+
 charencode = policy.importmod(r'charencode')
 
 isasciistr = charencode.isasciistr
@@ -45,6 +51,7 @@
 
 
 def hfsignoreclean(s):
+    # type: (bytes) -> bytes
     """Remove codepoints ignored by HFS+ from s.
 
     >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8'))
@@ -99,6 +106,7 @@
     round-tripped to the local encoding and back'''
 
     def __new__(cls, u, l):
+        # type: (Type[_Tlocalstr], Text, bytes) -> _Tlocalstr
         s = bytes.__new__(cls, l)
         s._utf8 = u
         return s
@@ -119,6 +127,7 @@
 
 
 def tolocal(s):
+    # type: (Text) -> bytes
     """
     Convert a string from internal UTF-8 to local encoding
 
@@ -185,6 +194,7 @@
 
 
 def fromlocal(s):
+    # type: (bytes) -> Text
     """
     Convert a string from the local character encoding to UTF-8
 
@@ -214,16 +224,19 @@
 
 
 def unitolocal(u):
+    # type: (Text) -> bytes
     """Convert a unicode string to a byte string of local encoding"""
     return tolocal(u.encode('utf-8'))
 
 
 def unifromlocal(s):
+    # type: (bytes) -> Text
     """Convert a byte string of local encoding to a unicode string"""
     return fromlocal(s).decode('utf-8')
 
 
 def unimethod(bytesfunc):
+    # type: (Callable[[Any], bytes]) -> Callable[[Any], Text]
     """Create a proxy method that forwards __unicode__() and __str__() of
     Python 3 to __bytes__()"""
 
@@ -281,11 +294,13 @@
 
 
 def colwidth(s):
+    # type: (bytes) -> int
     b"Find the column width of a string for display in the local encoding"
     return ucolwidth(s.decode(_sysstr(encoding), r'replace'))
 
 
 def ucolwidth(d):
+    # type: (Text) -> int
     b"Find the column width of a Unicode string for display"
     eaw = getattr(unicodedata, 'east_asian_width', None)
     if eaw is not None:
@@ -294,6 +309,7 @@
 
 
 def getcols(s, start, c):
+    # type: (bytes, int, int) -> bytes
     '''Use colwidth to find a c-column substring of s starting at byte
     index start'''
     for x in pycompat.xrange(start + c, len(s)):
@@ -303,6 +319,7 @@
 
 
 def trim(s, width, ellipsis=b'', leftside=False):
+    # type: (bytes, int, bytes, bool) -> bytes
     """Trim string 's' to at most 'width' columns (including 'ellipsis').
 
     If 'leftside' is True, left side of string 's' is trimmed.
@@ -400,6 +417,7 @@
 
 
 def lower(s):
+    # type: (bytes) -> bytes
     b"best-effort encoding-aware case-folding of local string s"
     try:
         return asciilower(s)
@@ -422,6 +440,7 @@
 
 
 def upper(s):
+    # type: (bytes) -> bytes
     b"best-effort encoding-aware case-folding of local string s"
     try:
         return asciiupper(s)
@@ -430,6 +449,7 @@
 
 
 def upperfallback(s):
+    # type: (Any) -> Any
     try:
         if isinstance(s, localstr):
             u = s._utf8.decode("utf-8")
@@ -464,6 +484,7 @@
 
 
 def jsonescape(s, paranoid=False):
+    # type: (Any, Any) -> Any
     '''returns a string suitable for JSON
 
     JSON is problematic for us because it doesn't support non-Unicode
@@ -527,6 +548,7 @@
 
 
 def getutf8char(s, pos):
+    # type: (Any, Any) -> Any
     '''get the next full utf-8 character in the given string, starting at pos
 
     Raises a UnicodeError if the given location does not start a valid
@@ -545,6 +567,7 @@
 
 
 def toutf8b(s):
+    # type: (Any) -> Any
     '''convert a local, possibly-binary string into UTF-8b
 
     This is intended as a generic method to preserve data when working
@@ -613,6 +636,7 @@
 
 
 def fromutf8b(s):
+    # type: (Text) -> bytes
     '''Given a UTF-8b string, return a local, possibly-binary string.
 
     return the original binary string. This



To: durin42, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list