使用 mypy 进行 github 操作时如何忽略`ModuleNotFoundError`?

问题描述 投票:0回答:1

我正在将

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
python django github-actions mypy cicd
1个回答
0
投票

您可以在一个通用文件中列出开发和测试所需的所有模块

common.txt
,同时在另一个文件中列出额外的依赖项
dev.txt
用于开发和
test.txt
用于测试。

dev.txt:-

-r common.txt

#your extra dependencies related to development.

测试.txt:-

-r common.txt

#your extra dependencies related to testing.
© www.soinside.com 2019 - 2024. All rights reserved.