Mypy 错误在本地环境中无法重现

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

我的 python 代码有一个 github 操作和预提交挂钩

下面是我的 git 操作的 yml 文件

name: Mypy

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.11"]
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        pip install "mypy==1.7.0" "pydantic>=2.4" "alembic>=1.8.1" "types-aiofiles>=23.2.0" "types-redis>=4.6.0" --quiet
    - name: Running mypy checks
      run: |
        mypy . --ignore-missing-imports --config-file backend/app/mypy.ini

这是我的预提交配置

repos:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.7.0
    hooks:
    -   id: mypy
        args: [--ignore-missing-imports, --config-file, backend/app/mypy.ini]
        verbose: true
        additional_dependencies:
        - "pydantic>=2.4"
        - "alembic>=1.8.1"
        - "types-aiofiles>=23.2.0"
        - "types-redis>=4.6.0"

我正在检查的代码看起来像这样

    async def total_monthly_size(self, user_id: int) -> int:
        now = utcnow()
        current_month = datetime(now.year, now.month, 1)
        sum_total_size_query = select(func.sum(self.model.total_size or self.model.estimated_total_size)).where(
            self.model.user_id == user_id,
            self.model.is_failed.is_(False),
            self.model.requested_at > current_month,
        )

        sum_total_size_result = await self._db.execute(sum_total_size_query)
        sum_total_size = sum_total_size_result.scalar()
        return int(sum_total_size or 0)

预提交运行没有错误,但 git 操作失败并显示

error: Need type annotation for "sum_total_size_query"  [var-annotated]

如何实现这两种工具的行为一致?

python github-actions mypy pre-commit pre-commit.com
1个回答
0
投票

你不是在进行同类比较

预提交的工作原理是将文件名作为位置参数传递给钩子

因此,您不想与

mypy . ...
进行比较,而是想与
mypy ... $(git ls-files -- '*.py')

进行比较

免责声明:我写了预提交

© www.soinside.com 2019 - 2024. All rights reserved.