agritheory / test_utils

Common Test Utilities and Fixtures for AgriTheory Projects
4 stars 4 forks source link

pre-commit mypy not ignoring missing imports for some stubs #56

Closed HKuz closed 2 months ago

HKuz commented 3 months ago

I ran into an issue with pre-commit's mypy step locally with AutoReader when trying to commit changes, which apparently is a known problem. Fortunately, there were several solutions listed in the issue comment thread. In general, seems that when mypy is run in pre-commit, it doesn't always "find" installed type stubs (even if they're in the local environment) and doesn't honor the --ignore-missing-imports for some stubs. Solutions noted below - they either need to be explicitly included in the pre-commit environment (via an additional-dependencies key), or installed similar to the CI workflow (via the args key including --install-types and --non-interactive flags).

Error Details

I specifically had trouble with the types-requests stub - this is listed as an AutoReader app dependency and is installed in my local environment (which was activated when the error occurred). It's also worth noting that when I ran mypy ./ directly, I didn't get errors, and the pre-commit-config.yaml already had the --ignore-missing-imports flag.

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

autoreader/autoreader/doctype/autoreader_mapping/autoreader_mapping.py:8: error: Library stubs not installed for "requests"  [import]
autoreader/autoreader/doctype/autoreader_mapping/autoreader_mapping.py:8: note: Hint: "python3 -m pip install types-requests"
autoreader/autoreader/doctype/autoreader_mapping/autoreader_mapping.py:8: note: (or run "mypy --install-types" to install all missing stub packages)
autoreader/autoreader/doctype/autoreader_mapping/autoreader_mapping.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 4 source files)

Solutions

That issue thread suggested a few solutions, the following two both worked for AutoReader - if we're defining the config in test_utils, solution 2 is the more flexible option.

1) Explicitly add the troublesome dependency in .pre-commit-config.yaml for the pre-commit env:

# .pre-commit-config.yaml
- repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.5.1
    hooks:
      - id: mypy
        exclude: ^tests/
        args: ["--ignore-missing-imports"]
        additional_dependencies: ['types-requests']

2) Have pre-commit install necessary packages:

# .pre-commit-config.yaml
- repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.5.1
    hooks:
      - id: mypy
        exclude: ^tests/
        args: ["--install-types", "--non-interactive", "--ignore-missing-imports"]