Python logoPython/
PYL-E1133

Non-iterable value used in an iterating contextPYL-E1133

Critical severityCritical
Bug Risk categoryBug Risk

A non-iterable value is being used in an iterating context, For example, a non-iterable value being passed into a for loop. This will raise a TypeError.

Bad practice

Using a non-iterable as object in a loop.

def fx(val=None):
    return val


seq = fx()
for val in seq:
    print(val)

Because seq would be None which is not an iterable, this code snippet will raise an error.

Make sure only an iterable is passed to the iterator. In our example, tweaking fx to always return an iterable will fix the problem:

def fx(val=None):
    val = val or []
    return val


seq = fx()
for val in seq:
    print(val)