Search code examples
pythonasserttruthiness

Assertions and testing emptiness of iterables


Using the built-in assert statement, is there a good, Pythonic way of checking emptiness in an iterable? I have seen:

but I'm looking for a solution using assertions. The following looks to work, but I am unsure whether I'm missing some important possible exception:

a = [1, 2, 3]
b = []

assert a, 'a is empty'
assert b, 'b is empty'

Which raises this AssertionError:

Traceback (most recent call last):
  File "<ipython-input-9-185714f6eb3c>", line 5, in <module>
    assert b, 'b is empty'
AssertionError: b is empty

Solution

  • Remember not to confuse a container (think lists, sets, tuples, dictionaries, whatever) with an iterable (anything that can come up with a new state). The only way to check whether an iterable is "empty" is to try to iterate over it and find out if it does not produce at least one new state. You are free to interpret that as the iterable being "empty".

    Something like this

    def test_thing_is_empty(self):  # We are in a unittest.Testcase
        foo = iter([])  # Or any other iterable thing
        try:
            item = next(foo)
        except StopIteration:
            pass  # This is what should happen
        else:
            self.fail("Expected foo to be empty, got %s" % (item, ))
    

    Also remember that an iterable is technically never truly "empty". It is allowed, though discouraged, for an iterable to throw a StopIteration sometimes and all of the sudden start producing new elements if tried again.

    Therefore there is no way to assert this. You have to try and see if you fail. If you do fail, the only true knowledge is that the iterable did not produce a new state right now (for whatever reason). If you get an item back, the iterable just produced a unique, precious snowflake that may never be reproducable again (think reading from network socket somewhere down the stack).