Using PyLint Feedback to Improve Your Code
You may come across certain messages PyLint returns to you, you don’t agree with. The purpose of this lesson is to show you a few examples, where this can happen.
00:00 All right, so now what I’m going to do is I’m going to go over this Pylint feedback here and I’m just going to fix these issues. So again, we have another code style issue here in line 12.
00:14
And then we’ve got a Missing module docstring
here, so let’s just turn that into a docstring.
00:26
And at any time, you can just rerun Pylint, right? There’s no harm in that. It’s not going to execute your program, it’s not going to do anything. It’s just going to inspect your code. All right, so Invalid class name "car"
.
00:38 I think that’s also interesting because, again, here what we want is an uppercase class name for this class if we want to follow the PEP 8 style guide, and that’s a very good recommendation, right?
00:51 So now what you are going to see in a minute, or I guess in a second, is that when I run this,
00:58
now we should actually get a new problem that tells me that car
isn’t defined, right? Because now car
doesn’t exist anymore. I just renamed the class from lowercase car
to uppercase Car
. Or like, it starts with an uppercase character now.
01:14
And now this code is broken. And Pylint told me about this, which is kind of nice. All right, so now the code is actually terrible, right? Like, now our score is really bad because we have some critical error in here that means the program wouldn’t actually run. So we have, like, a real error here—it’s not a warning, it’s a real error. All right, so let’s run this again. Okay, Missing class docstring
. All right, I’m just going to put a terrible docstring here, """Example class"""
. Right? It’s not a real good docstring. Okay, now we’re getting into some territory. So here, let me refresh this.
01:50 We’re getting into some territory where this is very, sort of, subjective feedback, right? So now it’s telling us that, “Hey! This class doesn’t have enough public methods.” And, I mean, that’s true, but then we’re sort of… You know, this might still make sense given the context of the program.
02:07 Pylint is very opinionated in that sense, and it’s going to tell you, it’s going to point out some things that you might disagree with. And that’s where it’s important to look into how to suppress some of these so that you can kind of tweak it and tune it to your liking.
02:24 If you’re doing this intentionally, then you might not want to see that error. And to a certain degree, that’s why I prefer Flake8, because Flake8 is only pointing out code style issues and things that are just not a good idea to do, right?
02:38 Like, it’s not going to tell you, “Hey, you didn’t define enough public methods on a class,” but it’s going to tell you that, you know, you made a formatting mistake or you’re doing something that is not recommended.
02:49
Like, for example, comparing to None
with the double equals operator (==
) versus the is
operator and stuff like that. Right?
02:56 So, it’s going to point out these things that should just be changed. And Pylint does that too, but it also adds stuff like this sort of subjective code feedback.
03:05 And, you know, this might be a good thing, it might be a bad thing. Like actually, I think for someone who’s just starting out to write Python or they want to get better at their Python—it can be a very helpful thing.
03:14
I actually used Pylint a lot in the past. And then, well, since the last couple of years, I switched over to Flake8 for pretty much that reason. So, you can always run both too, right? And that’s always a possibility. Anyway, let’s fix some of the remaining stuff here. Okay, I’ve got Invalid constant name 'my_car'
. Okay, because we’re not using that for anything, right? Because…
03:42 Oh wait, we are. Okay, so actually, in this case, because this is a top-level constant, we would want this to be uppercase, which—again, you know, this is something you might disagree with.
03:56 But let’s just fix all of the things that Pylint pointed out here.
04:02
Okay, Missing function docstring
…
04:07
"""An example function"""
. Okay, we did that. So now this is telling us about a missing or unused argument in this function definition here. As you can see here, we define car2
, but we’re never really using it in the function. And again, this might be intentional or this might not be intentional, and this is a good example to show you how we can suppress these errors in Pylint as well.
Vijay Iyer on Feb. 4, 2023
Is there a course or tutorial to setup flake8 with prominent editors?
Become a Member to join the conversation.
Mike K on June 7, 2020
Pylint is a good starting-point for beginners. There was a 3 month period where I had to work on an API written in Ruby and Rubocop (which is the Ruby equivalent of Pylint) taught me a lot of best practices.