MyPy Cannot Find Library Stubs for Specific Python Version

Viewed 1288

I'm following the tutorial on type checking with mypy here.

When I run a nox session to run mypy against the code, the Python 3.7 check complains about mypy not being able to find stubs for some libraries (e.g. click) whereas the checks for Python 3.8 finish successfully. And until I ignored requests, that was part of errors in 3.7 too. Why does this happen?

(my-hypermodern-python-VDazB31k-py3.8) ➜ nox -rs mypy
nox > Running session mypy-3.8
nox > Re-using existing virtual environment at .nox/mypy-3-8.
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/tmp/tmpyiz1np1u
nox > python -m pip install --constraint=/tmp/tmpyiz1np1u mypy
nox > mypy src tests noxfile.py
Success: no issues found in 8 source files
nox > Session mypy-3.8 was successful.
nox > Running session mypy-3.7
nox > Re-using existing virtual environment at .nox/mypy-3-7.
nox > poetry export --dev --format=requirements.txt --without-hashes --output=/tmp/tmp7_a7k3nt
nox > python -m pip install --constraint=/tmp/tmp7_a7k3nt mypy
nox > mypy src tests noxfile.py
src/my_hypermodern_python/wikipedia.py:1: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/console.py:3: error: Cannot find implementation or library stub for module named "click"
src/my_hypermodern_python/console.py:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
tests/test_console.py:1: error: Cannot find implementation or library stub for module named "click.testing"
tests/test_console.py:1: error: Cannot find implementation or library stub for module named "click"
Found 4 errors in 3 files (checked 8 source files)
nox > Command mypy src tests noxfile.py failed with exit code 1
nox > Session mypy-3.7 failed.
nox > Ran multiple sessions:
nox > * mypy-3.8: success
nox > * mypy-3.7: failed
WSL2 - Ubuntu 20.04
Python 3.7 and 3.8 versions from pyenv
nox 2020.12.31
mypy 0.910
click 7.1.2
# noxfile.py

def install_with_constraints(session, *args, **kwargs):
    with tempfile.NamedTemporaryFile() as requirements:
        session.run(
            "poetry",
            "export",
            "--dev",
            "--format=requirements.txt",
            "--without-hashes",
            f"--output={requirements.name}",
            external=True,
        )
        session.install(f"--constraint={requirements.name}", *args, **kwargs)

# Nox session for mypy
@nox.session(python=["3.8", "3.7"])
def mypy(session):
    args = session.posargs or locations
    install_with_constraints(session, "mypy")
    session.run("mypy", *args)
# mypy.ini

[mypy]

[mypy-nox.*,pytest,requests]
ignore_missing_imports = True
0 Answers
Related