DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Carlos Villavicencio
Carlos Villavicencio

Posted on

GitHub Action to run mypy on changed files only

Recently, I needed a way to perform Python static type checking using mypy only on "changed" files for a GitHub Pull Request.

So I googled a bit and found out this super useful action that you can use it in your workflow right away.

GitHub logo tj-actions / changed-files

:octocat: Github action to retrieve all (added, copied, modified, deleted, renamed, type changed, unmerged, unknown) files and directories.

Codacy Badge CI Update release version. Public workflows that use this action.

Ubuntu Mac OS Windows

All Contributors

changed-files

Retrieve all changed files and directories relative to the target branch (pull_request* based events) or the last remote commit (push based event) returning the absolute path to all changed files and directories from the project root.

Features

  • Fast execution (0-2 seconds on average).
  • Easy to debug.
  • Boolean output indicating that certain files have been changed.
  • Scales to large repositories.
  • Git submodules support.
  • No extra API calls.
  • Monorepos (Fetches only the last remote commit).
  • Supports all platforms (Linux, MacOS, Windows).
  • GitHub-hosted runners support
  • GitHub Enterprise Server support.
  • self-hosted runners support.
  • List all files and directories that have changed
    • Between the current pull request branch and the last commit on the target branch.
    • Between the current pull request branch and the fork point on the target branch.
    • Between the last commit and the current pushed change.
    • Between the last remote branch commit and the current HEAD.
  • Restrict change detection…

And after customizing some options like filter only *.py files, I was able to run mypy passing the changed files as argument.

It's worth to mention that I needed to include --ignore-missing-imports argument to mypy because I'm not scanning the whole repo, only some files. The goal of course is to check them all. However, a lot of fixes should be made before it's ready.

This solution can be very useful to you if you want to include static type checking for new files on a legacy project where existing files will be fixed little by little.

name: "mypy check"
on: [pull_request]

jobs:

  static-type-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v3
      with:
        python-version: '3.x'
    - run: pip install mypy  # you can pin your preferred version
    - name: Get Python changed files
      id: changed-py-files
      uses: tj-actions/changed-files@v23
      with:
        files: |
          *.py
          **/*.py
    - name: Run if any of the listed files above is changed
      if: steps.changed-py-files.outputs.any_changed == 'true'
      run: mypy ${{ steps.changed-py-files.outputs.all_changed_files }} --ignore-missing-imports

Enter fullscreen mode Exit fullscreen mode

Happy type-safety!

Oldest comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.