Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/pycompat.py @ 48029:1fda8c9358ce
typing: add a fake `__init__()` to bytestr to distract pytype
I'm not sure what changed before pytype 09-09-2021 (from 04-15-2021), but these
started getting flagged. This wrapping an exception in a `bytestr` pattern has
been flagged before, and I've fixed it then with `stringutil.forcebytestr()`.
But that doesn't work here, because it would create a circular import.
I suspect the issue is `bytes.__new__()` wants `Iterable[int]`, so it just
assumes the subclass will also take that. The referenced pytype bug isn't an
exact match, but seems related and the suggested workaround helps.
The specific warnings fixed are:
File "/mnt/c/Users/Matt/hg/mercurial/encoding.py", line 212, in tolocal: Function bytestr.__init__ was called with the wrong arguments [wrong-arg-types]
Expected: (self, ints: Iterable[int])
Actually passed: (self, ints: LookupError)
Attributes of protocol Iterable[int] are not implemented on LookupError: __iter__
Called from (traceback):
line 353, in current file
File "/mnt/c/Users/Matt/hg/mercurial/encoding.py", line 240, in fromlocal: Function bytestr.__init__ was called with the wrong arguments [wrong-arg-types]
Expected: (self, ints: Iterable[int])
Actually passed: (self, ints: UnicodeDecodeError)
Attributes of protocol Iterable[int] are not implemented on UnicodeDecodeError: __iter__
Differential Revision: https://phab.mercurial-scm.org/D11466
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 21 Sep 2021 00:16:35 -0400 |
parents | 2b76255a4f74 |
children | 6ffcaba7d122 |
comparison
equal
deleted
inserted
replaced
48028:b642a6298ce0 | 48029:1fda8c9358ce |
---|---|
219 bytearray-like behavior. | 219 bytearray-like behavior. |
220 | 220 |
221 >>> t = bytes(t) # cast to bytes | 221 >>> t = bytes(t) # cast to bytes |
222 >>> assert type(t) is bytes | 222 >>> assert type(t) is bytes |
223 """ | 223 """ |
224 | |
225 # Trick pytype into not demanding Iterable[int] be passed to __new__(), | |
226 # since the appropriate bytes format is done internally. | |
227 # | |
228 # https://github.com/google/pytype/issues/500 | |
229 if TYPE_CHECKING: | |
230 | |
231 def __init__(self, s=b''): | |
232 pass | |
224 | 233 |
225 def __new__(cls, s=b''): | 234 def __new__(cls, s=b''): |
226 if isinstance(s, bytestr): | 235 if isinstance(s, bytestr): |
227 return s | 236 return s |
228 if not isinstance( | 237 if not isinstance( |