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.
0:03 Flake 8 is actually a combination of three tools, pyflakes,
0:08 which gives you warnings about unused modules,
0:11 undefined variables and stuff flake that pycodestyle that gives you warnings about,
0:17 Pep 8 violations and mccabe that gives you warnings about two high cyclomatic complexity of
0:24 your functions. In other words,
0:26 it tells you if your functions are too complicated.
0:29 So, if they contain too many nested loops, too many if statements and stuff flake that
0:33 But by default, mccabe Checker is disabled with Flake 8.
0:38 So we only use Pyflakes and pycodestyle.
0:41 Let's go back to my ugly code and let's see what flake 8 things about it
0:45 So first, make sure you have Flake 8 installed.
0:51 Don't forget to copy the path to flake 8,
1:04 and now we can select Flake 8 as a Linter and we have a lot of
1:07 red underscores. So, let's see at the list of all the errors.
1:14 Here, flake 8 complaints that we imported the non existing function,
1:19 but we haven't used it, so actually this is a kind of a wrong warning
1:23 because this function doesn't exist. So, in this situation,
1:27 pylint was actually better, because it detected that this function doesn't exist.
1:33 Let's remove it. os was imported but unused.
1:37 Great. Let's remove it. What's next?
1:42 White space before closing bracket. Let's remove it.
1:47 Expected one blank line. Let's add this blank line.
1:52 You know what? Let's actually run black on it because I don't want to manually
1:54 Fix all the white spaces.
1:57 Perfect. Some warnings went away.Here
2:00 We still have too many white spaces,
2:01 but neither Flake 8 nor pylints complain about it.
2:05 Black also didn't change it because this is inside of a string and black doesn't touch
2:11 white spaces in a string, even though it would be better to actually remove those
2:15 additional white spaces. Here It correctly detected that the name is undefined and me is
2:22 assigned but never used. So let's fix it.
2:30 pylint actually complained that we're using person without the argument,
2:34 but flake 8 is not.
2:35 But we still have this error here,
2:37 so it can point us in the direction that this is something there is something wrong
2:41 with this code. Let's replace it and that's it.
2:45 We no longer have any errors here,
2:48 so that's how flake 8 works.
2:49 Compared to pylint. I personally find Flake 8,
2:53 much less strict than pylints. It didn't complain about the missing documentation or that our
2:58 class had too few public methods.
3:01 But on the other hand, it didn't detect that invalid import at the beginning of
3:05 our file. So each linter has its own pros and cons.