Как игнорировать `ModuleNotFoundError` при использовании mypy для действий на github?

Я интегрирую mypy в рабочий процесс GitHub Actions, чтобы проверять только измененные файлы в моем проекте django:

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

Однако я столкнулся с ModuleNotFoundError, что требует установки всех зависимостей с pip install -r requirements.txt. Этот процесс значительно замедляет работу. Есть ли способ пропустить эти ошибки и получить полезную информацию о проверке типов без установки полного набора зависимостей?

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

Вы можете перечислить все модули, которые могут понадобиться вам как для разработки, так и для тестирования, в общем файле common.txt, а дополнительные зависимости перечислить в другом файле dev.txt для разработки и test.txt для тестирования.

dev.txt:-

-r common.txt

#your extra dependencies related to development.

test.txt:-

-r common.txt

#your extra dependencies related to testing.
Вернуться на верх