Mercurial > public > mercurial-scm > hg
comparison mercurial/archival.py @ 49803: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 | 3b8d92f71d92 |
comparison
equal
deleted
inserted
replaced
49802:f3f33980f19b | 49803:55d45d0de4e7 |
---|---|
152 fileobj, | 152 fileobj, |
153 mtime=mtime, | 153 mtime=mtime, |
154 ) | 154 ) |
155 self.fileobj = gzfileobj | 155 self.fileobj = gzfileobj |
156 return ( | 156 return ( |
157 # taropen() wants Literal['a', 'r', 'w', 'x'] for the mode, | |
158 # but Literal[] is only available in 3.8+ without the | |
159 # typing_extensions backport. | |
160 # pytype: disable=wrong-arg-types | |
157 tarfile.TarFile.taropen( # pytype: disable=attribute-error | 161 tarfile.TarFile.taropen( # pytype: disable=attribute-error |
158 name, pycompat.sysstr(mode), gzfileobj | 162 name, pycompat.sysstr(mode), gzfileobj |
159 ) | 163 ) |
164 # pytype: enable=wrong-arg-types | |
160 ) | 165 ) |
161 else: | 166 else: |
162 try: | 167 try: |
163 return tarfile.open( | 168 return tarfile.open( |
164 name, pycompat.sysstr(mode + kind), fileobj | 169 name, pycompat.sysstr(mode + kind), fileobj |