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

Make sliced return Iterator[T] instead of Iterator[Sequence[T]] #667

Merged
merged 1 commit into from Jan 16, 2023

Conversation

ad-chaos
Copy link
Contributor

We make sure that T can be sliced instead for it be a Sequence

Issue reference

Fixes #666

Changes

Adds a SupportsSlicing protocol to check if the type can be sliced instead

Tested against the following code:

from typing import assert_type, Iterator
from array import array
from more_itertools import sliced

assert_type(sliced(str(1), 1), Iterator[str])
assert_type(sliced(list(range(1)), 1), Iterator[list[int]])
assert_type(sliced(tuple(range(1)), 1), Iterator[tuple[int, ...]])
assert_type(sliced(range(1), 1), Iterator[range])
assert_type(sliced((1).to_bytes(), 1), Iterator[bytes])
assert_type(sliced(array("l", [1, 2, 3]), 1), Iterator[array[int]])
sliced({1: 1}, 1)  # error dict[int, int] cannot be assigned to SupportsSlicing[T]
sliced({1}, 1)  # error set[int] cannot be assigned to SupportsSlicing[T]

seq: Sequence[_T], n: int, strict: bool = ...
) -> Iterator[Sequence[_T]]: ...
seq: SupportsSlicing[_T], n: int, strict: bool = ...
) -> Iterator[_T]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as to why this is returning Iterator[_T] and not an iterator over things that themselves support slicing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is precisely what this is doing. sliced is a generic function that accepts any type that supports slicing and returns an iterator over that type.

@ad-chaos ad-chaos changed the title Make sliced return Iterator[T] instead of Iterator[Sequence[T]] Make sliced return Iterator[T] instead of Iterator[Sequence[T]] Jan 10, 2023
@bbayles
Copy link
Collaborator

bbayles commented Jan 13, 2023

I'm not sure why GitHub Actions is not running tests for this PR. However, when I run the checks myself, I get:

% stubtest more_itertools.more more_itertools.recipes
git oerror: more_itertools.more.SupportsSlicing is not present at runtime
Stub: at line 42
<TypeInfo more_itertools.more.SupportsSlicing>
Runtime:
MISSING

Found 1 error (checked 2 modules)

Could you determine what's causing this and correct it? Thanks.

We make sure that `T` can be sliced instead for it be a Sequence
@ad-chaos
Copy link
Contributor Author

Done. I also noticed that the _SizedReversible protocol is not used anywhere in the codebase.

@bbayles bbayles merged commit 4000ba9 into more-itertools:master Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sliced should preferably return Iterator[T] over Iterator[Sequence[T]]
2 participants