comparison 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
comparison
equal deleted inserted replaced
52887:346d2c04440a 52888:7a6fc0e2a89a
86 List, 86 List,
87 Optional, 87 Optional,
88 Tuple, 88 Tuple,
89 ] 89 ]
90 90
91 if typing.TYPE_CHECKING:
92 from typing_extensions import (
93 Self,
94 )
95
96 _Tcow = TypeVar('_Tcow', bound="cow")
91 97
92 base85: intmod.Base85 = policy.importmod('base85') 98 base85: intmod.Base85 = policy.importmod('base85')
93 osutil = policy.importmod('osutil') 99 osutil = policy.importmod('osutil')
94 100
95 b85decode = base85.b85decode 101 b85decode = base85.b85decode
1327 """helper class to make copy-on-write easier 1333 """helper class to make copy-on-write easier
1328 1334
1329 Call preparewrite before doing any writes. 1335 Call preparewrite before doing any writes.
1330 """ 1336 """
1331 1337
1332 def preparewrite(self): 1338 _copied: int # doesn't exist until first preparewrite()
1339
1340 def preparewrite(self: _Tcow) -> _Tcow:
1333 """call this before writes, return self or a copied new object""" 1341 """call this before writes, return self or a copied new object"""
1334 if getattr(self, '_copied', 0): 1342 if getattr(self, '_copied', 0):
1335 self._copied -= 1 1343 self._copied -= 1
1336 # Function cow.__init__ expects 1 arg(s), got 2 [wrong-arg-count] 1344 # Function cow.__init__ expects 1 arg(s), got 2 [wrong-arg-count]
1337 return self.__class__(self) # pytype: disable=wrong-arg-count 1345 return self.__class__(self) # pytype: disable=wrong-arg-count
1338 return self 1346 return self
1339 1347
1340 def copy(self): 1348 def copy(self) -> Self:
1341 """always do a cheap copy""" 1349 """always do a cheap copy"""
1342 self._copied = getattr(self, '_copied', 0) + 1 1350 self._copied = getattr(self, '_copied', 0) + 1
1343 return self 1351 return self
1344 1352
1345 1353