##// END OF EJS Templates
typing: add type hints related to message output in mercurial/ui.py...
Matt Harbison -
r50690:f1e820cd default
parent child Browse files
Show More
@@ -20,7 +20,12 b' import sys'
20 20 import traceback
21 21
22 22 from typing import (
23 Dict,
24 List,
23 25 Optional,
26 Tuple,
27 Union,
28 cast,
24 29 )
25 30
26 31 from .i18n import _
@@ -52,6 +57,12 b' from .utils import ('
52 57 urlutil,
53 58 )
54 59
60 # The **opts args of the various write() methods can be basically anything, but
61 # there's no way to express it as "anything but str". So type it to be the
62 # handful of known types that are used.
63 _MsgOpts = Union[bytes, bool, List["_PromptChoice"]]
64 _PromptChoice = Tuple[bytes, bytes]
65
55 66 urlreq = util.urlreq
56 67
57 68 # for use with str.translate(None, _keepalnum), to keep just alphanumerics
@@ -1204,7 +1215,7 b' class ui:'
1204 1215 # Windows color printing is special, see ``write``.
1205 1216 return self._colormode != b'win32'
1206 1217
1207 def write(self, *args, **opts):
1218 def write(self, *args: bytes, **opts: _MsgOpts) -> None:
1208 1219 """write args to output
1209 1220
1210 1221 By default, this method simply writes to the buffer or stdout.
@@ -1262,10 +1273,10 b' class ui:'
1262 1273 util.timer() - starttime
1263 1274 ) * 1000
1264 1275
1265 def write_err(self, *args, **opts):
1276 def write_err(self, *args: bytes, **opts: _MsgOpts) -> None:
1266 1277 self._write(self._ferr, *args, **opts)
1267 1278
1268 def _write(self, dest, *args, **opts):
1279 def _write(self, dest, *args: bytes, **opts: _MsgOpts) -> None:
1269 1280 # update write() as well if you touch this code
1270 1281 if self._isbuffered(dest):
1271 1282 label = opts.get('label', b'')
@@ -1276,7 +1287,7 b' class ui:'
1276 1287 else:
1277 1288 self._writenobuf(dest, *args, **opts)
1278 1289
1279 def _writenobuf(self, dest, *args, **opts):
1290 def _writenobuf(self, dest, *args: bytes, **opts: _MsgOpts) -> None:
1280 1291 # update write() as well if you touch this code
1281 1292 if not opts.get('keepprogressbar', False):
1282 1293 self._progclear()
@@ -1318,7 +1329,7 b' class ui:'
1318 1329 util.timer() - starttime
1319 1330 ) * 1000
1320 1331
1321 def _writemsg(self, dest, *args, **opts):
1332 def _writemsg(self, dest, *args: bytes, **opts: _MsgOpts) -> None:
1322 1333 timestamp = self.showtimestamp and opts.get('type') in {
1323 1334 b'debug',
1324 1335 b'error',
@@ -1335,10 +1346,10 b' class ui:'
1335 1346 if timestamp:
1336 1347 dest.flush()
1337 1348
1338 def _writemsgnobuf(self, dest, *args, **opts):
1349 def _writemsgnobuf(self, dest, *args: bytes, **opts: _MsgOpts) -> None:
1339 1350 _writemsgwith(self._writenobuf, dest, *args, **opts)
1340 1351
1341 def flush(self):
1352 def flush(self) -> None:
1342 1353 # opencode timeblockedsection because this is a critical path
1343 1354 starttime = util.timer()
1344 1355 try:
@@ -1697,7 +1708,11 b' class ui:'
1697 1708
1698 1709 return i
1699 1710
1700 def _readline(self, prompt=b' ', promptopts=None):
1711 def _readline(
1712 self,
1713 prompt: bytes = b' ',
1714 promptopts: Optional[Dict[str, _MsgOpts]] = None,
1715 ) -> bytes:
1701 1716 # Replacing stdin/stdout temporarily is a hard problem on Python 3
1702 1717 # because they have to be text streams with *no buffering*. Instead,
1703 1718 # we use rawinput() only if call_readline() will be invoked by
@@ -1779,7 +1794,7 b' class ui:'
1779 1794 raise error.ResponseExpected()
1780 1795
1781 1796 @staticmethod
1782 def extractchoices(prompt):
1797 def extractchoices(prompt: bytes) -> Tuple[bytes, List[_PromptChoice]]:
1783 1798 """Extract prompt message and list of choices from specified prompt.
1784 1799
1785 1800 This returns tuple "(message, choices)", and "choices" is the
@@ -1811,7 +1826,7 b' class ui:'
1811 1826
1812 1827 return (msg, [choicetuple(s) for s in choices])
1813 1828
1814 def promptchoice(self, prompt, default=0):
1829 def promptchoice(self, prompt: bytes, default: int = 0) -> int:
1815 1830 """Prompt user with a message, read response, and ensure it matches
1816 1831 one of the provided choices. The prompt is formatted as follows:
1817 1832
@@ -1831,7 +1846,9 b' class ui:'
1831 1846 # TODO: shouldn't it be a warning?
1832 1847 self._writemsg(self._fmsgout, _(b"unrecognized response\n"))
1833 1848
1834 def getpass(self, prompt=None, default=None):
1849 def getpass(
1850 self, prompt: Optional[bytes] = None, default: Optional[bytes] = None
1851 ) -> Optional[bytes]:
1835 1852 if not self.interactive():
1836 1853 return default
1837 1854 try:
@@ -1854,7 +1871,7 b' class ui:'
1854 1871 except EOFError:
1855 1872 raise error.ResponseExpected()
1856 1873
1857 def status(self, *msg, **opts):
1874 def status(self, *msg: bytes, **opts: _MsgOpts) -> None:
1858 1875 """write status message to output (if ui.quiet is False)
1859 1876
1860 1877 This adds an output label of "ui.status".
@@ -1862,21 +1879,21 b' class ui:'
1862 1879 if not self.quiet:
1863 1880 self._writemsg(self._fmsgout, type=b'status', *msg, **opts)
1864 1881
1865 def warn(self, *msg, **opts):
1882 def warn(self, *msg: bytes, **opts: _MsgOpts) -> None:
1866 1883 """write warning message to output (stderr)
1867 1884
1868 1885 This adds an output label of "ui.warning".
1869 1886 """
1870 1887 self._writemsg(self._fmsgerr, type=b'warning', *msg, **opts)
1871 1888
1872 def error(self, *msg, **opts):
1889 def error(self, *msg: bytes, **opts: _MsgOpts) -> None:
1873 1890 """write error message to output (stderr)
1874 1891
1875 1892 This adds an output label of "ui.error".
1876 1893 """
1877 1894 self._writemsg(self._fmsgerr, type=b'error', *msg, **opts)
1878 1895
1879 def note(self, *msg, **opts):
1896 def note(self, *msg: bytes, **opts: _MsgOpts) -> None:
1880 1897 """write note to output (if ui.verbose is True)
1881 1898
1882 1899 This adds an output label of "ui.note".
@@ -1884,7 +1901,7 b' class ui:'
1884 1901 if self.verbose:
1885 1902 self._writemsg(self._fmsgout, type=b'note', *msg, **opts)
1886 1903
1887 def debug(self, *msg, **opts):
1904 def debug(self, *msg: bytes, **opts: _MsgOpts) -> None:
1888 1905 """write debug message to output (if ui.debugflag is True)
1889 1906
1890 1907 This adds an output label of "ui.debug".
@@ -2148,7 +2165,7 b' class ui:'
2148 2165 finally:
2149 2166 self._loggers = registeredloggers
2150 2167
2151 def label(self, msg, label):
2168 def label(self, msg: bytes, label: bytes) -> bytes:
2152 2169 """style msg based on supplied label
2153 2170
2154 2171 If some color mode is enabled, this will add the necessary control
@@ -2162,7 +2179,9 b' class ui:'
2162 2179 return color.colorlabel(self, msg, label)
2163 2180 return msg
2164 2181
2165 def develwarn(self, msg, stacklevel=1, config=None):
2182 def develwarn(
2183 self, msg: bytes, stacklevel: int = 1, config: Optional[bytes] = None
2184 ) -> None:
2166 2185 """issue a developer warning message
2167 2186
2168 2187 Use 'stacklevel' to report the offender some layers further up in the
@@ -2194,7 +2213,9 b' class ui:'
2194 2213 del curframe
2195 2214 del calframe
2196 2215
2197 def deprecwarn(self, msg, version, stacklevel=2):
2216 def deprecwarn(
2217 self, msg: bytes, version: bytes, stacklevel: int = 2
2218 ) -> None:
2198 2219 """issue a deprecation warning
2199 2220
2200 2221 - msg: message explaining what is deprecated and how to upgrade,
@@ -2287,7 +2308,7 b' def _selectmsgdests(ui):'
2287 2308 raise error.Abort(b'invalid ui.message-output destination: %s' % name)
2288 2309
2289 2310
2290 def _writemsgwith(write, dest, *args, **opts):
2311 def _writemsgwith(write, dest, *args: bytes, **opts: _MsgOpts) -> None:
2291 2312 """Write ui message with the given ui._write*() function
2292 2313
2293 2314 The specified message type is translated to 'ui.<type>' label if the dest
General Comments 0
You need to be logged in to leave comments. Login now