Skip to content

Make Cursor an iterator conforming to DB-API 2.0#785

Merged
methane merged 4 commits into
mainfrom
copilot/make-cursor-iterator
Jun 10, 2026
Merged

Make Cursor an iterator conforming to DB-API 2.0#785
methane merged 4 commits into
mainfrom
copilot/make-cursor-iterator

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

This PR updates the Cursor classes to fully support the standard Python iterator protocol, aligning the behavior with PEP-249 and parity with PyMySQL (referencing PyMySQL#995).

Changes made

  • Implemented Iterator Protocol in BaseCursor: Added __iter__ to return self and __next__ to yield rows using fetchone(), raising StopIteration when the result set is exhausted.
  • Cleaned up MixIns: Removed redundant and non-standard __iter__ and next() override implementations from CursorStoreResultMixIn and CursorUseResultMixIn.
  • Added Tests: Verified iterator compatibility for both Cursor and SSCursor.

Example usage

cursor.execute("SELECT id, name FROM test_table")

# The cursor is now a direct iterator
assert iter(cursor) is cursor

# Can be used directly in loops
for row in cursor:
    print(row)

Copilot AI linked an issue Jun 10, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Implement cursor iterator as per instructions Make Cursor an iterator conforming to DB-API 2.0 Jun 10, 2026
Copilot AI requested a review from methane June 10, 2026 06:53
@methane methane force-pushed the copilot/make-cursor-iterator branch from f4eb719 to 1979971 Compare June 10, 2026 08:11
@methane methane marked this pull request as ready for review June 10, 2026 08:17
@methane methane merged commit b233d95 into main Jun 10, 2026
9 checks passed
@methane methane deleted the copilot/make-cursor-iterator branch June 10, 2026 08:41
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.

Make Cursor Itarator

2 participants