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