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

login
register
mail settings
Submitter phabricator
Date Nov. 19, 2019, 6:20 p.m.
Message ID <5453b1f3d4cf19db3f62a7e22701ab35@localhost.localdomain>
Download mbox | patch
Permalink /patch/43366/
State Not Applicable
Headers show

Comments

phabricator - Nov. 19, 2019, 6:20 p.m.
durin42 updated this revision to Diff 18243.

REPOSITORY
  rHG Mercurial

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

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, dlax
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
@@ -48,6 +48,17 @@ 
         f.set_exception(exc_info[0])
 
 
+if not globals():  # hide this from non-pytype users
+    from typing import (
+        Callable,
+        Union,
+    )
+
+    # keep pyflakes happy
+    for t in (Callable, Union):
+        assert t
+
+
 def identity(a):
     return a
 
@@ -154,7 +165,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)
@@ -291,6 +302,8 @@ 
 
         return w
 
+    bytestr = _bytestr  # Callable[[Union[bytes, str]], bytestr]
+
     # these wrappers are automagically imported by hgloader
     delattr = _wrapattrfunc(builtins.delattr)
     getattr = _wrapattrfunc(builtins.getattr)
@@ -398,7 +411,7 @@ 
     unicode = unicode
     bytechr = chr
     byterepr = repr
-    bytestr = str
+    bytestr = str  # type: Callable[[Union[bytes, str]], bytestr]
     iterbytestr = iter
     maybebytestr = identity
     sysbytes = identity