D10124: typing: add type annotations to mercurial/i18n.py

mharbison72 (Matt Harbison) phabricator at mercurial-scm.org
Sun Mar 7 00:45:58 UTC 2021


mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  I'm a little unsure of this because `gettext()` clearly allows for passing
  unicode.  But the comments seem to indicate that this is related to tests, and
  this was useful for catching unicode being passed to `_()` in the keyring
  extension.  I'm also not sure why `_(None)` would make any sense, so maybe the
  argument shouldn't be optional?  I didn't add it to the lambda in plain mode
  because that spilled beyond 80 characters and so black mangled it.
  
  Black and pytype disagree on where the comment to disable a check needs to go,
  so this has to disable and then enable the checking.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/i18n.py

CHANGE DETAILS

diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -19,6 +19,14 @@
     pycompat,
 )
 
+if pycompat.TYPE_CHECKING:
+    from typing import (
+        Callable,
+        List,
+        Optional,
+    )
+
+
 # modelled after templater.templatepath:
 if getattr(sys, 'frozen', None) is not None:
     module = pycompat.sysexecutable
@@ -40,7 +48,10 @@
     try:
         import ctypes
 
+        # pytype: disable=module-attr
         langid = ctypes.windll.kernel32.GetUserDefaultUILanguage()
+        # pytype: enable=module-attr
+
         _languages = [locale.windows_locale[langid]]
     except (ImportError, AttributeError, KeyError):
         # ctypes not found or unknown langid
@@ -51,7 +62,7 @@
 localedir = os.path.join(datapath, 'locale')
 t = gettextmod.translation('hg', localedir, _languages, fallback=True)
 try:
-    _ugettext = t.ugettext
+    _ugettext = t.ugettext  # pytype: disable=attribute-error
 except AttributeError:
     _ugettext = t.gettext
 
@@ -60,6 +71,7 @@
 
 
 def gettext(message):
+    # type: (Optional[bytes]) -> Optional[bytes]
     """Translate message.
 
     The message is looked up in the catalog to get a Unicode string,
@@ -77,7 +89,7 @@
     if message not in cache:
         if type(message) is pycompat.unicode:
             # goofy unicode docstrings in test
-            paragraphs = message.split(u'\n\n')
+            paragraphs = message.split(u'\n\n')  # type: List[pycompat.unicode]
         else:
             # should be ascii, but we have unicode docstrings in test, which
             # are converted to utf-8 bytes on Python 3.
@@ -110,6 +122,6 @@
 
 
 if _plain():
-    _ = lambda message: message
+    _ = lambda message: message  # type: Callable[[bytes], bytes]
 else:
     _ = gettext



To: mharbison72, #hg-reviewers
Cc: mercurial-patches, mercurial-devel


More information about the Mercurial-devel mailing list