easy to miss bug

class Filter():
    def __init__(self, max_days):
        self.max_days = max_days

    def execute(self, asset):
        self.max_days = getOverrides('max_days', asset.type, default=self.max_days)

        if isAssetOlder(asset, self.max_days):
            return True


if __name__ == '__main__':
    filter = Filter(10)

    for asset in getAssets():
        if filter.execute(asset):
            delete(asset)

can you see the problem in the above code?

The code looks fine, unittests pass, good to deploy to the production server. Well, this is what I almost did and garbage collected most of the project. Ups.

answer

The gotcha here is that we only have one instance of the Filter class that is reused in the for loop. The moment we override self.max_days in the execute it never resets to the default value and gets applied to everything in the loop that comes after.

A lesson for me here: do not override instance variables without a good reason.