Modern Python Projects Transcripts
Chapter: Writing code
Lecture: Pylint

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So, I'm back to the original version of my ugly code,
0:02 and I will try to install,
0:04 Pylint and run it. So first we have to open the command palette and search
0:07 for select linter, this one python Select Linter and we want to select Pylint
0:13 As you can see, nothing really happened because we don't have Pylint installed.
0:17 So, let's go and install it with pipx,
0:23 Just like with black. Let's copy the path to the binary and let's add it
0:27 to the settings, search for pylint path and copy it here.
0:33 As you can see, I already did that in the past.
0:38 It can also happen that when you select Pylint Linter,
0:41 you will get a pop up here saying that Pylint is not installed.
0:44 Do you want to install it?
0:46 You can press okay and VSCode will download and install Pylint for you and
0:51 that will also work that way.
0:53 You don't have to modify any settings in the configuration file.
0:57 I usually don't install packages directly through VSCode because that way I don't really
1:02 know where VSCode, installs them. If I want to go and update those
1:07 packages I usually can't find them later.
1:10 So, I prefer to install packages first and then point VSCode to use them
1:13 But feel free to just click okay in the pop up.
1:16 when VSCode will set up everything for you.
1:19 So, we have selected Pylint. Let's now enable Linting.
1:24 So, we have Linting enabled and nothing really happened.
1:27 So, let's see what's wrong. There are no problems.
1:35 What if we actually run Pylint on this code in the terminal?
1:44 Ah, Great. My code was rated at -5.
1:47 Out of -10 out of 10.
1:49 That's great score. So in the terminal we see a lot of warnings,
1:53 but not in the VSCode.
1:55 Let's see what's wrong. VSCode.
1:57 Has this output tab with output from different extensions and different parts of VSCode
2:02 So, usually you can do this to check if everything is working fine of or
2:07 if there are some errors. I don't see any error about Pylint not being found
2:11 So, let's let's just try to added one more time as a Linter.
2:20 Okay, This time it worked.
2:22 We can see more red underscores quickly errors.
2:25 So, let's see what pylint is reporting, it correctly detected that we don't have non existing
2:31 function in itertools. So let's remove that, undefined variable.
2:36 So that's correct. Let's try to define this variable. unused
2:43 variable here and here. No value for arguments,
2:46 cell. So this already can point us into direction of what might be wrong.
2:50 So with those two information, I can see like Okay,
2:53 I forgot to pass me here.
2:56 try again. No problems anymore.
2:58 So, it didn't detect that it should be double underscore Main(__main__).
3:01 But this is kind of tricky error to spot, if we fix it and we can
3:08 run it working fine. So, as you can see with Pylint,
3:13 we were able to spot a lot of errors before we run our code the first
3:17 time. If you have used Pylint outside of VSCode,
3:22 you might be surprised why Pylint is not complaining about some of the additional problems like
3:28 missing white spaces or additional spaces here and there to show you what I mean.
3:34 Let's run Pylint in the terminal again on this ugly2 file.
3:38 That, according to VSCode,
3:40 has no more problems. As you can see here. There are still some other problems
3:50 that we have to fix, like missing docstrings in the module,
3:54 in the class or in the function,
3:56 too few public methods, missing functional method docstring and so on.
4:02 That's because when you check the documentation of VSCode,
4:04 you can see that VSCode has some default Pylint rules that are supposed to be
4:10 friendly to the largest number of python developers.
4:14 The thing with Pylint is that it's quite strict.
4:17 It complains about missing documentation about too few public methods,
4:22 or even if you create a class without init method,
4:25 it will still complain about that.
4:27 So, it really tries to make you right perfect,
4:29 well documented code. But sometimes you know better what you're doing.
4:34 If you're writing a simple script that you are going to use once,
4:37 then maybe you don't feel like documenting every function.
4:41 On the other hand, if you want to enable all Pylint warnings,
4:45 you can go to the VSCode documentation,
4:48 and here you can see that there is pylintUseMinimalCheckers option that is currently
4:52 set to true when we set it to false,
4:55 then Pylint will by default, show us all the warnings.
4:58 So, let's try that settings set for this guy.
5:08 And now you can see we have all the warnings and use import,
5:14 missing documentation and too few public methods, missing documentation.
5:19 But for example, it's not complaining about additional white spaces here,
5:23 so those are all the warnings, that we will see with the default Pylint settings.
5:27 But Pylint has also some additional checkers that are disabled by default,
5:32 but you can enable them. When you go to the Pylint documentation,
5:37 technical reference and optional Pylint checkers,
5:41 This option is quite well hidden documentation.
5:44 So, I'll put a link in the resources Page.
5:47 And here you can see some optional Pylint checkers that you can enable, to enable those
5:52 additional checkers. You will need to create a .pylintrc file and add
5:58 them under the load plugins parameter.
6:00 Some of the checkers here are very useful.
6:02 For example, there is one that will warn you when you use a Deprecated
6:06 builtin function. So the function that will be soon removed from python or there is
6:12 another one that will complain when you use else-if
6:15 instead of elif statement. So, those optional checkers can further help you when
6:20 you write your code, on top of the plugins that come with pylint,
6:24 there are also plug ins that you can install separated from Pypi.
6:28 For example, if you are using a Web framework like Django,
6:32 you can install a Pylint plugin called Pylint Django.
6:36 It will suppress some warnings that don't apply to your python code, when you're writing a
6:40 Django website, and it will also add some new checks specific to Django code.
6:45 Unfortunately, I haven't found like one repository that contains the list of different Pylint plugins
6:51 So usually you have to search for them on pypi or just Google
6:55 for them to find the interesting ones.