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

login
register
mail settings
Submitter phabricator
Date Nov. 6, 2019, 10:58 p.m.
Message ID <differential-rev-PHID-DREV-6ov5ujfzobgrrx5woxou-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42825/
State Superseded
Headers show

Comments

phabricator - Nov. 6, 2019, 10:58 p.m.
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




To: durin42, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 7, 2019, 8:14 a.m.
indygreg added inline comments.

INLINE COMMENTS

> encoding.py:24
> +if not globals():  # hide this from non-pytype users
> +    from typing import (
> +        Any,

Am I missing something here? Won't `globals()` always return a dict with elements? And isn't `typing` only available on Python 3?

REPOSITORY
  rHG Mercurial

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

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

To: durin42, #hg-reviewers
Cc: indygreg, mercurial-devel
phabricator - Nov. 7, 2019, 4:08 p.m.
durin42 added inline comments.

INLINE COMMENTS

> indygreg wrote in encoding.py:24
> Am I missing something here? Won't `globals()` always return a dict with elements? And isn't `typing` only available on Python 3?

This is a moderately bonkers way to write `if False` that pyflakes won't complain about and pytype won't be able to elide, so we can have pytype-only imports in the file to make the type annotation comments work.

REPOSITORY
  rHG Mercurial

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

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

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

Patch

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