Repository navigation
Add Enumerator::Lazy signatures for the methods it overrides - #3285
Open
hasannadeem wants to merge 1 commit into
Open
hasannadeem wants to merge 1 commit into
hasannadeem wants to merge 1 commit into
Conversation
Lazy only declared compact, eager and force, so map, select, take and the other methods that return a lazy enumerator were typed by Enumerable as returning an Array, and calling force or eager on the result was a type error.
ksss
reviewed
Nov 14, 2026
| # | ||
| def zip: [U] (_Each[U] arg) -> Enumerator::Lazy[[ E, U? ]] | ||
| | (_Each[untyped], *_Each[untyped]) -> Enumerator::Lazy[Array[untyped]] | ||
| | [U] (_Each[U]) { ([ E, U? ]) -> void } -> nil |
Collaborator
There was a problem hiding this comment.
With a block, lazy_zip delegates to Enumerable#zip via rb_call_super, but this def replaces all of Enumerable#zip's overloads. So the block form with multiple arguments, which type-checks on master, would no longer match:
(1..3).lazy.zip([4], [5]) { |a| p a } # => nil at runtimeHow about copying the remaining block overload from Enumerable#zip?
Suggested change
| | [U] (_Each[U]) { ([ E, U? ]) -> void } -> nil | |
| | [U] (_Each[U]) { ([ E, U? ]) -> void } -> nil | |
| | (_Each[untyped], *_Each[untyped]) { (Array[untyped]) -> void } -> nil |
A test case for it in test_zip would be nice too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enumerator::Lazyonly declaredcompact,eagerandforce. Every other method it overrides fell back toEnumerable, so a lazy chain was typed as anArrayafter its first step, and callingforceoreageron it was an error:With this change Steep infers
::Enumerator::Lazy[::String, void],::Array[::String]and::Enumerator[::String, void]for those three lines.This adds the methods
Enumerator::Lazydefines (Enumerator::Lazy.instance_methods(false)), exceptto_enumandenum_for:map,flat_map,select,reject,filter_map,take,take_while,drop,drop_while,grep,grep_v,uniq,with_indexandzip, plus thecollect,collect_concat,filterandfind_allaliaseschunk,chunk_while,slice_after,slice_beforeandslice_when, whichEnumerabletypes as a plainEnumeratorlazy, which returnsselfThe parameters follow the
Enumerablesignatures, with some differences:ArgumentErrorwithout a block (map,select,take_while,chunk_while, …).R, asEnumerable#lazydoes.eachwith a block on a derived lazy enumerator returnsnil, not the receiver'sR.takeanddroptake anint.with_indextakes anInteger?offset.nilmeans 0, and a non-Integer offset is yielded as the first index unconverted.zipwith a block is eager and returnsnil, as inEnumerable#zip.The doc comments are what
rake annotateproduces from Ruby v4.0.7'senumerator.c.Tests: 18 of the new tests fail without the signature change. The tests for
chunk,chunk_while, theslice_*methods andlazypass either way, because a Lazy is also anEnumerator; the Steep check above is what shows the difference there.rake validate,rake typecheck_testandrake stdlib_testpass. The 16stdlib_testerrors on Ruby 3.4 also occur on master.