Mercurial > public > mercurial-scm > hg
comparison mercurial/error.py @ 48284:9de0823705b4
typing: add more type hints to the errors module
These were found by looking for `Any` types in the generated `*.pyi` file after
running the tests. There are some more complicated types that I'm not sure of,
and am leaving untyped. I also can't figure out how to get `self.hint` to be
anything other than `Any` in most classes.
Differential Revision: https://phab.mercurial-scm.org/D11719
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sat, 23 Oct 2021 17:13:57 -0400 |
parents | 73f52278a158 |
children | 6a454e7053a1 |
comparison
equal
deleted
inserted
replaced
48283:ebac18733142 | 48284:9de0823705b4 |
---|---|
29 Union, | 29 Union, |
30 ) | 30 ) |
31 | 31 |
32 | 32 |
33 def _tobytes(exc): | 33 def _tobytes(exc): |
34 # type: (...) -> bytes | |
34 """Byte-stringify exception in the same way as BaseException_str()""" | 35 """Byte-stringify exception in the same way as BaseException_str()""" |
35 if not exc.args: | 36 if not exc.args: |
36 return b'' | 37 return b'' |
37 if len(exc.args) == 1: | 38 if len(exc.args) == 1: |
38 return pycompat.bytestr(exc.args[0]) | 39 return pycompat.bytestr(exc.args[0]) |
45 This should come first in the inheritance list to consume a hint and | 46 This should come first in the inheritance list to consume a hint and |
46 pass remaining arguments to the exception class. | 47 pass remaining arguments to the exception class. |
47 """ | 48 """ |
48 | 49 |
49 def __init__(self, *args, **kw): | 50 def __init__(self, *args, **kw): |
50 self.hint = kw.pop('hint', None) | 51 self.hint = kw.pop('hint', None) # type: Optional[bytes] |
51 super(Hint, self).__init__(*args, **kw) | 52 super(Hint, self).__init__(*args, **kw) |
52 | 53 |
53 | 54 |
54 class Error(Hint, Exception): | 55 class Error(Hint, Exception): |
55 """Base class for Mercurial errors.""" | 56 """Base class for Mercurial errors.""" |
69 return self.message | 70 return self.message |
70 | 71 |
71 if pycompat.ispy3: | 72 if pycompat.ispy3: |
72 | 73 |
73 def __str__(self): | 74 def __str__(self): |
75 # type: () -> str | |
74 # the output would be unreadable if the message was translated, | 76 # the output would be unreadable if the message was translated, |
75 # but do not replace it with encoding.strfromlocal(), which | 77 # but do not replace it with encoding.strfromlocal(), which |
76 # may raise another exception. | 78 # may raise another exception. |
77 return pycompat.sysstr(self.__bytes__()) | 79 return pycompat.sysstr(self.__bytes__()) |
78 | 80 |
103 pass | 105 pass |
104 | 106 |
105 | 107 |
106 class SidedataHashError(RevlogError): | 108 class SidedataHashError(RevlogError): |
107 def __init__(self, key, expected, got): | 109 def __init__(self, key, expected, got): |
110 # type: (int, bytes, bytes) -> None | |
108 self.hint = None | 111 self.hint = None |
109 self.sidedatakey = key | 112 self.sidedatakey = key |
110 self.expecteddigest = expected | 113 self.expecteddigest = expected |
111 self.actualdigest = got | 114 self.actualdigest = got |
112 | 115 |
115 __bytes__ = _tobytes | 118 __bytes__ = _tobytes |
116 | 119 |
117 | 120 |
118 class LookupError(RevlogError, KeyError): | 121 class LookupError(RevlogError, KeyError): |
119 def __init__(self, name, index, message): | 122 def __init__(self, name, index, message): |
123 # type: (bytes, bytes, bytes) -> None | |
120 self.name = name | 124 self.name = name |
121 self.index = index | 125 self.index = index |
122 # this can't be called 'message' because at least some installs of | 126 # this can't be called 'message' because at least some installs of |
123 # Python 2.6+ complain about the 'message' property being deprecated | 127 # Python 2.6+ complain about the 'message' property being deprecated |
124 self.lookupmessage = message | 128 self.lookupmessage = message |
341 | 345 |
342 class OutOfBandError(RemoteError): | 346 class OutOfBandError(RemoteError): |
343 """Exception raised when a remote repo reports failure""" | 347 """Exception raised when a remote repo reports failure""" |
344 | 348 |
345 def __init__(self, message=None, hint=None): | 349 def __init__(self, message=None, hint=None): |
350 # type: (Optional[bytes], Optional[bytes]) -> None | |
346 from .i18n import _ | 351 from .i18n import _ |
347 | 352 |
348 if message: | 353 if message: |
349 # Abort.format() adds a trailing newline | 354 # Abort.format() adds a trailing newline |
350 message = _(b"remote error:\n%s") % message.rstrip(b'\n') | 355 message = _(b"remote error:\n%s") % message.rstrip(b'\n') |