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

login
register
mail settings
Submitter phabricator
Date Nov. 6, 2019, 11:23 p.m.
Message ID <5cd6b571fcbe33fdb5378b42c9baeb03@localhost.localdomain>
Download mbox | patch
Permalink /patch/42848/
State Not Applicable
Headers show

Comments

phabricator - Nov. 6, 2019, 11:23 p.m.
durin42 updated this revision to Diff 17666.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7275?vs=17645&id=17666

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7275/new/

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

AFFECTED FILES
  mercurial/encoding.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -20,6 +20,20 @@ 
 
 from .pure import charencode as charencodepure
 
+if not globals():  # hide this from non-pytype users
+    from typing import (
+        Any,
+        Callable,
+        List,
+        Text,
+        Type,
+        TypeVar,
+        Union,
+    )
+
+    _T0 = TypeVar('_T0')
+    _Tlocalstr = TypeVar('_Tlocalstr', bound=localstr)
+
 charencode = policy.importmod(r'charencode')
 
 isasciistr = charencode.isasciistr
@@ -45,6 +59,7 @@ 
 
 
 def hfsignoreclean(s):
+    # type: (bytes) -> bytes
     """Remove codepoints ignored by HFS+ from s.
 
     >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8'))
@@ -99,6 +114,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 +135,7 @@ 
 
 
 def tolocal(s):
+    # type: (Text) -> bytes
     """
     Convert a string from internal UTF-8 to local encoding
 
@@ -185,6 +202,7 @@ 
 
 
 def fromlocal(s):
+    # type: (bytes) -> Text
     """
     Convert a string from the local character encoding to UTF-8
 
@@ -214,16 +232,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 +302,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 +317,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 +327,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 +425,7 @@ 
 
 
 def lower(s):
+    # type: (bytes) -> bytes
     b"best-effort encoding-aware case-folding of local string s"
     try:
         return asciilower(s)
@@ -422,6 +448,7 @@ 
 
 
 def upper(s):
+    # type: (bytes) -> bytes
     b"best-effort encoding-aware case-folding of local string s"
     try:
         return asciiupper(s)
@@ -430,6 +457,7 @@ 
 
 
 def upperfallback(s):
+    # type: (Any) -> Any
     try:
         if isinstance(s, localstr):
             u = s._utf8.decode("utf-8")
@@ -464,6 +492,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 +556,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 +575,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 +644,7 @@ 
 
 
 def fromutf8b(s):
+    # type: (Text) -> bytes
     '''Given a UTF-8b string, return a local, possibly-binary string.
 
     return the original binary string. This