Modern Python Projects Transcripts
Chapter: Writing code
Lecture: Flake8 plugins

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the reasons for Flake 8 popularity.
0:02 It's it's massive catalog of plugins that you can use to customize it.
0:07 There is a GitHub repository called Awesome Flake 8 extensions, that contains the list of
0:13 most popular plugins, and I definitely recommend that you check it out.
0:17 Some extensions will modify the default behavior of flake 8 and others will give you some
0:23 additional options and checkers that you can use.
0:26 I chose a few plugins and listed them under the resources page at
0:31 modernpythonprojects.com/resources, with a short description for each of them.
0:36 Let's quickly take a look at What do we have here?
0:40 First we have flake8-bugbear.
0:42 It adds some additional checks that can help you find some possible bugs and design problems
0:47 in your code. For example,
0:50 it complains when you just do,
0:51 except, without specifying the exception type or it complaints when you write +(+(n))because
0:58 that's not how you implement a variable by one in python.
1:02 So, that's a mistake that you can do when you move to python from a
1:04 different programming language. You can see the full list of different warnings.
1:08 Here, you can see there is quite a lot of them,
1:11 and there are even some opinionated warnings.
1:14 Opinionated means that the author things they are useful.
1:17 But maybe other programmers don't agree,
1:19 so those checks are disabled by default.
1:22 Next, we have, flake8 Builtins,
1:24 and this one makes sure that you don't use python builtins as variables or parameters.
1:30 So, for example, when you use list as the name of the argument in
1:34 the function, you're basically shadowing the built in list function from python.
1:39 So that's something that you should avoid in your code.
1:43 Next, we have a flake 8 plugin that can help you write better list,
1:46 set and dictionary comprehensions. It will analyze your code and give you suggestions in
1:51 which cases you should rewrite your code as a comprehension and in which case is you
1:56 should actually avoid using comprehension. So this can be actually very useful.
2:01 If you're new to python, comprehensions,
2:03 and you're having a hard time trying to,
2:06 understand how they work and where you should use them.
2:10 Next, we have flake8-docstrings,
2:12 and this will enforce rules from Pep257. So, this is the pep that tells you
2:18 how to write documentation strings in your code.
2:21 Next, we have Flake8-
2:22 eradicate, on this plugin will try to find a commented out code and complain about
2:27 it. So if you forgot to remove some code that you commented out,
2:31 you can easily spot it. Next.
2:34 We have Flake8-isort.
2:36 This plugin integrates the isort to with,
2:38 flake 8. And isort is a tool that will check if your import statements
2:44 are organized according to the pep8 guidelines.
2:49 Next there is flake8-broken-line.
2:52 This plugin will complain when you try to use a backslash for line breaks instead
2:56 of using something better, like parenthesis or a triple quote.
3:00 So, in the first example, instead of using a single quote and the line break
3:04 we could use a triple quote instead in the second example.
3:07 Instead, of adding the line break.
3:09 We could either put this if statement on one line or use the parenthesis and then
3:14 for using method changing. You actually don't need to add the backslash and so on
3:18 Then we have flake8-rst-docstrings.
3:23 If you're writing documentation with strings,
3:25 then you are probably writing documentation using the 'rst'.
3:28 So the restructure text format. It's kind of similar to mark down,
3:32 but it has some different things.
3:35 So, with this flake8-rst-docstrings,
3:38 you could get Flake 8 to check the docstrings of your function.
3:41 If the rst format is correct,
3:44 it's quite easy to make a mistake and use,
3:46 for example, the markdown link style instead of the rst,
3:50 so this plugin can be quite useful.
3:54 Next, we have darglint,
3:55 which is a plugin for pep 8 that will check if the docstring description matches
4:00 the function definitions. Here is an example.
4:04 If you have a function with self and arg1 arguments and then in the docstring
4:09 you forgot to document the self argument,
4:12 then this plugin will complain.
4:14 Also, it might complain if you forget to specify the return statement as in the
4:18 above example. So this is a very good plugin because when you're modifying the code
4:22 sometimes you forget to update the documentation.
4:25 Maybe you modify the name of a function argument or you add or remove the argument
4:30 from the function signature and then you forget to update the documentation,
4:34 which is quite a common mistake.
4:35 So, with this plugin, you can get a warning that your documentation is not corresponding
4:40 to what the function looks like.
4:43 And finally, we have flake8-mutable.
4:45 This is a nice little plugin that will warn you when you use
4:50 a mutable default argument in a function.
4:53 For example, if you defined a function like here,
4:55 then each time you call this function,
4:57 the 'b' argument will be always pointing to the same dictionary.
5:02 So, you might call this function five times thinking that each time you have a different b
5:06 B. But when you try to modify it,
5:08 it turns out that you will always be referencing the same dictionary between all those different
5:13 five calls. So that's something that can be tricky when you're new to Python.