Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 52888:7a6fc0e2a89a
typing: add type hints to the `mercurial.util.cow` mixin class
The `disable=wrong-arg-count` suppression is still required with pytype
2023.11.21 for some reason. PyCharm agrees, until the `_Tcow` annotation is
added to the `self` arg (which is what pytype has been doing in the generated
*.pyi file, so it's made explicit here).
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Fri, 20 Dec 2024 19:32:02 -0500 |
parents | 346d2c04440a |
children | 9a8815128679 |
line wrap: on
line diff
--- a/mercurial/util.py Fri Dec 20 19:13:06 2024 -0500 +++ b/mercurial/util.py Fri Dec 20 19:32:02 2024 -0500 @@ -88,6 +88,12 @@ Tuple, ] +if typing.TYPE_CHECKING: + from typing_extensions import ( + Self, + ) + + _Tcow = TypeVar('_Tcow', bound="cow") base85: intmod.Base85 = policy.importmod('base85') osutil = policy.importmod('osutil') @@ -1329,7 +1335,9 @@ Call preparewrite before doing any writes. """ - def preparewrite(self): + _copied: int # doesn't exist until first preparewrite() + + def preparewrite(self: _Tcow) -> _Tcow: """call this before writes, return self or a copied new object""" if getattr(self, '_copied', 0): self._copied -= 1 @@ -1337,7 +1345,7 @@ return self.__class__(self) # pytype: disable=wrong-arg-count return self - def copy(self): + def copy(self) -> Self: """always do a cheap copy""" self._copied = getattr(self, '_copied', 0) + 1 return self