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 IteratorSize(::Iterators.Cycle) not always IsInfinite and document why #54187

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions base/generator.jl
Expand Up @@ -83,12 +83,24 @@
This trait is generally used to select between algorithms that pre-allocate space for their
result, and algorithms that resize their result incrementally.

Most iterators' `IteratorSize` category can be determined from their type. In this case
the generic `IteratorSize(iterator) = IteratorSize(typeof(iterator))` fallback applies.
However, some iterators (e.g. [`Iterators.cycle`](@ref)) have a size category that can only
be determined at runtime. In this case the `IteratorSize(itr)` method will not be type
stable and the `IteratorSize(::Type)` method will return `SizeUnkown`.

Check warning on line 90 in base/generator.jl

View workflow job for this annotation

GitHub Actions / Check for new typos

perhaps "Unkown" should be "Unknown".
LilithHafner marked this conversation as resolved.
Show resolved Hide resolved

```jldoctest
julia> Base.IteratorSize(1:5)
Base.HasShape{1}()

julia> Base.IteratorSize((2,3))
Base.HasLength()

julia> Base.IteratorSize(Iterators.cycle(1:5))
Base.IsInfinite()

julia> Base.IteratorSize(Iterators.cycle(1:0))
Base.HasLength()
```
"""
IteratorSize(x) = IteratorSize(typeof(x))
Expand Down
8 changes: 7 additions & 1 deletion base/iterators.jl
Expand Up @@ -988,7 +988,13 @@ cycle(xs, n::Integer) = flatten(repeated(xs, n))

eltype(::Type{Cycle{I}}) where {I} = eltype(I)
IteratorEltype(::Type{Cycle{I}}) where {I} = IteratorEltype(I)
IteratorSize(::Type{Cycle{I}}) where {I} = IsInfinite() # XXX: this is false if iterator ever becomes empty
function IteratorSize(::Type{Cycle{I}}) where I
# TODO: find a better way of communicating the size of a cycle
# IsInfinite() would be false if iterator ever becomes empty
IteratorSize(I) === IsInfinite() ? IsInfinite() : SizeUnknown()
end
IteratorSize(it::Cycle) = isempty(it.xs) ? HasLength() : IsInfinite()
Copy link
Member

Choose a reason for hiding this comment

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

Do we have other examples where IteratorSize(it) and IteratorSize(typeof(it)) (potentially) disagree? Makes me a bit uncomfortable.

And if it.xs is stateful, it seems that IsInfinite might turn out to be wrong, because isempty(it.xs) might become true.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have other examples where IteratorSize(it) and IteratorSize(typeof(it)) (potentially) disagree? Makes me a bit uncomfortable.

No, this is the first case, and changes the docs to suggest the possibility.

And if it.xs is stateful, it seems that IsInfinite might turn out to be wrong, because isempty(it.xs) might become true.

IMO that's because Iterators.cycle on stateful iterators is broken. It is explicitly documented to be infinite for non-empty arguments.

length(it::Cycle) = isempty(it.xs) ? 0 : throw(ArgumentError("Cannot compute length of infinite iterator"))

iterate(it::Cycle) = iterate(it.xs)
isdone(it::Cycle) = isdone(it.xs)
Expand Down
12 changes: 12 additions & 0 deletions test/iterators.jl
Expand Up @@ -1049,3 +1049,15 @@ end
@testset "Iterators docstrings" begin
@test isempty(Docs.undocumented_names(Iterators))
end

@testset "cycle IteratorSize (#53169)" begin
@test Base.IteratorSize(Iterators.cycle(1:3)) == Base.IsInfinite()
@test Base.IteratorSize(Iterators.cycle(1:0)) == Base.HasLength()
@test length(Iterators.cycle(1:0)) == 0
@test collect(Iterators.cycle(1:0))::Vector{Int} == Int[]
@test Base.IteratorSize(Iterators.cycle(Iterators.Repeated(6))) == Base.IsInfinite()

@test Base.IteratorSize(typeof(Iterators.cycle(1:3))) == Base.SizeUnknown()
@test Base.IteratorSize(typeof(Iterators.cycle(1:0))) == Base.SizeUnknown()
@test Base.IteratorSize(typeof(Iterators.cycle(Iterators.Repeated(6)))) == Base.IsInfinite()
end