Skip to main content

A GitHub template with my python package configurations.

Project description

🐍 python-package-template

build status pre-commit.ci status docs status license

Overview

A GitHub template with my python package configurations.

To make sure that the all the tools are available in your virtual environment (and that you are running your code with its latest modifications), install the package in editable mode by running:

pip install --editable ".[dev]"

Alternatively, you can Dev Containers: Reopen in Container in Visual Studio Code if Docker is installed.

[!WARNING] This template represents my personal understanding of the current best practices.
It is advised to do further research before implementing these configurations in your environment.

Feel free to open a new issue if you have any questions or suggestions.

Package tools

This template package relies on the synchronized cooperation of several exceptional tools.
These tools include:

  • Codecov - Code coverage
  • Pre-Commit - Git hooks running on commits
  • MyPy - Static type checking
  • Pytest - Testing and code coverage
  • Ruff - Formatting and linting
  • Tox - Orchestration of the above tools

For documentation:

Codecov

Codecov is used to check the code coverage of the tests.
It also provides a badge that can be added to the README file.

Codecov is set up to be part of the tox reusable workflow, but for this action it is important to generate the coverage report using the --cov-report=xml flag in the pyproject.toml file.

Pre-Commit

Pre-Commit is used to run certain checks on the code before it is committed.
These checks are defined in the .pre-commit-config.yaml file.
To use pre-commit is has to be installed in the virtual environment and also added to the git hooks by running pre-commit install.

In this repository pre-commit is set up for a number of general issues and to run formatting and linting checks with ruff.

Call pre-commit by running:

pre-commit run --all-files

MyPy

Python by default is a dynamically typed language, but being explicit about types can help to avoid bugs.
MyPy makes sure that the types are correct and consistent throughout the code.

The mypy related settings are defined in the pyproject.toml file.
In this repository MyPy is set up be strict and it also checks for some additional issues.

Call mypy by running:

mypy src tests

Pytest

Pytest is a modern testing framework for python.
It is way too complex to explain it here, but it runs all the tests from the tests directory and also checks the code coverage.

Its settings are defined in the pyproject.toml file.

Call pytest by running:

pytest

Ruff

Ruff is a formatter and linter that is built on top of a lot of open source tools.
It is very fast and unifies all the useful code quality solutions into a single tool.
By default it is not too strict, but I like to make it strict by selecting all the available rules. The exact configuration is defined in the ruff.toml file.

If for some reason it makes sense not to comply with a certain rule, it can be disabled for that line using # noqa: <rule number>.

Call ruff by running:

ruff check src tests

Tox

Tox is useful for running the above tools in an isolated environment.
It makes sure that the package setup is consistent and that the tools are working as expected.
It can be used to test different python versions and different testing scenarios.

In this repository tox is set up to use python 3.11, 3.12 and run pytest, ruff, mypy and documentation tests. The settings are specified in the tox.ini file.

Call tox by running:

tox

Documentation

The documentation is built with Sphinx and it is hosted both on ReadTheDocs and GitHub Pages.
Both of these services are recommended, however ReadTheDocs requires a bit more setup, but I prefer it as it does not require an extra feature branch to be present.

GitHub repository settings

The following settings are enabled in my repository settings:

Code/About:

  • Releases

General/Features:

  • Issues
  • Preserve this repository

General/Pull Requests:

  • Allow squash merging
  • Always suggest updating pull request branches
  • Automatically delete head branches

Rules/Rulesets: main
Target branches: Default

  • Restrict deletions
  • Require pull request before merging
    • Dismiss stale pull request approvals when new commits are pushed
    • Require review from Code Owners
    • Allowed merge methods: Squash
  • Require status checks to pass before merging
    • Require branches to be up to date before merging
    • pre-commit.ci - pr
    • tox / tox (3.11)
    • tox / tox (3.12)
  • Block force pushes

Environments:
pypi

  • Deployment protection rules:
  • Required reviewers: daniel-mizsak
  • Allow administrators to bypass configured protection rules

Pages/Build and deployment:

  • Source: Deploy from branch
  • Branch: gh-pages (root)

Setup PyPi trusted publishing

PyPI publishing settings

Add a new pending publisher:

  • PyPI Project Name: python-package-template-pypi (has to match the project name in pyproject.toml)
  • Owner: daniel-mizsak
  • Repository name: python-package-template
  • Workflow name: release.yml

(I am currently not using trusted publishing, as it does not support getting called from a reusable GitHub workflow.
Instead, I am calling my PyPI publishing workflow with an API token.)

More examples

I am trying to use this template in all of my repositories and also contribute back here with new best practices I find. Some of my other repositories that may be interesting to look at:

I have also integrated some of the above mentioned tools into my vscode settings. You can find them in my macos-setup repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_package_template_pypi-0.2.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_package_template_pypi-0.2.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file python_package_template_pypi-0.2.1.tar.gz.

File metadata

File hashes

Hashes for python_package_template_pypi-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2c8998aafc51800d63541b9f502c1205302d1df92b3828b0f2b4fc9473ee854b
MD5 e5d589c78c22ae8c630422b4eaaa116b
BLAKE2b-256 e0535e08ee634de56307637cb15c083a9961818351124fc40c7d5ab87c0c14b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_package_template_pypi-0.2.1.tar.gz:

Publisher: release.yml on daniel-mizsak/python-package-template

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_package_template_pypi-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_package_template_pypi-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dffa81abef73971c507a7e1e573445f329ab730d06cc5cf0c15e7fd259a4506a
MD5 712c5c77cb58d47df7dd442c499a835e
BLAKE2b-256 27da597c03d560d34f5e4b0eeaf7efdd8c7af1e9a20fc4cf148c7ae808a8d17b

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_package_template_pypi-0.2.1-py3-none-any.whl:

Publisher: release.yml on daniel-mizsak/python-package-template

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page