Patchwork D10235: typing: mark the argument to mercurial.i18n.gettext() non-Optional

login
register
mail settings
Submitter phabricator
Date March 19, 2021, 5:58 a.m.
Message ID <differential-rev-PHID-DREV-wjtdfm63hjbzgchoevnh-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48551/
State Superseded
Headers show

Comments

phabricator - March 19, 2021, 5:58 a.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Few if any of the callers are handling a `None` return, which is making pytype
  complain.  I tried adding @overload definitions to indicate the bytes -> bytes
  and None -> None relationship, but pytype doesn't seem to apply that to `_()`
  through the function assignment.  What did work was to change `_()` into its own
  function that called `gettext()`, but that has an extra function call overhead.
  Even putting that function into an `if pycompat.TYPE_CHECKING` block and leaving
  the existing assignments in the `else` block caused pytype to lose track of the
  @overloads.
  
  At that point, I simply gave up.  PyCharm doesn't like that it can return None,
  given the new type hints, but pytype doesn't complain about this nor does it
  see any callers passing None.  The most important thing here is to catch str
  being passed anyway.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/i18n.py

CHANGE DETAILS




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

Patch

diff --git a/mercurial/i18n.py b/mercurial/i18n.py
--- a/mercurial/i18n.py
+++ b/mercurial/i18n.py
@@ -23,7 +23,6 @@ 
     from typing import (
         Callable,
         List,
-        Optional,
     )
 
 
@@ -71,7 +70,7 @@ 
 
 
 def gettext(message):
-    # type: (Optional[bytes]) -> Optional[bytes]
+    # type: (bytes) -> bytes
     """Translate message.
 
     The message is looked up in the catalog to get a Unicode string,