Improving Code Quality in Python Codebases

Photo by Hitesh Choudhary on Unsplash

Code formatting

Black makes code review faster by producing the smallest diffs possible (Black).

Code linting

Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions (Pylint).

Type checking

# The possible types for this structure are:
# Dict[str, Dict[str, Any]] <- more specific type
# Dict[Any, Dict[Any, Any]] <- more general type
max_values = {
"companies": { "group_id": "", "company_count": 0 },
"employees": { "group_id": "", "employee_count": 0 }
# Given the max_values nested dictionary,
# you may want to compare the counts before updating the max values,
# but the type checker will complain because you're trying to
# compare an int (comp_count and emp_count) with an
# object of the Any type (the counts in the dictionary.
company_count > max_values["companies"]["company_count"]
employee_count > max_values["employees"]["employee_count"]
# Since we, the programmer, know better, we can give the type
# checker some help.
# from typing import cast
comp_count > cast(int, max_values["companies"]["company_count"])
emp_count > cast(int, max_values["employees"]["employee_count"])
no_implicit_optional = True
check_untyped_defs = True
ignore_missing_imports = True
mypy_type_error('but the programmer knows better') # type: ignore


… makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries (Pytest).

Managing multi-version Python environments

If you learned something new or enjoyed reading this article, please clap it up 👏 and share it so that others will see it. Feel free to leave a comment too.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store