Patchwork D7296: pycompat: kludge around pytype being confused by __new__

login
register
mail settings
Submitter phabricator
Date Nov. 14, 2019, 3:51 a.m.
Message ID <7d892c2a4d9e1db6b7f1239b506df31b@localhost.localdomain>
Download mbox | patch
Permalink /patch/43173/
State Not Applicable
Headers show

Comments

phabricator - Nov. 14, 2019, 3:51 a.m.
Herald added a subscriber: mjpieters.
durin42 updated this revision to Diff 18066.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7296?vs=17672&id=18066

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

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

AFFECTED FILES
  mercurial/pycompat.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
--- a/mercurial/pycompat.py
+++ b/mercurial/pycompat.py
@@ -154,7 +154,7 @@ 
     bytechr = struct.Struct('>B').pack
     byterepr = b'%r'.__mod__
 
-    class bytestr(bytes):
+    class _bytestr(bytes):
         """A bytes which mostly acts as a Python 2 str
 
         >>> bytestr(), bytestr(bytearray(b'foo')), bytestr(u'ascii'), bytestr(1)
@@ -208,7 +208,7 @@ 
         """
 
         def __new__(cls, s=b''):
-            if isinstance(s, bytestr):
+            if isinstance(s, _bytestr):
                 return s
             if not isinstance(
                 s, (bytes, bytearray)
@@ -398,7 +398,7 @@ 
     unicode = unicode
     bytechr = chr
     byterepr = repr
-    bytestr = str
+    _bytestr = str
     iterbytestr = iter
     maybebytestr = identity
     sysbytes = identity
@@ -504,3 +504,29 @@ 
     return tempfile.NamedTemporaryFile(
         mode, bufsize, suffix=suffix, prefix=prefix, dir=dir, delete=delete
     )
+
+
+try:
+    if ispy3:
+        import typing
+
+        typing.Union
+except ImportError:
+    pass
+
+# Hide this from non-pytype users. Note that when we're willing to
+# depend on the `typing` module, we can remove the `if not globals`
+# hack.
+if ispy3 and 'typing' in globals():
+    from typing import TYPE_CHECKING
+
+    if typing.TYPE_CHECKING:
+        _bytestr = Any
+
+        def bytestr(s=b''):
+            # type: (Union[bytes,str]) -> _bytestr
+            return _bytestr(s)
+
+
+else:
+    bytestr = _bytestr