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

Cursor is not an Iterator #992

Closed
sanchezg opened this issue Jul 27, 2021 · 4 comments · Fixed by #995
Closed

Cursor is not an Iterator #992

sanchezg opened this issue Jul 27, 2021 · 4 comments · Fixed by #995
Milestone

Comments

@sanchezg
Copy link
Contributor

Describe the bug

Trying to iterate over a set of results:

main_query = cursor.execute(QUERY, args=args)
result = next(main_query)

I get a:
TypeError: 'DictCursor' object is not an iterator

I think the problem is that BaseCursor implements __iter__ method and not the __next__ method, as the Python reference states: https://docs.python.org/3/glossary.html#term-iterator

I made a workaround:

class DataBaseCursor(pymysql.cursors.DictCursor):
    def __iter__(self):
        return self

    def __next__(self):
        return next(iter(self.fetchone, None))

And it works as expected.

I can upload a PR with that for review, what do you think?

@methane
Copy link
Member

methane commented Jul 27, 2021

Currently, Cursor is an iterable, not an iterator. So missing __next__ is not a bug. You can iterate over the cursor with for statement or iter(cursor).

But DB-API 2.0 says .next() optional method. It is iterator protocol in Python 2. So having __next__ makes sense to me.
Go ahead.

@sanchezg
Copy link
Contributor Author

Thanks for your response! And yes, you're right, my bad. In this case Cursor is an iterable. However I can work with having also __next__ implemented.

@methane
Copy link
Member

methane commented Aug 3, 2021

Please create a PR.

@sanchezg
Copy link
Contributor Author

sanchezg commented Aug 3, 2021

@methane I couldn't find a contribution guidelines, I just upload a PR but please do not hesitate in point me if I need to follow a particular template or complete any additional docs or tests.

EDIT: working on tests right now.

@methane methane changed the title TypeError: 'DictCursor' object is not an iterator Cursor is not an Iterator May 23, 2023
@methane methane added this to the v1.1 milestone May 23, 2023
@methane methane linked a pull request May 23, 2023 that will close this issue
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants