Pylint Documentation Release 2.7.3
Total Page:16
File Type:pdf, Size:1020Kb
Pylint Documentation Release 2.7.3 Logilab, PyCQA and contributors Mar 29, 2021 Contents 1 Introduction 3 1.1 What is Pylint?..............................................3 1.2 What Pylint is not?............................................3 2 Tutorial 5 2.1 Intro...................................................5 2.2 Getting Started..............................................5 2.3 Your First Pylint’ing...........................................6 2.4 The Next Step..............................................8 3 User Guide 11 3.1 Installation................................................ 11 3.2 Running Pylint.............................................. 11 3.3 Pylint output............................................... 14 3.4 Messages control............................................. 16 3.5 Configuration............................................... 19 3.6 Editor and IDE integration........................................ 21 4 How To Guides 27 4.1 How to Write a Checker......................................... 27 4.2 How To Write a Pylint Plugin...................................... 31 4.3 Transform plugins............................................ 32 5 Technical Reference 35 5.1 Startup and the Linter Class....................................... 35 5.2 Checkers................................................. 35 5.3 Optional Pylint checkers in the extensions module........................... 35 5.4 Pylint features.............................................. 43 5.5 Pylint and C extensions......................................... 74 6 Development 75 6.1 Contributing............................................... 75 7 Frequently Asked Questions 79 7.1 1. About Pylint.............................................. 79 7.2 2. Installation............................................... 79 7.3 3. Running Pylint............................................. 80 i 7.4 4. Message Control............................................ 81 7.5 5. Classes and Inheritance........................................ 82 7.6 6. Troubleshooting............................................ 82 8 Some projects using Pylint 85 9 Support 87 10 What’s New in Pylint 89 10.1 What’s New in Pylint 2.8......................................... 89 10.2 What’s New in Pylint 2.7......................................... 89 10.3 What’s New in Pylint 2.6......................................... 91 10.4 What’s New in Pylint 2.5......................................... 91 10.5 What’s New in Pylint 2.4......................................... 93 10.6 What’s New in Pylint 2.3......................................... 96 10.7 What’s New in Pylint 2.2......................................... 97 10.8 What’s New in Pylint 2.1......................................... 98 10.9 What’s New in Pylint 2.0......................................... 99 10.10 What’s New In Pylint 1.9........................................ 104 10.11 What’s New In Pylint 1.8........................................ 106 10.12 What’s New In Pylint 1.7........................................ 111 10.13 What’s New In Pylint 1.6........................................ 124 10.14 Pylint’s ChangeLog........................................... 127 Index 197 ii Pylint Documentation, Release 2.7.3 Pylint is a tool that checks for errors in Python code, tries to enforce a coding standard and looks for code smells. It can also look for certain type errors, it can recommend suggestions about how particular blocks can be refactored and can offer you details about the code’s complexity. Contents 1 Pylint Documentation, Release 2.7.3 2 Contents CHAPTER 1 Introduction 1.1 What is Pylint? Pylint is a tool that checks for errors in Python code, tries to enforce a coding standard and looks for code smells. It can also look for certain type errors, it can recommend suggestions about how particular blocks can be refactored and can offer you details about the code’s complexity. Other similar projects would include pychecker (now defunct), pyflakes, flake8, and mypy. The default coding style used by Pylint is close to PEP 8. Pylint will display a number of messages as it analyzes the code and it can also be used for displaying some statistics about the number of warnings and errors found in different files. The messages are classified under various categories such as errors and warnings. Last but not least, the code is given an overall mark, based on the number and severity of the warnings and errors. 1.2 What Pylint is not? What Pylint says is not to be taken as gospel and Pylint isn’t smarter than you are: it may warn you about things that you have conscientiously done. Pylint tries hard to report as few false positives as possible for errors, but it may be too verbose with warnings. That’s for example because it tries to detect things that may be dangerous in a context, but are not in others, or because it checks for some things that you don’t care about. Generally, you shouldn’t expect Pylint to be totally quiet about your code, so don’t necessarily be alarmed if it gives you a hell lot of messages for your project! The best way to tackle pylint’s verboseness is to: • enable or disable the messages or message categories that you want to be activated or not for when pylint is analyzing your code. This can be done easily through a command line flag. For instance, disabling all convention messages is simple as a --disable=C option added to pylint command. • create a custom configuration file, tailored to your needs. You can generate one using pylint’s command --generate-rcfile. 3 Pylint Documentation, Release 2.7.3 4 Chapter 1. Introduction CHAPTER 2 Tutorial Author Robert Kirkpatrick 2.1 Intro Beginner to coding standards? Pylint can be your guide to reveal what’s really going on behind the scenes and help you to become a more aware programmer. Sharing code is a rewarding endeavor. Putting your code out there can be either an act of philanthropy, coming of age, or a basic extension of belief in open source. Whatever the motivation, your good intentions may not have the desired outcome if people find your code hard to use or understand. The Python community has formalized some recommended programming styles to help everyone write code in a common, agreed-upon style that makes the most sense for shared code. This style is captured in PEP 8, the "Style Guide for Python Code". Pylint can be a quick and easy way of seeing if your code has captured the essence of PEP 8 and is therefore friendly to other potential users. Perhaps you’re not ready to share your code but you’d like to learn a bit more about writing better code and don’t know where to start. Pylint can tell you where you may have run astray and point you in the direction to figure out what you have done and how to do better. This tutorial is all about approaching coding standards with little or no knowledge of in-depth programming or the code standards themselves. It’s the equivalent of skipping the manual and jumping right in. My command line prompt for these examples is: robertk01 Desktop$ 2.2 Getting Started Running Pylint with no arguments will invoke the help dialogue and give you an idea of the arguments available to you. Do that now, i.e.: 5 Pylint Documentation, Release 2.7.3 robertk01 Desktop$ pylint ... a bunch of stuff ... A couple of the options that we’ll focus on here are: Commands: --help-msg=<msg-id> --generate-rcfile Messages control: --disable=<msg-ids> Reports: --reports=<y_or_n> --output-format=<format> If you need more detail, you can also ask for an even longer help message, like so: robertk01 Desktop$ pylint --long-help ... Even more stuff ... Pay attention to the last bit of this longer help output. This gives you a hint of what Pylint is going to pick on: Output: Using the default text output, the message format is : MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE There are5 kind of message types : * (C) convention, for programming standard violation * (R) refactor, for bad code smell * (W) warning, for python specific problems * (E) error, for probable bugs in the code * (F) fatal, if an error occurred which prevented pylint from doing further processing. When Pylint is first run on a fresh piece of code, a common complaint is that it is too noisy. The current default configuration is set to enforce all possible warnings. We’ll use some of the options I noted above to make it suit your preferences a bit better (and thus make it emit messages only when needed). 2.3 Your First Pylint’ing We’ll use a basic Python script as fodder for our tutorial. The starting code we will use is called simplecaesar.py and is here in its entirety: 1 #!/usr/bin/env python3 2 3 import string; 4 5 shift=3 6 choice= input("would you like to encode or decode?") 7 word= input("Please enter text") 8 letters= string.ascii_letters+ string.punctuation+ string.digits 9 encoded='' 10 if choice =="encode": (continues on next page) 6 Chapter 2. Tutorial Pylint Documentation, Release 2.7.3 (continued from previous page) 11 for letter in word: 12 if letter =='': 13 encoded= encoded+'' 14 else: 15x= letters.index(letter)+ shift 16 encoded= encoded+ letters[x] 17 if choice =="decode": 18 for letter in word: 19 if letter =='': 20 encoded= encoded+'' 21 else: 22x= letters.index(letter)- shift 23 encoded= encoded+ letters[x] 24 25 print(encoded) Let’s get started. If we run this: robertk01 Desktop$ pylint simplecaesar.py ************* Module simplecaesar simplecaesar.py:3:0: W0301: Unnecessary semicolon (unnecessary-semicolon) simplecaesar.py:1:0: C0114: Missing module docstring (missing-module-docstring)