Skip to content

feat(builtins): make TextIOWrapper enumerable and readline arg optional#331

Merged
dbrattli merged 1 commit into
mainfrom
fix/textiowrapper-enumerable-131
Jun 25, 2026
Merged

feat(builtins): make TextIOWrapper enumerable and readline arg optional#331
dbrattli merged 1 commit into
mainfrom
fix/textiowrapper-enumerable-131

Conversation

@dbrattli

Copy link
Copy Markdown
Collaborator

Resolves #131.

Problem

builtins.open(...) returns a TextIOWrapper. Two ergonomic gaps were reported:

  1. Iterating a file's lines required an untyped cast (!!result) to make for line in result compile.
  2. readline forced a size argument (result.readline(-1)), but Python's readline(size=-1) takes it optionally.

Changes

src/stdlib/Builtins.fs:

  • Add inherit IEnumerable<string> to TextIOWrapper. Per the bindings guide, IEnumerable<'T> maps to Python's iterator protocol, so for line in reader now compiles directly and types each line as string. A Python file object is its own iterator, so the generated get_enumerator(reader) (→ iter()) works at runtime.
  • Add zero-arg readline: unit -> string and readlines: unit -> string list overloads on TextIOBase, matching the existing two-overload pattern already used by read.

The reporter's snippet now works without the cast:

use reader = builtins.``open`` ("test.txt", OpenTextMode.Read)
for (line: string) in reader do
    builtins.print (line.Trim())

Testing

Added two tests to test/TestBuiltins.fs (line enumeration + arg-less readline). Full suite passes — 563 passed.

🤖 Generated with Claude Code

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>
@dbrattli dbrattli merged commit 2cdb42e into main Jun 25, 2026
9 checks passed
@dbrattli dbrattli deleted the fix/textiowrapper-enumerable-131 branch June 25, 2026 19:26
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.

allow TextIOWrapper to be enumerated

1 participant