[Python] Why does mypy report different errors when run directly versus through pre-commit?

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm experiencing an inconsistent behavior with mypy type checking in my Python project. When I run mypy directly on a file, I get different results compared to running it through pre-commit hooks.

    What I've tried:


    Running mypy directly:

    $ mypy django_model_file.py
    django_model_file.py:5: error: "ForeignKey" expects 1 type argument, but 2 given [type-arg]


    Running through pre-commit:

    $ pre-commit run mypy --files idjango_model_file
    # Different errors or no errors at all


    I've confirmed that both are supposed to use the same config file:

    • My pre-commit hook specifies entry: mypy --config-file=mypy.ini
    • Direct mypy also finds mypy.ini (confirmed with --verbose flag)
    Environment details:

    • macOS 24.4.0
    • Python 3.12
    • mypy 1.15.0
    • django-stubs 5.1.3
    • pre-commit 4.2.0
    Key configuration:


    # .pre-commit-config.yaml
    - id: mypy
    name: mypy type checker
    entry: mypy --config-file=mypy.ini
    language: system # <-- This might be relevant
    types: [python]


    # mypy.ini
    [mypy]
    python_version = 3.12
    exclude = (?x)(
    venv|
    build|
    dist|
    (^|.*/)test_.*\.py$|
    (^|.*/)(tests?|__tests__)/.*\.py$)

    What could be causing this discrepancy?

    1. Is it because pre-commit uses language: system while I'm in a virtualenv?
    2. Could the exclude patterns be interpreted differently?
    3. Is there some caching mechanism causing stale results?
    4. Does pre-commit modify the environment or path in a way that affects mypy?

    I need to ensure consistent type checking between CI and local development. Any help identifying why these two approaches produce different results would be greatly appreciated!

    Continue reading...

Compartilhe esta Página