PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.05k stars 103 forks source link

feat(rules): Add rule to check for mutations of loop iterator #446

Closed mimre25 closed 5 months ago

mimre25 commented 5 months ago

This implements a POC for #445.

I opted to go the route of alerting for any mutations of a loop iterator.

Note: I picked B999 temporarily, not sure how the numbers are picked. Also, I'm very much open to change the error message.

Would be happy if you could give this a review @cooperlees :slightly_smiling_face:

sahuagin commented 5 months ago

I have an example for a false positive (I hope), if it's desired. Setup. A CSV file as input with the first item in each row being an index number.

        id_pos = 0
        # lf is LogFile not LazyFrame
        lf = self._logger
        with self.rlock:
            lf.seek(0, 0)
            cpos = lf.tell()
            for line in lf:
                if len(line.strip()) == 0:
                    continue
                sl = line.split(b",", 1)
                self._mapper[sl[id_pos]] = cpos
                cpos = lf.tell()

Desired behavior is to find all indexes and record the file position where they start. This just started getting flagged, which led me here. There are 4 more flags in the same file, tell() seek() x2 write()

JelleZijlstra commented 5 months ago

@sahuagin thanks for the report, #453 should fix this.