Skip to content

Python

Code Style

Type Checking

  • Static type checking is performed using mypy
  • To fully leverage this, type hints should be added to all code (this is enforced by mypy)
  • This also helps the IDE in providing appropriate help when writing code
  • A lot of Python packages support type hints and provide mypy types declarations (e.g., there is a types-requests package for requests, mypy will warn about this when run)

Unit Testing

Unit tests are executed using pytest and coverage is checked using coverage.

Additionally, mocking can be done using the additional pytest plugin pytest-mock.

For complex migrations in Django that require Python code to run, there is the plugin django-test-migrations which allows to accomplish this.

(Upgrading) Dependencies

If not already done by an automatic dependency checker (see above), dependencies can be upgraded via pip-upgrader.

Django Migrations

When writing migrations in Django that require custom Python code to be executed (using the RunPython migration), forward and backward code needs to be provided to ensure that the database can be migrated in both directions without encountering any errors.

TBD

Have a checker check for this (pylint has a plugin)