# HG changeset patch # User Matt Harbison # Date 2021-03-06 20:58:23 # Node ID b9f40b743627f35e85aae9fb520a02aab5afc277 # Parent 15c2f9220ae881ca91ee8b4314965e43493b3a41 typing: add type annotations to mercurial/i18n.py 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. Differential Revision: https://phab.mercurial-scm.org/D10124 diff --git a/mercurial/i18n.py b/mercurial/i18n.py --- a/mercurial/i18n.py +++ b/mercurial/i18n.py @@ -19,6 +19,14 @@ from . import ( 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 @@ if ( 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 @@ datapath = pycompat.fsdecode(resourceuti 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 @@ except AttributeError: 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 @@ def gettext(message): 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 @@ def _plain(): if _plain(): - _ = lambda message: message + _ = lambda message: message # type: Callable[[bytes], bytes] else: _ = gettext