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

Need fix for next vs __next__ iterator #136

Open
bhawkins opened this issue Mar 2, 2016 · 2 comments
Open

Need fix for next vs __next__ iterator #136

bhawkins opened this issue Mar 2, 2016 · 2 comments

Comments

@bhawkins
Copy link

bhawkins commented Mar 2, 2016

Classes that implement an iterator have to provide a next method in Python 2 but a __next__ method in Python 3. See

https://www.python.org/dev/peps/pep-3114/

The six package includes the class six.Iterator which you can subclass and provide only a __next__ method.

@daira
Copy link
Contributor

daira commented Aug 9, 2016

Is the desired fix to add six.Iterator to the end of the base class list of any class that statically defines a next method?

@bhawkins
Copy link
Author

bhawkins commented Aug 9, 2016

I think it'd be something like that, in addition to renaming the next method to __next__. I've attached a simple example. Output is below:

test.zip

$ python-modernize test_py2.py
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: No changes to test_py2.py
RefactoringTool: Files that need to be modified:
RefactoringTool: test_py2.py
$ python3 test_py2.py
Traceback (most recent call last):
  File "test_py2.py", line 19, in <module>
    for i in Foo(10):
TypeError: iter() returned non-iterator of type 'Foo'

I'm not sure you can always just add six.Iterator to the inheritance list. At least one special case is when the class inherits from object, e.g., Foo(object,Iterator) results in the exception

TypeError: duplicate base class object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants