__iter__ returns a collection or literal, which is not an iterator

tx0/python:iter-method-returns-collectiondefaultwarningbug

What it finds

def __iter__(self): return [self.a, self.b]: iter() requires __iter__ to return an iterator, an object with __next__, and raises TypeError: iter() returned non-iterator for a list, tuple, dict, set, string, number or None. Wrapping the value in iter(...), or writing __iter__ as a generator, returns an iterator.

From the rule's source, iter_method_returns_collection.rs.

Example from the rule's tests
class Pair:    def __init__(self, a, b):        self.a, self.b = a, b     def __iter__(self):        return [self.a, self.b]tx0 reports: [self.a, self.b] is not an iterator, so iter() raises TypeError on this object  class Fine:    def __iter__(self):        return iter([1, 2])  class Gen:    def __iter__(self):        yield 1        return None
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:iter-method-returns-collection" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:iter-method-returns-collection] reason="..."

tx0 explain tx0/python:iter-method-returns-collection shows the same in the terminal. Found a false positive? Report it.