Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects

Enhancing Code Quality with Static Analysis Tools in Resume Enhancer

In this blog, I’ll share my experience of adding Static Analysis tools to my Resume Enhancer project. Static analysis tools work on source code itself, helping to maintain …


This content originally appeared on DEV Community and was authored by Harshil Patel

Enhancing Code Quality with Static Analysis Tools in Resume Enhancer

In this blog, I’ll share my experience of adding Static Analysis tools to my Resume Enhancer project. Static analysis tools work on source code itself, helping to maintain quality by automatically fixing formatting issues, spotting potential code flaws, and alerting us to common errors.

Source Code Formatter

For code formatting, I chose Black. Black has become my go-to formatter because it removes the need to worry about minor formatting decisions. By using Black, I give up some control over these small details, but in return, I get a fast, consistent, and reliable tool that keeps my code clean and eliminates pycodestyle warnings. This frees up time and mental energy for more important tasks.

To install Black, I used:

pip install black

After exploring the documentation, I ran Black manually on my main code file, resumeEnhancer.py, and found that most of my code was already formatted. Here’s what it looked like:

Image description

Next, I created a format.sh script to make it easy to run Black on all coding files in one step. Here’s the result of executing the script:

Image description

Linter

Linters help us catch common and subtle mistakes, which can be easy to overlook. To spot these issues, I used Flake8 to lint my source code. After installing Flake8 with pip install flake8, I ran it on my main code file and, to my surprise, discovered many errors:

Image description

I added Flake8 configurations in a .flake8 file and created a lint.sh script to streamline the linting process. After fixing all the errors that Flake8 identified, I realized how essential linting is—it catches tiny errors that could otherwise lead to larger issues later on.

Editor/IDE Integration

Using static analysis tools at build time is great for automating code quality checks, but integrating them directly into my editor made an even bigger impact. With Black and Flake8 integrated into my editor, I could catch and fix issues as I coded—no waiting until I was finished. A formatter that tidies code on save and a linter that flags issues in real-time helped keep my code clean and consistent.

To integrate Black and Flake8, I installed the necessary extensions in VSCode. Initially, I installed an extension called black instead of black formatter, which caused formatting issues. After troubleshooting, I discovered the correct extension, and everything worked smoothly.

Image description

Pre-Commit Hook

Adding a Git pre-commit hook was a powerful way to ensure my code remained consistent and clean before each commit. A pre-commit hook automatically runs the formatter on any files being committed, saving us from having to remember to do it manually.

Pre-commit hooks are especially useful when working with others because they apply formatting uniformly across all commits, making the project’s codebase more readable. Setting one up was straightforward. I used pre-commit to configure a hook that runs Black on any changed files before committing.

After configuring the pre-commit hook, I tested it, and it worked perfectly:

Image description

What I Learned

This lab taught me the value of automating code quality checks. Setting up tools like Black and Flake8 wasn’t just about following rules—it made my coding process smoother and more efficient. I realized how much time and effort is saved when formatting and linting happen automatically, allowing me to catch issues early on. This experience showed me the power of automation and consistent coding standards in collaborative projects.


This content originally appeared on DEV Community and was authored by Harshil Patel


Print Share Comment Cite Upload Translate Updates
APA

Harshil Patel | Sciencx (2024-11-01T18:42:22+00:00) Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects. Retrieved from https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/

MLA
" » Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects." Harshil Patel | Sciencx - Friday November 1, 2024, https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/
HARVARD
Harshil Patel | Sciencx Friday November 1, 2024 » Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects., viewed ,<https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/>
VANCOUVER
Harshil Patel | Sciencx - » Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/
CHICAGO
" » Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects." Harshil Patel | Sciencx - Accessed . https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/
IEEE
" » Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects." Harshil Patel | Sciencx [Online]. Available: https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/. [Accessed: ]
rf:citation
» Automating Code Quality: A Guide to Setting Up Formatters, Linters, and Pre-Commit Hooks in Python Projects | Harshil Patel | Sciencx | https://www.scien.cx/2024/11/01/automating-code-quality-a-guide-to-setting-up-formatters-linters-and-pre-commit-hooks-in-python-projects/ |

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.