Mypy Type Checker GitHub Workflow

Mypy Type Checker GitHub Workflow

Oct 01, 2023

Mypy is an optional static type checker for Python that aims to combine the benefits of dynamic (or "duck") typing and static typing. Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking. Mypy type checks standard Python programs; run them using any Python VM with basically no runtime overhead. (mypy-lang.org)

When I had my moment of pure genius (yeah right) and decided I wanted to create a repository with all (not yet complete) of the examples from the Hypermedia Systems book, I said to myself, "why stop there?" Heck, I could use this as an opportunity to get some reps at static typing in Python and as an excuse to really kick the tires on mypy!

Initially I would run mypy locally, but that got old quick and already had an idea brewing on how to get this going in a GitHub Workflow on every push. The resulting workflow is a bit long for its purpose, due to every single example being its own project, a monorepo if you will. This can be easily refactored (less than 30 lines) for a project that has, for example, its Python source files at the root of the repo.

name: Type Checker

on:
  push:

jobs:
  type_checker:
    name: Type Checker
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Set Up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install Dependencies
        run: |
          pip install -r requirements.txt
          python3 -m pip install mypy 
      - name: Type Checker
        run: mypy .

Another idea would be to run mypy as a pre-commit hook. Hmm, gonna have to try that next.

Thank you so much for reading, and please become a member to follow along on some of my indie hacking projects!!

Vous aimez cette publication ?

Achetez un café à LispDev

More from LispDev