Skip to content

Commit

Permalink
add CanIterSelf[+V] and CanAIterSelf[+V]
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenham committed Mar 16, 2024
1 parent 9a4aa7a commit 3d13b25
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@ from the abracadabra collections. This is how they are defined:
</tr>
</table>

For the sake of compatibility with `collections.abc`, there is
`optype.CanIterSelf[T]`, which is a protocol whose `__iter__` returns
`typing.Self`, as well as a `__next__` method that returns `T`.
I.e. it is equivalent to `collections.abc.Iterator[T]`, but without the `abc`
nonsense.

### Async Iteration

Yes, you guessed it right; the abracadabra collections made the exact same
Expand Down Expand Up @@ -918,7 +924,7 @@ But fret not; the `optype` alternatives are right here:
<td><code>do_aiter</code></td>
<td><code>DoesAIter</code></td>
<td><code>__aiter__</code></td>
<td><code>CanANext[Vs: CanAnext]</code></td>
<td><code>CanAIter[Vs: CanAnext]</code></td>
</tr>
</table>

Expand All @@ -930,6 +936,9 @@ liberal). For details, see the discussion at [python/typeshed#7491][AN].
Just because something is legal, doesn't mean it's a good idea (don't eat the
yellow snow).

Additionally, there is `optype.CanAIterSelf[V]`, with both the
`__aiter__() -> Self` and the `__anext__() -> V` methods.

[AN]: https://github.com/python/typeshed/pull/7491

### Containers
Expand Down
4 changes: 4 additions & 0 deletions optype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'CanAEnter',
'CanAExit',
'CanAIter',
'CanAIterSelf',
'CanANext',
'CanAbs',
'CanAdd',
Expand Down Expand Up @@ -52,6 +53,7 @@
'CanInt',
'CanInvert',
'CanIter',
'CanIterSelf',
'CanLe',
'CanLen',
'CanLengthHint',
Expand Down Expand Up @@ -280,6 +282,7 @@
CanAEnter,
CanAExit,
CanAIter,
CanAIterSelf,
CanANext,
CanAbs,
CanAdd,
Expand Down Expand Up @@ -330,6 +333,7 @@
CanInt,
CanInvert,
CanIter,
CanIterSelf,
CanLe,
CanLen,
CanLengthHint,
Expand Down
17 changes: 16 additions & 1 deletion optype/_can.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import (
Any,
Protocol,
Self,
overload,
override,
runtime_checkable,
Expand All @@ -13,6 +14,7 @@
# Iterator types
# https://docs.python.org/3/library/stdtypes.html#iterator-types


@runtime_checkable
class CanNext[V](Protocol):
"""
Expand All @@ -28,6 +30,14 @@ class CanIter[Vs: CanNext[Any]](Protocol):
def __iter__(self) -> Vs: ...


@runtime_checkable
class CanIterSelf[V](CanNext[V], Protocol):
"""
Equivalent to `collections.abc.Iterator[T]`, minus the `abc` nonsense.
"""
def __iter__(self) -> Self: ...


# 3.3.1. Basic customization
# https://docs.python.org/3/reference/datamodel.html#basic-customization

Expand Down Expand Up @@ -92,7 +102,6 @@ def __hash__(self) -> int: ...

@runtime_checkable
class CanLt[X, Y](Protocol):
""""""
def __lt__(self, __x: X) -> Y: ...


Expand Down Expand Up @@ -644,6 +653,12 @@ class CanAIter[Y: CanANext[Any]](Protocol):
def __aiter__(self) -> Y: ...


@runtime_checkable
class CanAIterSelf[V](CanANext[V], Protocol):
"""A less inflexible variant of `collections.abc.AsyncIterator[T]`."""
def __aiter__(self) -> Self: ...


# 3.4.4. Asynchronous Context Managers
# https://docs.python.org/3/reference/datamodel.html#asynchronous-context-managers

Expand Down

0 comments on commit 3d13b25

Please sign in to comment.