From e529089ff34d8908fcf509ea7e39e17255dc109d Mon Sep 17 00:00:00 2001 From: Thomas Tanner Date: Thu, 26 Nov 2026 09:59:26 +0000 Subject: [PATCH] chore(typing): Fix all mypy deprecated warnings Note: Depends on #823 --- .coveragerc | 7 --- kazoo/protocol/connection.py | 8 ++- kazoo/recipe/lease.py | 19 ++------ kazoo/tests/test_client.py | 3 +- kazoo/tests/test_lease.py | 95 ++++++++++++------------------------ pyproject.toml | 5 +- 6 files changed, 46 insertions(+), 91 deletions(-) diff --git a/.coveragerc b/.coveragerc index c79b53748..d84a6fc8b 100644 --- a/.coveragerc +++ b/.coveragerc @@ -4,10 +4,3 @@ include = omit = kazoo/tests/* kazoo/testing/* - -# Note - this is a copy of the default exclusions from coverage 7.10.1 -[report] -exclude_lines = - #\s*(pragma|PRAGMA)[:\s]?\s*(no|NO)\s*(cover|COVER) - ^\s*(((async )?def .*?)?\)(\s*->.*?)?:\s*)?\.\.\.\s*(#|$) - if (typing\.)?TYPE_CHECKING: diff --git a/kazoo/protocol/connection.py b/kazoo/protocol/connection.py index 3e6a8ff24..902baf3b2 100644 --- a/kazoo/protocol/connection.py +++ b/kazoo/protocol/connection.py @@ -449,8 +449,12 @@ def _read_watch_event(self, buffer: bytes, offset: int) -> None: watchers.extend(client._child_watchers.pop(path, [])) elif watch.type == CHILD_EVENT: watchers.extend(client._child_watchers.pop(path, [])) - else: - self.logger.warn("Received unknown event %r", watch.type) + else: # pragma: no cover + # No coverage here because because it'd require an unknown event + # type to be sent from the server. We could do a unit test that + # calls this method directly with a fake event, but that would be + # fragile and not really test the code in a meaningful way. + self.logger.warning("Received unknown event %r", watch.type) return # Strip the chroot if needed diff --git a/kazoo/recipe/lease.py b/kazoo/recipe/lease.py index f167c6de9..9da7bbaa4 100644 --- a/kazoo/recipe/lease.py +++ b/kazoo/recipe/lease.py @@ -11,7 +11,6 @@ import datetime import json import socket -from collections.abc import Callable from typing import TYPE_CHECKING, TypedDict, cast from kazoo.exceptions import CancelledError @@ -56,8 +55,8 @@ class NonBlockingLease: """ # Bump when storage format changes - _version = 1 - _date_format = "%Y-%m-%dT%H:%M:%S" + _version = 2 + _date_format = "%Y-%m-%dT%H:%M:%S%z" _byte_encoding = "utf-8" def __init__( @@ -66,7 +65,6 @@ def __init__( path: str, duration: datetime.timedelta, identifier: str | None = None, - utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow, ): """Create a non-blocking lease. @@ -77,13 +75,11 @@ def __init__( :param identifier: Unique name to use for this lease holder. Reuse in order to renew the lease. Defaults to :meth:`socket.gethostname()`. - :param utcnow: Clock function, by default returning - :meth:`datetime.datetime.utcnow()`. Used for testing. - """ + ident = identifier or socket.gethostname() self.obtained = False - self._attempt_obtaining(client, path, duration, ident, utcnow) + self._attempt_obtaining(client, path, duration, ident) def _attempt_obtaining( self, @@ -91,14 +87,13 @@ def _attempt_obtaining( path: str, duration: datetime.timedelta, ident: str, - utcnow: Callable[[], datetime.datetime], ) -> None: client.ensure_path(path) holder_path = path + "/lease_holder" lock = client.Lock(path, ident) try: with lock: - now = utcnow() + now = datetime.datetime.now(datetime.timezone.utc) if client.exists(holder_path): raw, _ = client.get(holder_path) data = self._decode(raw) @@ -149,8 +144,6 @@ class MultiNonBlockingLease: :param identifier: Unique name to use for this lease holder. Reuse in order to renew the lease. Defaults do :meth:`socket.gethostname()`. - :param utcnow: Clock function, by default returning - :meth:`datetime.datetime.utcnow()`. Used for testing. """ @@ -161,7 +154,6 @@ def __init__( path: str, duration: datetime.timedelta, identifier: str | None = None, - utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow, ): self.obtained = False for num in range(count): @@ -170,7 +162,6 @@ def __init__( "%s/%d" % (path, num), duration, identifier=identifier, - utcnow=utcnow, ) if ls: self.obtained = True diff --git a/kazoo/tests/test_client.py b/kazoo/tests/test_client.py index a57cecd1a..4bd78123f 100644 --- a/kazoo/tests/test_client.py +++ b/kazoo/tests/test_client.py @@ -684,7 +684,8 @@ def test_create_makepath_incompatible_acls(self) -> None: credential = make_digest_acl_credential("username", "password") alt_client = KazooClient( self.cluster[0].address + self.client.chroot, - max_retries=5, + connection_retry=KazooRetry(max_tries=5), + command_retry=KazooRetry(max_tries=5), auth_data=[("digest", credential)], handler=self._makeOne(), ) diff --git a/kazoo/tests/test_lease.py b/kazoo/tests/test_lease.py index 789852ce6..1c7b71ea0 100644 --- a/kazoo/tests/test_lease.py +++ b/kazoo/tests/test_lease.py @@ -3,21 +3,12 @@ import datetime import uuid +from freezegun import freeze_time + from kazoo.recipe.lease import MultiNonBlockingLease, NonBlockingLease from kazoo.testing import KazooTestCase -class MockClock: - def __init__(self, epoch: float = 0): - self.epoch = epoch - - def forward(self, seconds: float) -> None: - self.epoch += seconds - - def __call__(self) -> datetime.datetime: - return datetime.datetime.utcfromtimestamp(self.epoch) - - class KazooLeaseTests(KazooTestCase): def setUp(self) -> None: super().setUp() @@ -26,7 +17,10 @@ def setUp(self) -> None: self.client3 = self._get_client(timeout=0.8) self.client3.start() self.path = "/" + uuid.uuid4().hex - self.clock = MockClock(10) + # From python3.11 this should use enterContext + self._frozen_time = freeze_time() + self._clock = self._frozen_time.start() + self.addCleanup(self._frozen_time.stop) def tearDown(self) -> None: for cl in [self.client2, self.client3]: @@ -43,14 +37,14 @@ def test_renew(self) -> None: # class directly in # other tests in order to get better IDE support. lease = self.client.NonBlockingLease( - self.path, datetime.timedelta(seconds=3), utcnow=self.clock + self.path, datetime.timedelta(seconds=3) ) assert lease assert lease.obtained is True - self.clock.forward(2) + self._clock.tick(2) renewed_lease = self.client.NonBlockingLease( - self.path, datetime.timedelta(seconds=3), utcnow=self.clock + self.path, datetime.timedelta(seconds=3) ) assert renewed_lease @@ -59,17 +53,15 @@ def test_busy(self) -> None: self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert lease - self.clock.forward(2) + self._clock.tick(2) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert not foreigner_lease assert foreigner_lease.obtained is False @@ -79,40 +71,37 @@ def test_overtake(self) -> None: self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert lease - self.clock.forward(4) + self._clock.tick(4) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert foreigner_lease def test_renew_no_overtake(self) -> None: lease = self.client.NonBlockingLease( - self.path, datetime.timedelta(seconds=3), utcnow=self.clock + self.path, datetime.timedelta(seconds=3) ) assert lease assert lease.obtained is True - self.clock.forward(2) + self._clock.tick(2) renewed_lease = self.client.NonBlockingLease( - self.path, datetime.timedelta(seconds=3), utcnow=self.clock + self.path, datetime.timedelta(seconds=3) ) assert renewed_lease - self.clock.forward(2) + self._clock.tick(2) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert not foreigner_lease @@ -121,27 +110,24 @@ def test_overtaker_renews(self) -> None: self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert lease - self.clock.forward(4) + self._clock.tick(4) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert foreigner_lease - self.clock.forward(2) + self._clock.tick(2) foreigner_renew = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert foreigner_renew @@ -150,26 +136,23 @@ def test_overtake_refuse_first(self) -> None: self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert lease - self.clock.forward(4) + self._clock.tick(4) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) assert foreigner_lease - self.clock.forward(2) + self._clock.tick(2) first_again_lease = NonBlockingLease( self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert not first_again_lease @@ -180,19 +163,17 @@ def test_old_version(self) -> None: self.client, self.path, datetime.timedelta(seconds=3), - utcnow=self.clock, ) assert lease # Then back to today. NonBlockingLease._version -= 1 - self.clock.forward(4) + self._clock.tick(4) foreigner_lease = NonBlockingLease( self.client2, self.path, datetime.timedelta(seconds=3), identifier="some.other.host", - utcnow=self.clock, ) # Since a newer version wrote the lease file, the lease is not taken. assert not foreigner_lease @@ -201,16 +182,15 @@ def test_old_version(self) -> None: class MultiNonBlockingLeaseTest(KazooLeaseTests): def test_1_renew(self) -> None: ls = self.client.MultiNonBlockingLease( - 1, self.path, datetime.timedelta(seconds=4), utcnow=self.clock + 1, self.path, datetime.timedelta(seconds=4) ) assert ls - self.clock.forward(2) + self._clock.tick(2) ls2 = MultiNonBlockingLease( self.client, 1, self.path, datetime.timedelta(seconds=4), - utcnow=self.clock, ) assert ls2 @@ -220,17 +200,15 @@ def test_1_reject(self) -> None: 1, self.path, datetime.timedelta(seconds=4), - utcnow=self.clock, ) assert ls - self.clock.forward(2) + self._clock.tick(2) ls2 = MultiNonBlockingLease( self.client2, 1, self.path, datetime.timedelta(seconds=4), identifier="some.other.host", - utcnow=self.clock, ) assert not ls2 @@ -240,36 +218,32 @@ def test_2_renew(self) -> None: 2, self.path, datetime.timedelta(seconds=7), - utcnow=self.clock, ) assert ls - self.clock.forward(2) + self._clock.tick(2) ls2 = MultiNonBlockingLease( self.client2, 2, self.path, datetime.timedelta(seconds=7), identifier="host2", - utcnow=self.clock, ) assert ls2 - self.clock.forward(2) + self._clock.tick(2) ls3 = MultiNonBlockingLease( self.client, 2, self.path, datetime.timedelta(seconds=7), - utcnow=self.clock, ) assert ls3 - self.clock.forward(2) + self._clock.tick(2) ls4 = MultiNonBlockingLease( self.client2, 2, self.path, datetime.timedelta(seconds=7), identifier="host2", - utcnow=self.clock, ) assert ls4 @@ -279,27 +253,24 @@ def test_2_reject(self) -> None: 2, self.path, datetime.timedelta(seconds=7), - utcnow=self.clock, ) assert ls - self.clock.forward(2) + self._clock.tick(2) ls2 = MultiNonBlockingLease( self.client2, 2, self.path, datetime.timedelta(seconds=7), identifier="host2", - utcnow=self.clock, ) assert ls2 - self.clock.forward(2) + self._clock.tick(2) ls3 = MultiNonBlockingLease( self.client3, 2, self.path, datetime.timedelta(seconds=7), identifier="host3", - utcnow=self.clock, ) assert not ls3 @@ -309,35 +280,31 @@ def test_2_handover(self) -> None: 2, self.path, datetime.timedelta(seconds=4), - utcnow=self.clock, ) assert ls - self.clock.forward(2) + self._clock.tick(2) ls2 = MultiNonBlockingLease( self.client2, 2, self.path, datetime.timedelta(seconds=4), identifier="host2", - utcnow=self.clock, ) assert ls2 - self.clock.forward(3) + self._clock.tick(3) ls3 = MultiNonBlockingLease( self.client3, 2, self.path, datetime.timedelta(seconds=4), identifier="host3", - utcnow=self.clock, ) assert ls3 - self.clock.forward(2) + self._clock.tick(2) ls4 = MultiNonBlockingLease( self.client, 2, self.path, datetime.timedelta(seconds=4), - utcnow=self.clock, ) assert ls4 diff --git a/pyproject.toml b/pyproject.toml index 6cbcddc8f..ff595c5ce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,6 +52,7 @@ ruff = [ ] test-extras = [ "cryptography==50.0.1", + "freezegun==1.5.5", "objgraph==3.7.0", "pyjks==20.0.0", "unittest-parametrize==1.9.0", @@ -182,9 +183,7 @@ warn_unreachable = true # Miscellaneous strictness flags allow_untyped_globals = false allow_redefinition = false -# FIXME Disabled for now - there's a few things to clean up (mostly using -# deprecated APIs in tests) before we can enable this. -#enable_error_code = ["deprecated"] +enable_error_code = ["deprecated"] local_partial_types = true implicit_reexport = false strict_concatenate = true