comparison mercurial/templater.py @ 49898:55d45d0de4e7

typing: add type hints to pycompat.bytestr The problem with leaving pytype to its own devices here was that for functions that returned a bytestr, pytype inferred `Union[bytes, int]`. It now accepts that it can be treated as plain bytes. I wasn't able to figure out the arg type for `__getitem__`- `SupportsIndex` (which PyCharm indicated is how the superclass function is typed) got flagged: File "/mnt/c/Users/Matt/hg/mercurial/pycompat.py", line 236, in __getitem__: unsupported operand type(s) for item retrieval: bytestr and SupportsIndex [unsupported-operands] Function __getitem__ on bytestr expects int But some caller got flagged when I marked it as `int`. There's some minor spillover problems elsewhere- pytype doesn't seem to recognize that `bytes.startswith()` can optionally take a 3rd and 4th arg, so those few places have the warning disabled. It also flags where the tar API is being abused, but that would be a tricky refactor (and would require typing extensions until py3.7 is dropped), so disable those too.
author Matt Harbison <matt_harbison@yahoo.com>
date Wed, 14 Dec 2022 01:51:33 -0500
parents 642e31cb55f0
children 18c8c18993f0
comparison
equal deleted inserted replaced
49897:f3f33980f19b 49898:55d45d0de4e7
175 else: 175 else:
176 token = b'template' 176 token = b'template'
177 quote = program[pos : pos + 2] 177 quote = program[pos : pos + 2]
178 s = pos = pos + 2 178 s = pos = pos + 2
179 while pos < end: # find closing escaped quote 179 while pos < end: # find closing escaped quote
180 # pycompat.bytestr (and bytes) both have .startswith() that
181 # takes an optional start and an optional end, but pytype thinks
182 # it only takes 2 args.
183
184 # pytype: disable=wrong-arg-count
180 if program.startswith(b'\\\\\\', pos, end): 185 if program.startswith(b'\\\\\\', pos, end):
181 pos += 4 # skip over double escaped characters 186 pos += 4 # skip over double escaped characters
182 continue 187 continue
183 if program.startswith(quote, pos, end): 188 if program.startswith(quote, pos, end):
189 # pytype: enable=wrong-arg-count
190
184 # interpret as if it were a part of an outer string 191 # interpret as if it were a part of an outer string
185 data = parser.unescapestr(program[s:pos]) 192 data = parser.unescapestr(program[s:pos])
186 if token == b'template': 193 if token == b'template':
187 data = _parsetemplate(data, 0, len(data))[0] 194 data = _parsetemplate(data, 0, len(data))[0]
188 yield (token, data, s) 195 yield (token, data, s)
298 if c == quote: 305 if c == quote:
299 yield (b'end', None, n + 1) 306 yield (b'end', None, n + 1)
300 return 307 return
301 308
302 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, b'}')) 309 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, b'}'))
310
311 # pycompat.bytestr (and bytes) both have .startswith() that
312 # takes an optional start and an optional end, but pytype thinks
313 # it only takes 2 args.
314
315 # pytype: disable=wrong-arg-count
303 if not tmpl.startswith(b'}', pos): 316 if not tmpl.startswith(b'}', pos):
317 # pytype: enable=wrong-arg-count
304 raise error.ParseError(_(b"invalid token"), pos) 318 raise error.ParseError(_(b"invalid token"), pos)
305 yield (b'template', parseres, n) 319 yield (b'template', parseres, n)
306 pos += 1 320 pos += 1
307 321
308 if quote: 322 if quote: