Skip to content
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

add CanIterSelf[+V] and CanAIterSelf[+V] #14

Merged
merged 2 commits into from
Mar 16, 2024
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
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
19 changes: 18 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,15 @@ class CanIter[Vs: CanNext[Any]](Protocol):
def __iter__(self) -> Vs: ...


@runtime_checkable
class CanIterSelf[V](CanNext[V], CanIter[CanNext[V]], Protocol):
"""
Equivalent to `collections.abc.Iterator[T]`, minus the `abc` nonsense.
"""
@override
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 +103,6 @@ def __hash__(self) -> int: ...

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


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


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


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

Expand Down