How to ignore `ModuleNotFoundError` when using mypy for github actions?

I’m integrating mypy into my GitHub Actions workflow to check only the changed files in my django project:

name: Pull Request Backend Lint and Format

on: [push, pull_request]

jobs:
  backend-lint-and-check:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: backend
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install ruff mypy mypy-extensions django-stubs django-stubs-ext

    - name: Get changed files
      id: changed-files
      run: |
        if [ "${{ github.event_name }}" == "pull_request" ]; then
          BASE_SHA=${{ github.event.pull_request.base.sha }}
        else
          BASE_SHA=$(git rev-parse HEAD^)
        fi
        CHANGED_FILES=$(git diff --name-only --diff-filter=d $BASE_SHA...HEAD | grep '^backend/' | sed 's|^backend/||' | grep '.py$' | tr 'n' ' ')
        echo "files<<EOF" >> $GITHUB_OUTPUT
        echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
        echo "EOF" >> $GITHUB_OUTPUT

    - name: Run Ruff check
      if: steps.changed-files.outputs.files
      run: |
        FILES="${{ steps.changed-files.outputs.files }}"
        if [ ! -z "$FILES" ]; then
          echo "Running Ruff check on files:"
          echo "$FILES"
          for file in $FILES; do
            ruff check "$file" || echo "Ruff check found issues in $file"
          done
        else
          echo "No Python files to check"
        fi

    - name: Run Ruff format check
      if: steps.changed-files.outputs.files
      run: |
        FILES="${{ steps.changed-files.outputs.files }}"
        if [ ! -z "$FILES" ]; then
          echo "Running Ruff format check on files:"
          echo "$FILES"
          for file in $FILES; do
            ruff format --check -v "$file" || echo "Ruff format check found issues in $file"
          done
        else
          echo "No Python files to check"
        fi

    - name: Set up environment
      run: |
        if [ -f credentials/secrets.sh ]; then
          source credentials/secrets.sh
        fi
        if [ -f deployment/config-dev.sh ]; then
          source deployment/config-dev.sh
        fi

    - name: Run Mypy
      if: steps.changed-files.outputs.files
      run: |
        FILES="${{ steps.changed-files.outputs.files }}"
        if [ ! -z "$FILES" ]; then
          echo "Running Mypy on files:"
          echo "$FILES"
          for file in $FILES; do
            mypy --config-file setup.cfg --follow-imports=skip "$file" || echo "Mypy found issues in $file"
          done
        else
          echo "No Python files to check"
        fi

However, I’m encountering ModuleNotFoundError which would require installing all dependencies with
pip install -r requirements.txt. This process significantly slows down the workflow. Is there a way to skip these errors and still get useful feedback on type checks without installing the full set of dependencies?

Running Mypy on files:
gh-actions-test.py 
Traceback (most recent call last):
Error constructing plugin instance of NewSemanalDjangoPlugin
  File "/opt/hostedtoolcache/Python/3.12.5/x64/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 103, in main
  File "mypy/main.py", line 187, in run_build
  File "mypy/build.py", line 193, in build
  File "mypy/build.py", line 238, in _build
  File "mypy/build.py", line 503, in load_plugins
  File "mypy/build.py", line 484, in load_plugins_from_config
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/mypy_django_plugin/main.py", line 71, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/mypy_django_plugin/django/context.py", line 127, in __init__
    apps, settings = initialize_django(self.django_settings_module)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/mypy_django_plugin/django/context.py", line 81, in initialize_django
    settings._setup()  # type: ignore[misc]
    ^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/django/conf/__init__.py", line 68, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/site-packages/django/conf/__init__.py", line 166, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.12.5/x64/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/runner/work/myproject/myproject/backend/myproject/settings.py", line 13, in <module>
    import openai
ModuleNotFoundError: No module named 'openai'
Mypy found issues in gh-actions-test.py

2

You can list all the module which you may need for both for development and testing purpose in a common file common.txt while listing extra dependencies in another file dev.txt for development and test.txt for testing.

dev.txt:-

-r common.txt

#your extra dependencies related to development.

test.txt:-

-r common.txt

#your extra dependencies related to testing.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật