Modern Python Projects Transcripts
Chapter: Writing code
Lecture: Flake8
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Another very popular Python Linter is called Flake 8. Flake 8 is actually a combination of three tools, pyflakes,
0:09
which gives you warnings about unused modules, undefined variables and stuff flake that pycodestyle that gives you warnings about,
0:18
Pep 8 violations and mccabe that gives you warnings about two high cyclomatic complexity of your functions. In other words,
0:27
it tells you if your functions are too complicated. So, if they contain too many nested loops, too many if statements and stuff flake that
0:34
But by default, mccabe Checker is disabled with Flake 8. So we only use Pyflakes and pycodestyle.
0:42
Let's go back to my ugly code and let's see what flake 8 things about it So first, make sure you have Flake 8 installed.
0:52
Don't forget to copy the path to flake 8, and now we can select Flake 8 as a Linter and we have a lot of
1:08
red underscores. So, let's see at the list of all the errors. Here, flake 8 complaints that we imported the non existing function,
1:20
but we haven't used it, so actually this is a kind of a wrong warning because this function doesn't exist. So, in this situation,
1:28
pylint was actually better, because it detected that this function doesn't exist. Let's remove it. os was imported but unused.
1:38
Great. Let's remove it. What's next? White space before closing bracket. Let's remove it. Expected one blank line. Let's add this blank line.
1:53
You know what? Let's actually run black on it because I don't want to manually Fix all the white spaces. Perfect. Some warnings went away.Here
2:01
We still have too many white spaces, but neither Flake 8 nor pylints complain about it.
2:06
Black also didn't change it because this is inside of a string and black doesn't touch
2:12
white spaces in a string, even though it would be better to actually remove those
2:16
additional white spaces. Here It correctly detected that the name is undefined and me is assigned but never used. So let's fix it.
2:31
pylint actually complained that we're using person without the argument, but flake 8 is not. But we still have this error here,
2:38
so it can point us in the direction that this is something there is something wrong with this code. Let's replace it and that's it.
2:46
We no longer have any errors here, so that's how flake 8 works. Compared to pylint. I personally find Flake 8,
2:54
much less strict than pylints. It didn't complain about the missing documentation or that our class had too few public methods.
3:02
But on the other hand, it didn't detect that invalid import at the beginning of our file. So each linter has its own pros and cons.