feat(builtins): make TextIOWrapper enumerable and readline arg optional#331
Merged
Conversation
Resolves #131. - Add `inherit IEnumerable<string>` to TextIOWrapper so file objects can be iterated line-by-line in a type-safe `for line in reader` loop without a cast. Maps to Python's iterator protocol; a file object is its own iterator. - Add zero-arg `readline` / `readlines` overloads on TextIOBase so the size argument is optional (Python's `readline(size=-1)`), matching the existing two-overload pattern used by `read`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Resolves #131.
Problem
builtins.open(...)returns aTextIOWrapper. Two ergonomic gaps were reported:!!result) to makefor line in resultcompile.readlineforced a size argument (result.readline(-1)), but Python'sreadline(size=-1)takes it optionally.Changes
src/stdlib/Builtins.fs:inherit IEnumerable<string>toTextIOWrapper. Per the bindings guide,IEnumerable<'T>maps to Python's iterator protocol, sofor line in readernow compiles directly and types eachlineasstring. A Python file object is its own iterator, so the generatedget_enumerator(reader)(→iter()) works at runtime.readline: unit -> stringandreadlines: unit -> string listoverloads onTextIOBase, matching the existing two-overload pattern already used byread.The reporter's snippet now works without the cast:
Testing
Added two tests to
test/TestBuiltins.fs(line enumeration + arg-lessreadline). Full suite passes — 563 passed.🤖 Generated with Claude Code