This commit is contained in:
Nuno Campos
2025-03-31 16:36:16 -07:00
parent 0425d4e65d
commit 881b07cf7f
10 changed files with 22 additions and 30 deletions
@@ -1,4 +1,4 @@
from typing import Any, Generic, Optional, Sequence, Type
from typing import Any, Generic, Sequence, Type
from typing_extensions import Self
@@ -30,7 +30,7 @@ class AnyValue(Generic[Value], BaseChannel[Value, Value, Value]):
"""The type of the update received by the channel."""
return self.typ
def from_checkpoint(self, checkpoint: Optional[Value]) -> Self:
def from_checkpoint(self, checkpoint: Value) -> Self:
empty = self.__class__(self.typ)
empty.key = self.key
if checkpoint is not MISSING:
+3 -3
View File
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Generic, Optional, Sequence, TypeVar
from typing import Any, Generic, Sequence, TypeVar
from typing_extensions import Self
@@ -29,14 +29,14 @@ class BaseChannel(Generic[Value, Update, C], ABC):
# serialize/deserialize methods
def checkpoint(self) -> Optional[C]:
def checkpoint(self) -> C:
"""Return a serializable representation of the channel's current state.
Raises EmptyChannelError if the channel is empty (never updated yet),
or doesn't support checkpoints."""
return self.get()
@abstractmethod
def from_checkpoint(self, checkpoint: Optional[C]) -> Self:
def from_checkpoint(self, checkpoint: C) -> Self:
"""Return a new identical channel, optionally initialized from a checkpoint.
If the checkpoint contains complex data structures, they should be copied."""
+2 -8
View File
@@ -1,11 +1,5 @@
import collections.abc
from typing import (
Callable,
Generic,
Optional,
Sequence,
Type,
)
from typing import Callable, Generic, Sequence, Type
from typing_extensions import NotRequired, Required, Self
@@ -72,7 +66,7 @@ class BinaryOperatorAggregate(Generic[Value], BaseChannel[Value, Value, Value]):
"""The type of the update received by the channel."""
return self.typ
def from_checkpoint(self, checkpoint: Optional[Value]) -> Self:
def from_checkpoint(self, checkpoint: Value) -> Self:
empty = self.__class__(self.typ, self.operator)
empty.key = self.key
if checkpoint is not MISSING:
@@ -50,8 +50,7 @@ class DynamicBarrierValue(
return (self.names, self.seen)
def from_checkpoint(
self,
checkpoint: Optional[tuple[Optional[set[Value]], set[Value]]],
self, checkpoint: tuple[Optional[set[Value]], set[Value]]
) -> Self:
empty = self.__class__(self.typ)
empty.key = self.key
@@ -1,4 +1,4 @@
from typing import Any, Generic, Optional, Sequence, Type
from typing import Any, Generic, Sequence, Type
from typing_extensions import Self
@@ -30,7 +30,7 @@ class EphemeralValue(Generic[Value], BaseChannel[Value, Value, Value]):
"""The type of the update received by the channel."""
return self.typ
def from_checkpoint(self, checkpoint: Optional[Value]) -> Self:
def from_checkpoint(self, checkpoint: Value) -> Self:
empty = self.__class__(self.typ, self.guard)
empty.key = self.key
if checkpoint is not MISSING:
@@ -1,4 +1,4 @@
from typing import Any, Generic, Optional, Sequence, Type
from typing import Any, Generic, Sequence, Type
from typing_extensions import Self
@@ -34,7 +34,7 @@ class LastValue(Generic[Value], BaseChannel[Value, Value, Value]):
"""The type of the update received by the channel."""
return self.typ
def from_checkpoint(self, checkpoint: Optional[Value]) -> Self:
def from_checkpoint(self, checkpoint: Value) -> Self:
empty = self.__class__(self.typ)
empty.key = self.key
if checkpoint is not MISSING:
@@ -1,4 +1,4 @@
from typing import Generic, Optional, Sequence, Type
from typing import Generic, Sequence, Type
from typing_extensions import Self
@@ -36,7 +36,7 @@ class NamedBarrierValue(Generic[Value], BaseChannel[Value, Value, set[Value]]):
def checkpoint(self) -> set[Value]:
return self.seen
def from_checkpoint(self, checkpoint: Optional[set[Value]]) -> Self:
def from_checkpoint(self, checkpoint: set[Value]) -> Self:
empty = self.__class__(self.typ, self.names)
empty.key = self.key
if checkpoint is not MISSING:
+5 -6
View File
@@ -1,4 +1,4 @@
from typing import Any, Generic, Iterator, Optional, Sequence, Type, Union
from typing import Any, Generic, Iterator, Sequence, Type, Union
from typing_extensions import Self
@@ -17,9 +17,7 @@ def flatten(values: Sequence[Union[Value, list[Value]]]) -> Iterator[Value]:
class Topic(
Generic[Value],
BaseChannel[
Sequence[Value], Union[Value, list[Value]], tuple[set[Value], list[Value]]
],
BaseChannel[Sequence[Value], Union[Value, list[Value]], list[Value]],
):
"""A configurable PubSub Topic.
@@ -50,14 +48,15 @@ class Topic(
"""The type of the update received by the channel."""
return Union[self.typ, list[self.typ]] # type: ignore[name-defined]
def checkpoint(self) -> tuple[set[Value], list[Value]]:
def checkpoint(self) -> list[Value]:
return self.values
def from_checkpoint(self, checkpoint: Optional[list[Value]]) -> Self:
def from_checkpoint(self, checkpoint: list[Value]) -> Self:
empty = self.__class__(self.typ, self.accumulate)
empty.key = self.key
if checkpoint is not MISSING:
if isinstance(checkpoint, tuple):
# backwards compatibility
empty.values = checkpoint[1]
else:
empty.values = checkpoint
@@ -1,4 +1,4 @@
from typing import Generic, Optional, Sequence, Type
from typing import Generic, Sequence, Type
from typing_extensions import Self
@@ -33,7 +33,7 @@ class UntrackedValue(Generic[Value], BaseChannel[Value, Value, Value]):
def checkpoint(self) -> Value:
raise EmptyChannelError()
def from_checkpoint(self, checkpoint: Optional[Value]) -> Self:
def from_checkpoint(self, checkpoint: Value) -> Self:
empty = self.__class__(self.typ, self.guard)
empty.key = self.key
return empty
+1 -1
View File
@@ -164,7 +164,7 @@ def _ensure_future(
elif EAGER_NOT_SUPPORTED or lazy:
return loop.create_task(coro_or_future, name=name, context=context)
else:
return asyncio.eager_task_factory(
return asyncio.eager_task_factory( # type:ignore[attr-defined]
loop, coro_or_future, name=name, context=context
)
except RuntimeError: