File: core.py

package info (click to toggle)
python-aiostream 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: python: 2,800; makefile: 5
file content (760 lines) | stat: -rw-r--r-- 24,772 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
"""Core objects for stream operators."""

from __future__ import annotations

import inspect
import functools
import warnings

from .aiter_utils import AsyncIteratorContext, aiter, assert_async_iterable
from typing import (
    Any,
    AsyncIterator,
    Callable,
    Generator,
    Iterator,
    Protocol,
    Union,
    TypeVar,
    AsyncIterable,
    Awaitable,
)

from typing_extensions import ParamSpec, Concatenate


__all__ = ["Stream", "Streamer", "StreamEmpty", "operator", "streamcontext"]


# Exception


class StreamEmpty(Exception):
    """Exception raised when awaiting an empty stream."""

    pass


# Helpers

T = TypeVar("T", covariant=True)
X = TypeVar("X")
A = TypeVar("A", contravariant=True)
P = ParamSpec("P")
Q = ParamSpec("Q")


async def wait_stream(aiterable: BaseStream[T]) -> T:
    """Wait for an asynchronous iterable to finish and return the last item.

    The iterable is executed within a safe stream context.
    A StreamEmpty exception is raised if the sequence is empty.
    """

    class Unassigned:
        pass

    last_item: Unassigned | T = Unassigned()

    async with streamcontext(aiterable) as streamer:
        async for item in streamer:
            last_item = item

    if isinstance(last_item, Unassigned):
        raise StreamEmpty()
    return last_item


# Core objects


class BaseStream(AsyncIterable[T], Awaitable[T]):
    """
    Base class for streams.

    See `Stream` and `Streamer` for more information.
    """

    def __init__(self, factory: Callable[[], AsyncIterable[T]]) -> None:
        """Initialize the stream with an asynchronous iterable factory.

        The factory is a callable and takes no argument.
        The factory return value is an asynchronous iterable.
        """
        aiter = factory()
        assert_async_iterable(aiter)
        self._generator = self._make_generator(aiter, factory)

    def _make_generator(
        self, first: AsyncIterable[T], factory: Callable[[], AsyncIterable[T]]
    ) -> Iterator[AsyncIterable[T]]:
        """Generate asynchronous iterables when required.

        The first iterable is created beforehand for extra checking.
        """
        yield first
        del first
        while True:
            yield factory()

    def __await__(self) -> Generator[Any, None, T]:
        """Await protocol.

        Safely iterate and return the last element.
        """
        return wait_stream(self).__await__()

    def __or__(self, func: Callable[[BaseStream[T]], X]) -> X:
        """Pipe protocol.

        Allow to pipe stream operators.
        """
        return func(self)

    def __add__(self, value: AsyncIterable[X]) -> Stream[Union[X, T]]:
        """Addition protocol.

        Concatenate with a given asynchronous sequence.
        """
        from .stream import chain

        return chain(self, value)

    def __getitem__(self, value: Union[int, slice]) -> Stream[T]:
        """Get item protocol.

        Accept index or slice to extract the corresponding item(s)
        """
        from .stream import getitem

        return getitem(self, value)

    # Disable sync iteration
    # This is necessary because __getitem__ is defined
    # which is a valid fallback for for-loops in python
    __iter__: None = None


class Stream(BaseStream[T]):
    """Enhanced asynchronous iterable.

    It provides the following features:

      - **Operator pipe-lining** - using pipe symbol ``|``
      - **Repeatability** - every iteration creates a different iterator
      - **Safe iteration context** - using ``async with`` and the ``stream``
        method
      - **Simplified execution** - get the last element from a stream using
        ``await``
      - **Slicing and indexing** - using square brackets ``[]``
      - **Concatenation** - using addition symbol ``+``

    It is not meant to be instanciated directly.
    Use the stream operators instead.

    Example::

        xs = stream.count()    # xs is a stream object
        ys = xs | pipe.skip(5) # pipe xs and skip the first 5 elements
        zs = ys[5:10:2]        # slice ys using start, stop and step

        async with zs.stream() as streamer:  # stream zs in a safe context
            async for z in streamer:         # iterate the zs streamer
                print(z)                     # Prints 10, 12, 14

        result = await zs  # await zs and return its last element
        print(result)      # Prints 14
        result = await zs  # zs can be used several times
        print(result)      # Prints 14
    """

    def stream(self) -> Streamer[T]:
        """Return a streamer context for safe iteration.

        Example::

            xs = stream.count()
            async with xs.stream() as streamer:
                async for item in streamer:
                    <block>

        """
        return self.__aiter__()

    def __aiter__(self) -> Streamer[T]:
        """Asynchronous iteration protocol.

        Return a streamer context for safe iteration.
        """
        return streamcontext(next(self._generator))

    # Advertise the proper synthax for entering a stream context

    __aexit__: None = None

    async def __aenter__(self) -> None:
        raise TypeError(
            "A stream object cannot be used as a context manager. "
            "Use the `stream` method instead: "
            "`async with xs.stream() as streamer`"
        )


class Streamer(AsyncIteratorContext[T], BaseStream[T]):
    """Enhanced asynchronous iterator context.

    It is similar to AsyncIteratorContext but provides the stream
    magic methods for concatenation, indexing and awaiting.

    It's not meant to be instanciated directly, use streamcontext instead.

    Example::

        ait = some_asynchronous_iterable()
        async with streamcontext(ait) as streamer:
            async for item in streamer:
                await streamer[5]
    """

    pass


def streamcontext(aiterable: AsyncIterable[T]) -> Streamer[T]:
    """Return a stream context manager from an asynchronous iterable.

    The context management makes sure the aclose asynchronous method
    of the corresponding iterator has run before it exits. It also issues
    warnings and RuntimeError if it is used incorrectly.

    It is safe to use with any asynchronous iterable and prevent
    asynchronous iterator context to be wrapped twice.

    Correct usage::

        ait = some_asynchronous_iterable()
        async with streamcontext(ait) as streamer:
            async for item in streamer:
                <block>

    For streams objects, it is possible to use the stream method instead::

        xs = stream.count()
        async with xs.stream() as streamer:
            async for item in streamer:
                <block>
    """
    aiterator = aiter(aiterable)
    if isinstance(aiterator, Streamer):
        return aiterator
    return Streamer(aiterator)


# Operator type protocol


class Operator(Protocol[P, T]):
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Stream[T]: ...

    @staticmethod
    def raw(*args: P.args, **kwargs: P.kwargs) -> AsyncIterator[T]: ...


class PipableOperator(Protocol[A, P, T]):
    def __call__(
        self, source: AsyncIterable[A], /, *args: P.args, **kwargs: P.kwargs
    ) -> Stream[T]: ...

    @staticmethod
    def raw(
        source: AsyncIterable[A], /, *args: P.args, **kwargs: P.kwargs
    ) -> AsyncIterator[T]: ...

    @staticmethod
    def pipe(
        *args: P.args, **kwargs: P.kwargs
    ) -> Callable[[AsyncIterable[A]], Stream[T]]: ...


class SourcesOperator(Protocol[P, T]):
    def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Stream[T]: ...

    @staticmethod
    def raw(*args: P.args, **kwargs: P.kwargs) -> AsyncIterator[T]: ...

    @staticmethod
    def pipe(
        *args: P.args, **kwargs: P.kwargs
    ) -> Callable[[AsyncIterable[Any]], Stream[T]]: ...


# Operator decorators


def operator(
    func: Callable[P, AsyncIterator[T]] | None = None,
    pipable: bool | None = None,
) -> Operator[P, T]:
    """Create a stream operator from an asynchronous generator
    (or any function returning an asynchronous iterable).

    Decorator usage::

        @operator
        async def random(offset=0., width=1.):
            while True:
                yield offset + width * random.random()

    The return value is a dynamically created callable.
    It has the same name, module and documentation as the original function.

    A new stream is created by simply calling the operator::

        xs = random()

    The original function is called right away to check that the
    signatures match. Other methods are available:

      - `original`: the original function as a static method
      - `raw`: same as original with extra checking

    The `pipable` argument is deprecated, use `pipable_operator` instead.
    """

    # Handle compatibility with legacy (aiostream <= 0.4)
    if pipable is not None or func is None:
        warnings.warn(
            "The `pipable` argument is deprecated. Use either `@operator` or `@pipable_operator` directly.",
            DeprecationWarning,
        )
    if func is None:
        return pipable_operator if pipable else operator  # type: ignore
    if pipable is True:
        return pipable_operator(func)  # type: ignore

    # First check for classmethod instance, to avoid more confusing errors later on
    if isinstance(func, classmethod):
        raise ValueError(
            "An operator cannot be created from a class method, "
            "since the decorated function becomes an operator class"
        )

    # Gather data
    name = func.__name__
    module = func.__module__
    extra_doc = func.__doc__
    doc = extra_doc or f"Regular {name} stream operator."

    # Extract signature
    signature = inspect.signature(func)
    parameters = list(signature.parameters.values())
    return_annotation = signature.return_annotation
    if parameters and parameters[0].name in ("self", "cls"):
        raise ValueError(
            "An operator cannot be created from a method, "
            "since the decorated function becomes an operator class"
        )

    # Wrapped static method
    original_func = func
    original_func.__qualname__ = name + ".original"

    # Raw static method
    raw_func = func
    raw_func.__qualname__ = name + ".raw"

    # Gather attributes
    class OperatorImplementation:

        original = staticmethod(original_func)

        def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Stream[T]:
            factory = functools.partial(raw_func, *args, **kwargs)
            return Stream(factory)

        @staticmethod
        def raw(*args: P.args, **kwargs: P.kwargs) -> AsyncIterator[T]:
            return raw_func(*args, **kwargs)

        def __repr__(self) -> str:
            return f"{module}.{name}"

        def __str__(self) -> str:
            return f"{module}.{name}"

    # Customize raw method
    OperatorImplementation.raw.__signature__ = signature  # type: ignore[attr-defined]
    OperatorImplementation.raw.__qualname__ = name + ".raw"
    OperatorImplementation.raw.__module__ = module
    OperatorImplementation.raw.__doc__ = doc

    # Customize call method
    self_parameter = inspect.Parameter("self", inspect.Parameter.POSITIONAL_OR_KEYWORD)
    new_parameters = [self_parameter] + parameters
    new_return_annotation = (
        return_annotation.replace("AsyncIterator", "Stream")
        if isinstance(return_annotation, str)
        else return_annotation
    )
    OperatorImplementation.__call__.__signature__ = signature.replace(  # type: ignore[attr-defined]
        parameters=new_parameters, return_annotation=new_return_annotation
    )
    OperatorImplementation.__call__.__qualname__ = name + ".__call__"
    OperatorImplementation.__call__.__name__ = "__call__"
    OperatorImplementation.__call__.__module__ = module
    OperatorImplementation.__call__.__doc__ = doc

    # Create operator singleton
    properly_named_class = type(
        name,
        (OperatorImplementation,),
        {
            "__qualname__": name,
            "__module__": module,
            "__doc__": doc,
        },
    )
    operator_instance = properly_named_class()
    return operator_instance


def pipable_operator(
    func: Callable[Concatenate[AsyncIterable[X], P], AsyncIterator[T]],
) -> PipableOperator[X, P, T]:
    """Create a pipable stream operator from an asynchronous generator
    (or any function returning an asynchronous iterable).

    Decorator usage::

        @pipable_operator
        async def multiply(source, factor):
            async with streamcontext(source) as streamer:
                 async for item in streamer:
                     yield factor * item

    The first argument is expected to be the asynchronous iteratable used
    for piping.

    The return value is a dynamically created callable.
    It has the same name, module and documentation as the original function.

    A new stream is created by simply calling the operator::

        xs = random()
        ys = multiply(xs, 2)

    The original function is called right away (but not awaited) to check that
    signatures match. The sources are also checked for asynchronous iteration.

    The operator also have a `pipe` method that can be used with the pipe
    synthax::

        xs = random()
        ys = xs | multiply.pipe(2)

    This is strictly equivalent to the previous example.

    Other methods are available:

      - `original`: the original function as a static method
      - `raw`: same as original with extra checking

    The raw method is useful to create new operators from existing ones::

        @pipable_operator
        def double(source):
            return multiply.raw(source, 2)
    """

    # First check for classmethod instance, to avoid more confusing errors later on
    if isinstance(func, classmethod):
        raise ValueError(
            "An operator cannot be created from a class method, "
            "since the decorated function becomes an operator class"
        )

    # Gather data
    name = func.__name__
    module = func.__module__
    extra_doc = func.__doc__
    doc = extra_doc or f"Regular {name} stream operator."

    # Extract signature
    signature = inspect.signature(func)
    parameters = list(signature.parameters.values())
    return_annotation = signature.return_annotation
    if parameters and parameters[0].name in ("self", "cls"):
        raise ValueError(
            "An operator cannot be created from a method, "
            "since the decorated function becomes an operator class"
        )

    # Check for positional first parameter
    if not parameters or parameters[0].kind not in (
        inspect.Parameter.POSITIONAL_ONLY,
        inspect.Parameter.POSITIONAL_OR_KEYWORD,
    ):
        raise ValueError("The first parameter of the operator must be positional")

    # Look for "more_sources"
    for i, p in enumerate(parameters):
        if p.name == "more_sources" and p.kind == inspect.Parameter.VAR_POSITIONAL:
            more_sources_index = i
            break
    else:
        more_sources_index = None

    # Wrapped static method
    original_func = func
    original_func.__qualname__ = name + ".original"

    # Gather attributes
    class PipableOperatorImplementation:

        original = staticmethod(original_func)

        @staticmethod
        def raw(
            arg: AsyncIterable[X], /, *args: P.args, **kwargs: P.kwargs
        ) -> AsyncIterator[T]:
            assert_async_iterable(arg)
            if more_sources_index is not None:
                for source in args[more_sources_index - 1 :]:
                    assert_async_iterable(source)
            return func(arg, *args, **kwargs)

        def __call__(
            self, arg: AsyncIterable[X], /, *args: P.args, **kwargs: P.kwargs
        ) -> Stream[T]:
            assert_async_iterable(arg)
            if more_sources_index is not None:
                for source in args[more_sources_index - 1 :]:
                    assert_async_iterable(source)
            factory = functools.partial(self.raw, arg, *args, **kwargs)
            return Stream(factory)

        @staticmethod
        def pipe(
            *args: P.args,
            **kwargs: P.kwargs,
        ) -> Callable[[AsyncIterable[X]], Stream[T]]:
            return lambda source: operator_instance(source, *args, **kwargs)

        def __repr__(self) -> str:
            return f"{module}.{name}"

        def __str__(self) -> str:
            return f"{module}.{name}"

    # Customize raw method
    PipableOperatorImplementation.raw.__signature__ = signature  # type: ignore[attr-defined]
    PipableOperatorImplementation.raw.__qualname__ = name + ".raw"
    PipableOperatorImplementation.raw.__module__ = module
    PipableOperatorImplementation.raw.__doc__ = doc

    # Customize call method
    self_parameter = inspect.Parameter("self", inspect.Parameter.POSITIONAL_OR_KEYWORD)
    new_parameters = [self_parameter] + parameters
    new_return_annotation = (
        return_annotation.replace("AsyncIterator", "Stream")
        if isinstance(return_annotation, str)
        else return_annotation
    )
    PipableOperatorImplementation.__call__.__signature__ = signature.replace(  # type: ignore[attr-defined]
        parameters=new_parameters, return_annotation=new_return_annotation
    )
    PipableOperatorImplementation.__call__.__qualname__ = name + ".__call__"
    PipableOperatorImplementation.__call__.__name__ = "__call__"
    PipableOperatorImplementation.__call__.__module__ = module
    PipableOperatorImplementation.__call__.__doc__ = doc

    # Customize pipe method
    pipe_parameters = parameters[1:]
    pipe_return_annotation = f"Callable[[AsyncIterable[X]], {new_return_annotation}]"
    PipableOperatorImplementation.pipe.__signature__ = signature.replace(  # type: ignore[attr-defined]
        parameters=pipe_parameters, return_annotation=pipe_return_annotation
    )
    PipableOperatorImplementation.pipe.__qualname__ = name + ".pipe"
    PipableOperatorImplementation.pipe.__module__ = module
    PipableOperatorImplementation.pipe.__doc__ = (
        f'Piped version of the "{name}" stream operator.'
    )
    if extra_doc:
        PipableOperatorImplementation.pipe.__doc__ += "\n\n    " + extra_doc

    # Create operator singleton
    properly_named_class = type(
        name,
        (PipableOperatorImplementation,),
        {
            "__qualname__": name,
            "__module__": module,
            "__doc__": doc,
        },
    )
    operator_instance = properly_named_class()
    return operator_instance


