Test stub files against real code · Issue #5028 · python/mypy (original) (raw)

There are a few use cases when stubs may be compelling over type annotations or type comments:

However at the moment this has some limitations: stub files are used only to check clients of the stub, not the code itself.

a.py

def add_int(a, b): return a + b

add_int("a", "b") # mypy does not complain, actually ignores the entire file

a.pyi

def add_int(a:int, b:int) -> int: ...

client.py

from a import add_int add_int(1, 2) add_int(1, "2") # here mypy is going to complain

In case of the above files mypy will warn about bad usage of inside client.py, but not in a.py (that is, mypy does not validate against the source files being annotated by the stub). This has serious drawbacks:

Here I propose to implement a way to support testing stub files against the real code:

Merging the AST definitely is not trivial. The plan is to start with a POC implementation, that works for the most cases, and then see the viability of this approach. Note this would help a lot both stub maintainers I think (typeshed) and projects that need Python 2 support.