Matt Harbison <matt_harbison@yahoo.com> [Thu, 15 Dec 2022 01:05:27 -0500] rev 49807
typing: add type hints to most mercurial/pycompat.py functions
The `rapply` methods are left out because it's not `rapply(f, xs: _T0) -> _T0`
as I first thought- it's used somewhere to walk a collection and convert between
bytes and str.
Also, the `open()` call is partially untyped because I'm not sure what its
purpose is at this point- both the name and mode can be either bytes or str as
it is currently constituted. It might make sense to assert that the file is
being opened in binary mode (like `namedtempfile()`) and cast the result to
`BinaryIO`, but that shouldn't be smuggled in with these other changes. The
return is currently typed as `Any` because something suddenly got smarter and a
few uses in util.py (like readfile()) suddenly think it returns `IO[str]`
instead of `IO[bytes]` (BinaryIO), and it flags the type mismatch there.
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 22:27:22 -0500] rev 49806
statprof: don't pass str `sys.argv` to a function expecting bytes
Found by typing the global functions in mercurial.pycompat.
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 22:24:54 -0500] rev 49805
typing: drop an unnecessary warning disabling comment in match.py
This stopped being necessary in
d2e1dcd4490d, when the exception stopped being
subscripted.
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 22:22:12 -0500] rev 49804
scmposix: don't subscript IOError
This warning disabling has been in place since late 2019 in
667f56d73ceb. We
should have had some py3 support at the time, but both pytype complains and
subscripting a real FileNotFoundError generated in `hg debugshell` crashed, so
maybe this fixes a problem. It looks like all other instances of subscripting
exceptions have been replaced (at least as far as greping for `== errno.`
revealed).
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 01:51:33 -0500] rev 49803
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.
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 01:38:52 -0500] rev 49802
pycompat: explicitly prefix builtin attr usage with `builtins.`
It doesn't seem like this would fix any bug, because the wrapped functions that
take bytes instead of str are defined after these calls. But PyCharm was
flagging the second and third uses, saying "Type 'str' doesn't have expected
attribute 'decode'". It wasn't flagging the first, but I changed it for
consistency.
Matt Harbison <matt_harbison@yahoo.com> [Wed, 14 Dec 2022 01:32:03 -0500] rev 49801
typing: add type hints to global variables in mercurial/pycompat.py
The way `osaltsep` and `sysexecutable` were defined, pytype determined them to
be `Union[bytes, str]`. This was a problem because that cascaded to all of the
callers, and also because it couldn't be annotated as bytes on the initial
assignment. Therefore, we use a ternary operator.
The documentation says that `sys.executable` can either be None or an empty
string if the value couldn't be determined. We opt for an empty string here
because there are places that blindly pass it to `os.path.xxx()` functions,
which crash if given None. Other places test `if pycompat.sysexecutable`, so
empty string works for both.