def sources_operator(
    func: Callable[P, AsyncIterator[T]],
) -> SourcesOperator[P, T]:
    """Create a pipable stream operator from an asynchronous generator
    (or any function returning an asynchronous iterable) that takes
    a variadic ``*args`` of sources as argument.

    Decorator usage::

        @sources_operator
        async def chain(*sources, repeat=1):
            for source in (sources * repeat):
                async with streamcontext(source) as streamer:
                    async for item in streamer:
                        yield item

    Positional arguments are expected to be asynchronous iterables.

    When used in a pipable context, the asynchronous iterable injected by
    the pipe operator is used as the first argument.

    The return value is a dynamically created callable.
    It has the same name, module and documentation as the original function.

    A new stream is created by simply calling the operator::

        xs = chain()
        ys = chain(random())
        zs = chain(stream.just(0.0), stream.just(1.0), random())

    The original function is called right away (but not awaited) to check that
    signatures match. The sources are also checked for asynchronous iteration.

    The operator also have a `pipe` method that can be used with the pipe
    synthax::

        just_zero = stream.just(0.0)
        zs = just_zero | chain.pipe(stream.just(1.0), random())

    This is strictly equivalent to the previous ``zs`` example.

    Other methods are available:

      - `original`: the original function as a static method
      - `raw`: same as original with extra checking

    The raw method is useful to create new operators from existing ones::

        @sources_operator
        def chain_twice(*sources):
            return chain.raw(*sources, repeat=2)
    """
    # First check for classmethod instance, to avoid more confusing errors later on
    if isinstance(func, classmethod):
        raise ValueError(
            "An operator cannot be created from a class method, "
            "since the decorated function becomes an operator class"
        )

    # Gather data
    name = func.__name__
    module = func.__module__
    extra_doc = func.__doc__
    doc = extra_doc or f"Regular {name} stream operator."

    # Extract signature
    signature = inspect.signature(func)
    parameters = list(signature.parameters.values())
    return_annotation = signature.return_annotation
    if parameters and parameters[0].name in ("self", "cls"):
        raise ValueError(
            "An operator cannot be created from a method, "
            "since the decorated function becomes an operator class"
        )

    # Check for positional first parameter
    if not parameters or parameters[0].kind != inspect.Parameter.VAR_POSITIONAL:
        raise ValueError(
            "The first parameter of the sources operator must be var-positional"
        )

    # Wrapped static method
    original_func = func
    original_func.__qualname__ = name + ".original"

    # Gather attributes
    class SourcesOperatorImplementation:

        original = staticmethod(original_func)

        @staticmethod
        def raw(*args: P.args, **kwargs: P.kwargs) -> AsyncIterator[T]:
            for source in args:
                assert_async_iterable(source)
            return func(*args, **kwargs)

        def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Stream[T]:
            for source in args:
                assert_async_iterable(source)
            factory = functools.partial(self.raw, *args, **kwargs)
            return Stream(factory)

        @staticmethod
        def pipe(
            *args: P.args,
            **kwargs: P.kwargs,
        ) -> Callable[[AsyncIterable[Any]], Stream[T]]:
            return lambda source: operator_instance(source, *args, **kwargs)  # type: ignore

        def __repr__(self) -> str:
            return f"{module}.{name}"

        def __str__(self) -> str:
            return f"{module}.{name}"

    # Customize raw method
    SourcesOperatorImplementation.raw.__signature__ = signature  # type: ignore[attr-defined]
    SourcesOperatorImplementation.raw.__qualname__ = name + ".raw"
    SourcesOperatorImplementation.raw.__module__ = module
    SourcesOperatorImplementation.raw.__doc__ = doc

    # Customize call method
    self_parameter = inspect.Parameter("self", inspect.Parameter.POSITIONAL_OR_KEYWORD)
    new_parameters = [self_parameter] + parameters
    new_return_annotation = (
        return_annotation.replace("AsyncIterator", "Stream")
        if isinstance(return_annotation, str)
        else return_annotation
    )
    SourcesOperatorImplementation.__call__.__signature__ = signature.replace(  # type: ignore[attr-defined]
        parameters=new_parameters, return_annotation=new_return_annotation
    )
    SourcesOperatorImplementation.__call__.__qualname__ = name + ".__call__"
    SourcesOperatorImplementation.__call__.__name__ = "__call__"
    SourcesOperatorImplementation.__call__.__module__ = module
    SourcesOperatorImplementation.__call__.__doc__ = doc

    # Customize pipe method
    pipe_parameters = parameters
    pipe_return_annotation = f"Callable[[AsyncIterable[Any]], {new_return_annotation}]"
    SourcesOperatorImplementation.pipe.__signature__ = signature.replace(  # type: ignore[attr-defined]
        parameters=pipe_parameters, return_annotation=pipe_return_annotation
    )
    SourcesOperatorImplementation.pipe.__qualname__ = name + ".pipe"
    SourcesOperatorImplementation.pipe.__module__ = module
    SourcesOperatorImplementation.pipe.__doc__ = (
        f'Piped version of the "{name}" stream operator.'
    )
    if extra_doc:
        SourcesOperatorImplementation.pipe.__doc__ += "\n\n    " + extra_doc

    # Create operator singleton
    properly_named_class = type(
        name,
        (SourcesOperatorImplementation,),
        {
            "__qualname__": name,
            "__module__": module,
            "__doc__": doc,
        },
    )
    operator_instance = properly_named_class()
    return operator_instance