Lets say I have a simple function inside the file home/func.py, and I make one call to it.
def f(x):
return x
print(f("example"))
Now lets say I want to type check this function using a stub, home/stubs/func.pyi which looks like:
def f(x: int): ...
Now I want to check my code with mypy to see if I've made any typing mistakes. Running mypy on home/func.py I would expect an warning/error like: Got string where int was expected - however I can't get this functionality to work.
What I've read online and in the docs suggests I should create a home/mypy.ini config file with the following contents:
[mypy]
mypy_path = $MYPY_CONFIG_FILE_DIR/stubs
and then I should run mypy while pointing to this config, so something like: python -m mypy --config mypy.ini func.py. This has not worked.
I know I could use typing annotations in the function definition but in this case the use of stubs is non-optional. Any help is really really appreciated.
To this day (Python 3.10 and MyPy 0.982), as you, I haven't been able to separate stubs from code and make it work in my projects.
For now I either put the annotations in the code or use the alternatives (such as pytype, pyright, Pyre).
I hope someone else to correct me, or maybe understand how is it done in others projects (such as Panda, or Django).
A few hints to understand
Quoting Pragy Agarwal
Which, in turn, reference the yet unclosed issue "Test stub files against real code".
Out of context quote from the BDFL:
Related question: How to point to custom stubs directory in mypy via MYPY environment variable or mypy_path variable