Patchwork D7380: encoding: define per-use identity functions only in typechecking mode

login
register
mail settings
Submitter phabricator
Date Nov. 14, 2019, 3:51 a.m.
Message ID <differential-rev-PHID-DREV-ifsuxg5c3bmwalju23t5-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/43172/
State Superseded
Headers show

Comments

phabricator - Nov. 14, 2019, 3:51 a.m.
durin42 created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This avoids some unfortunate overhead from my previous iteration.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/encoding.py

CHANGE DETAILS




To: durin42, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 14, 2019, 4:03 a.m.
durin42 added a comment.


  Let's not land this for now: it's breaking a fair amount of typechecking. :(

REPOSITORY
  rHG Mercurial

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

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

To: durin42, #hg-reviewers
Cc: mercurial-devel
phabricator - Nov. 14, 2019, 10:50 a.m.
dlax added a comment.


  Have you tried using variables annotations? Like:
  
    strtolocal = pycompat.identity  # type: (str) -> bytes
    strfromlocal = pycompat.identity  # type: (bytes) -> str

REPOSITORY
  rHG Mercurial

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

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

To: durin42, #hg-reviewers
Cc: dlax, mercurial-devel
Yuya Nishihara - Nov. 14, 2019, 2:29 p.m.
>     strtolocal = pycompat.identity  # type: (str) -> bytes
>     strfromlocal = pycompat.identity  # type: (bytes) -> str

`# type: Callable[[str], bytes]` might work. pytype complained
`# type: ... -> ...` is an invalid type comment.
phabricator - Nov. 14, 2019, 2:31 p.m.
yuja added a comment.


  >   strtolocal = pycompat.identity  # type: (str) -> bytes
  >   strfromlocal = pycompat.identity  # type: (bytes) -> str
  
  `# type: Callable[[str], bytes]` might work. pytype complained
  `# type: ... -> ...` is an invalid type comment.

REPOSITORY
  rHG Mercurial

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

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

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

Patch

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -25,6 +25,7 @@ 
         Any,
         Callable,
         List,
+        TYPE_CHECKING,
         Text,
         Type,
         TypeVar,
@@ -32,7 +33,7 @@ 
     )
 
     # keep pyflakes happy
-    for t in (Any, Callable, List, Text, Type, Union):
+    for t in (Any, Callable, List, Text, Type, Union, TYPE_CHECKING):
         assert t
 
     _Tlocalstr = TypeVar('_Tlocalstr', bound=localstr)
@@ -265,16 +266,20 @@ 
     strfromlocal = unifromlocal
     strmethod = unimethod
 else:
+    strtolocal = pycompat.identity
+    strfromlocal = pycompat.identity
+    strmethod = pycompat.identity
 
-    def strtolocal(s):
-        # type: (str) -> bytes
-        return s
+    if 'TYPE_CHECKING' in globals() and TYPE_CHECKING:
 
-    def strfromlocal(s):
-        # type: (bytes) -> str
-        return s
+        def strtolocal(s):
+            # type: (str) -> bytes
+            return s
 
-    strmethod = pycompat.identity
+        def strfromlocal(s):
+            # type: (bytes) -> str
+            return s
+
 
 if not _nativeenviron:
     # now encoding and helper functions are available, recreate the environ