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