✨Write clean Python code using pylint and black ✨🐍

First encounter 🔍

The very first time I encountered linters and style formatters for Python was during this year’s Hacktoberfest. My CI/CD runs kept failing for one particular pull request on GitHub due to errors on black, isort, gitlint. M…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Samina Rahman Purba

First encounter 🔍

The very first time I encountered linters and style formatters for Python was during this year's Hacktoberfest. My CI/CD runs kept failing for one particular pull request on GitHub due to errors on black, isort, gitlint. Multiple times. The code was working, but why were my CI/CD runs failing so many times? You can read all about the CI/CD nightmare in this blog. I was frustrated and questioned the need for such checks. Now, I have studied all about pylint to integrate it into my static site generator - rwar. I understand how important it is for code to go through such checks to help spot silly errors that programmers often make and keep the code bug free in the long run, especially when many programmers are working on one big project. Imagine it this way, while typing things on a word document we almost always use a spellchecker to catch typos. Similarly, linters help us catch those silly errors that might snowball into a bigger problem if not addressed on time. They put quality control checks in place. Plus, linters are customizable, allowing us to set rules for the checks we want for our code.

What is pylint

pylint is a tool that checks for errors in Python code, enforces a coding standard, and checks for bad code smell, giving us immediate feedback about our code. After analyzing the code it gives an overall score out of 10 along with errors in the file. Honestly, this was really fun for me to see all the possible ways I could make my code better. I, then, went through each of the warning messages to fix them all.

However, it is important to note that:

"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." (source: pylint docs)

The errors: Before fixing the score was 9.35/10

Image description

After fixing, the score was 10/10

Image description

To install pylint I had to first:

pip install pylint 

To run pylint on my project I typed in pylint ./rwar.py ./src/

The .pylintc file 📁

pylint requires a .pylintrc file for configuration. While I am no expert at writing out .pylintrc files, I did a little bit of googling to find good .pylintrc files that reside on the internet. Then I copied it and slightly modified it for my project.

What is black

black is a great tool for formatting your Python code automatically, while you give full attention to the logic of the code.

To use black I first had to install it by

pip install black 

After that you specify black filename.py in the terminal and black automatically formats your code. As shown below when I ran black on rwar.py file, it reformatted everything according to the PEP8 standard.

Image description

Integrating to VSCode

While black can be installed in many different IDEs, I personally love and use VSCode.

Open your VSCode settings, by going Code -> Preferences -> Settings.

Search for 'python formatting provider' and then select 'black' from the drop down menu:

Image description

After that search for 'format on save' and enable the "Editor: Format On Save" option

Image description

Now, everytime you save, the code will be automatically formatted.

The Contributing.md file 📝

The CONTRIBUTING.md file for my project contains information regarding the linters and formatters used for my project and ways to install them with useful links to its documentation. As I improve my static site generator and build it along the way, this file is also going to get bigger.

What's next...❓

I had fun experimenting with black and pylint. Next, I am going to install a pre-commit hook to my project. I tried to get it done this week but had some setbacks with lots of errors while attempting to install it. By investing a little bit more time in it, I believe I can get the pre-commit hook, up and running before my next weekly blog on this python project story series. The second thing, I am going to experiment with before the next blog would be adding a virtual environment for Python.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Samina Rahman Purba


Print Share Comment Cite Upload Translate Updates
APA

Samina Rahman Purba | Sciencx (2022-11-05T03:55:37+00:00) ✨Write clean Python code using pylint and black ✨🐍. Retrieved from https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/

MLA
" » ✨Write clean Python code using pylint and black ✨🐍." Samina Rahman Purba | Sciencx - Saturday November 5, 2022, https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/
HARVARD
Samina Rahman Purba | Sciencx Saturday November 5, 2022 » ✨Write clean Python code using pylint and black ✨🐍., viewed ,<https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/>
VANCOUVER
Samina Rahman Purba | Sciencx - » ✨Write clean Python code using pylint and black ✨🐍. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/
CHICAGO
" » ✨Write clean Python code using pylint and black ✨🐍." Samina Rahman Purba | Sciencx - Accessed . https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/
IEEE
" » ✨Write clean Python code using pylint and black ✨🐍." Samina Rahman Purba | Sciencx [Online]. Available: https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/. [Accessed: ]
rf:citation
» ✨Write clean Python code using pylint and black ✨🐍 | Samina Rahman Purba | Sciencx | https://www.scien.cx/2022/11/05/%e2%9c%a8write-clean-python-code-using-pylint-and-black-%e2%9c%a8%f0%9f%90%8d/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.