comparison mercurial/util.py @ 46901:51841b23670b

typing: make minor adjustments to mercurial/util.py to pass pytype checking I'm assuming the wrong-arg-count is a pytype bug, because this code is used by the config object. Avoiding initializing `_lrucachenode` node points to None eliminates a few `is not None` assertions, but apparently not all of them. I can't figure out why it gets confused over the state where these new assertions are. Differential Revision: https://phab.mercurial-scm.org/D10276
author Matt Harbison <matt_harbison@yahoo.com>
date Thu, 25 Mar 2021 22:29:41 -0400
parents 64400d05db1e
children 856820b497fc
comparison
equal deleted inserted replaced
46900:64400d05db1e 46901:51841b23670b
1263 1263
1264 def preparewrite(self): 1264 def preparewrite(self):
1265 """call this before writes, return self or a copied new object""" 1265 """call this before writes, return self or a copied new object"""
1266 if getattr(self, '_copied', 0): 1266 if getattr(self, '_copied', 0):
1267 self._copied -= 1 1267 self._copied -= 1
1268 return self.__class__(self) 1268 # Function cow.__init__ expects 1 arg(s), got 2 [wrong-arg-count]
1269 return self.__class__(self) # pytype: disable=wrong-arg-count
1269 return self 1270 return self
1270 1271
1271 def copy(self): 1272 def copy(self):
1272 """always do a cheap copy""" 1273 """always do a cheap copy"""
1273 self._copied = getattr(self, '_copied', 0) + 1 1274 self._copied = getattr(self, '_copied', 0) + 1
1406 """ 1407 """
1407 1408
1408 __slots__ = ('next', 'prev', 'key', 'value', 'cost') 1409 __slots__ = ('next', 'prev', 'key', 'value', 'cost')
1409 1410
1410 def __init__(self): 1411 def __init__(self):
1411 self.next = None 1412 self.next = self
1412 self.prev = None 1413 self.prev = self
1413 1414
1414 self.key = _notset 1415 self.key = _notset
1415 self.value = None 1416 self.value = None
1416 self.cost = 0 1417 self.cost = 0
1417 1418
1446 """ 1447 """
1447 1448
1448 def __init__(self, max, maxcost=0): 1449 def __init__(self, max, maxcost=0):
1449 self._cache = {} 1450 self._cache = {}
1450 1451
1451 self._head = head = _lrucachenode() 1452 self._head = _lrucachenode()
1452 head.prev = head
1453 head.next = head
1454 self._size = 1 1453 self._size = 1
1455 self.capacity = max 1454 self.capacity = max
1456 self.totalcost = 0 1455 self.totalcost = 0
1457 self.maxcost = maxcost 1456 self.maxcost = maxcost
1458 1457
1553 Unlike get(), this doesn't mutate the internal state. But be aware 1552 Unlike get(), this doesn't mutate the internal state. But be aware
1554 that it doesn't mean peek() is thread safe. 1553 that it doesn't mean peek() is thread safe.
1555 """ 1554 """
1556 try: 1555 try:
1557 node = self._cache[k] 1556 node = self._cache[k]
1557 assert node is not None # help pytype
1558 return node.value 1558 return node.value
1559 except KeyError: 1559 except KeyError:
1560 if default is _notset: 1560 if default is _notset:
1561 raise 1561 raise
1562 return default 1562 return default
1610 return 1610 return
1611 1611
1612 # Walk the linked list backwards starting at tail node until we hit 1612 # Walk the linked list backwards starting at tail node until we hit
1613 # a non-empty node. 1613 # a non-empty node.
1614 n = self._head.prev 1614 n = self._head.prev
1615
1616 assert n is not None # help pytype
1617
1615 while n.key is _notset: 1618 while n.key is _notset:
1616 n = n.prev 1619 n = n.prev
1617 1620
1618 assert n is not None # help pytype 1621 assert n is not None # help pytype
1619 1622