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

Calling next on an iterator over native Python containers should have Python side-effects #278

Open
khatchad opened this issue Nov 1, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@khatchad
Copy link
Member

khatchad commented Nov 1, 2023

Consider the following code:

# From  https://www.tensorflow.org/guide/function#using_python_iterators_and_generators

import tensorflow as tf


@tf.function
def buggy_consume_next(iterator):
  tf.print("Value:", next(iterator))


iterator = iter([1, 2, 3])

buggy_consume_next(iterator)
# This reuses the first value from the iterator, rather than consuming the next value.
buggy_consume_next(iterator)
buggy_consume_next(iterator)

Above, iterator is over a Python container (a list here). Calling next() on the iterator moves its cursor over the container. Thus, that could be considered a Python side-effect, but only because the underlying container is a native Python container.

Regression

I believe is a type inferencing problem. The function call to iter() above will return a certain kind of iterator depending on its argument's type:

>>> type(iter([1,2,3]))
<class 'list_iterator'>
@khatchad khatchad added the bug Something isn't working label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant