Skip to content

support copy.deepcopy #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions streamable/stream.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import datetime
import logging
from contextlib import suppress
Expand All @@ -7,6 +8,7 @@
Callable,
Collection,
Coroutine,
Dict,
Generic,
Iterable,
Iterator,
Expand Down Expand Up @@ -609,6 +611,11 @@ class DownStream(Stream[U], Generic[T, U]):
def __init__(self, upstream: Stream[T]) -> None:
self._upstream: Stream[T] = upstream

def __deepcopy__(self, memo: Dict[int, Any]) -> "DownStream[T, U]":
new = copy.copy(self)
new._upstream = copy.deepcopy(self._upstream, memo)
return new

@property
def source(self) -> Union[Iterable, Callable[[], Iterable]]:
return self._upstream.source
Expand Down
20 changes: 20 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import copy
import datetime
import logging
import math
Expand Down Expand Up @@ -1866,3 +1867,22 @@ def test_on_queue_in_thread(self) -> None:
["foo", "bar"],
msg="stream must work on Queue",
)

def test_deepcopy(self) -> None:
stream = Stream([]).map(str)
stream_copy = copy.deepcopy(stream)
self.assertEqual(
stream,
stream_copy,
msg="the copy must be equal",
)
self.assertIsNot(
stream,
stream_copy,
msg="the copy must be a different object",
)
self.assertIsNot(
stream.source,
stream_copy.source,
msg="the copy's source must be a different object",
)