Mypy in Nox session not finding Nump type stubs

573 Views Asked by At

My nox session is defined like:

@nox.session(python=["3.10", "3.9.10"])
def mypy(session: Session) -> None:
    args = session.posargs or locations
    install_with_constraints(session, "mypy")
    session.run("mypy", *args)

def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
    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)

And I have a module where I import numpy the standard way:

import numpy as np

...some code...

PyCharm doesn't have a problem with this import, while for example if I import torch, it warns me that there are no typestubs. Yet, when I run nox, I get:

error: Cannot find implementation or library stub for module named "numpy"

How come Mypy can't find the Numpy stubs when inside running inside the nox session?

1

There are 1 best solutions below

0
On

Numpy introduces type stubs in version 1.20. If you use a numpy version lower than 1.20, install [numpy-stubs]. If you use numpy 1.20 or higher, this issue should not arise.