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
--- a/mercurial/i18n.py Sat Mar 06 15:26:46 2021 -0500
+++ b/mercurial/i18n.py Sat Mar 06 15:58:23 2021 -0500
@@ -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