Search code examples
condamypytypingpre-commitpre-commit.com

Conda and pre-commit, error: Library stubs not installed for "requests" (or incompatible with Python 3.10)


Error message

While using a conda environment and pre-commit I receive the error:

error: Library stubs not installed for "requests" (or incompatible with Python 3.10)

The full error message is:

rc/export_data/plantuml_get_package.py:69: error: Library stubs not installed for "requests" (or incompatible with Python 3.10)
src/export_data/plantuml_get_package.py:69: note: Hint: "python3 -m pip install types-requests"
src/export_data/plantuml_get_package.py:69: note: (or run "mypy --install-types" to install all missing stub packages)
src/export_data/plantuml_get_package.py:69: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 24 source files)

The default solution is given in the error message:

mypy --install-types

However, the requirement is already satisfied:

Installing missing stub packages:
/usr/bin/python3 -m pip install types-requests

Install? [yN] y

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: types-requests in /usr/lib/python3/dist-packages (2.25)

Conda Yaml

This would suggest it may be installed on the device, but not in the conda environment, however, it should be, because the following conda environment is created successfully (last 2 lines):

# run: conda env create --file environment.yml
# include new packages: conda env update --file environment.yml
name: someenvironment
channels:
  - conda-forge
  - conda
dependencies:
# Specify specific python version.
- python=3.10
- anaconda
- nb_conda
- conda:
# Run python tests.
  - pytest=6.1.2
# Convert notebooks to pdf.
  - nbconvert
  - matplotlib
# Support notebooks.
  - nb_conda
# Visualise graphs for PlantUML
  - graphviz
# Run graph software quickly
  - networkx
- pip
- pip:
# Compile pdf from python.
  - pdflatex
# Write unit tests on Jupyter notebooks.
  - testbook
# Auto generate docstrings
  - pyment
# Auto generate documentation.
  - pdoc3
# Code formatting compliance
  - black
# Generate diagrams.
  - plantuml
# Identify and remove dead code.
  - vulture
# Get PlantUML .jar file.
  - requests
  - types-requests

Question

So I was wondering, how can I ensure the mypy recognizes the types-requests package in the environment?


Solution

  • A solution was found in this discussion Changing the mypy entry in the .pre-commit-config.yaml to:

    # Test if the variable typing is correct. (Variable typing is when you say:
    # def is_larger(nr: int) -> bool: instead of def is_larger(nr). It makes
    # it explicit what type of input and output a function has.
     - repo: https://github.com/pre-commit/mirrors-mypy
       rev: v0.950
       hooks:
        - id: mypy
          verbose: true
          args: [--show-error-codes]
          additional_dependencies: ['types-requests']
    

    Which yielded output:

    mypy.....................................................................Passed
    - hook id: mypy
    - duration: 1.3s
    
    Success: no issues found in 24 source files