typing: add type hints to the prompt methods in mercurial/ui.py
The @overloads allow for the callers that pass a non-None `default` to not have
to worry about handling a None return to appease pytype.
--- a/mercurial/ui.py Mon Dec 12 14:17:05 2022 -0500
+++ b/mercurial/ui.py Mon Dec 12 14:10:12 2022 -0500
@@ -31,6 +31,7 @@
TypeVar,
Union,
cast,
+ overload,
)
from .i18n import _
@@ -1780,12 +1781,36 @@
return line
+ if pycompat.TYPE_CHECKING:
+
+ @overload
+ def prompt(self, msg: bytes, default: bytes) -> bytes:
+ pass
+
+ @overload
+ def prompt(self, msg: bytes, default: None) -> Optional[bytes]:
+ pass
+
def prompt(self, msg, default=b"y"):
"""Prompt user with msg, read response.
If ui is not interactive, the default is returned.
"""
return self._prompt(msg, default=default)
+ if pycompat.TYPE_CHECKING:
+
+ @overload
+ def _prompt(
+ self, msg: bytes, default: bytes, **opts: _MsgOpts
+ ) -> bytes:
+ pass
+
+ @overload
+ def _prompt(
+ self, msg: bytes, default: None, **opts: _MsgOpts
+ ) -> Optional[bytes]:
+ pass
+
def _prompt(self, msg, default=b'y', **opts):
opts = {**opts, 'default': default}
if not self.interactive():