You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the stubs are only searched automatically from the directory stubs below the directory containing (the translated) mypy.py. It would be useful to also look for stubs in a directory relative to the program being type checked/run.
For example, consider the following hierarchy:
d/foo.py
d/stubs/mylib.py
Now if d/foo.py does import mylib, it should refer to the mylib.py file in the stubs directory under d. stubs is a magic directory name for stubs.
You can specify an additional module path (for looking up stubs or any modules) by using the MYPYPATH environment variable. The implicit 'stubs' directory would just be a convenience.
This might be a stupid question, but I have tried the both methods to use a local stub: putting the pyi file in the same directory as the py file to run and exporting MYPMPATH to a stub directory and put the pyi file there. I named both pyi file the same name as the py file except for the extension. However, both situations does not throw an error when I run a test program with the incorrect typing hint.
Is there some new way to use the new local stub or I was just stupid and misunderstand the instruction?
When MYPYPATH is unset and I execute mypy stubtyping/ I have a blank output instead of the expected:
main.py:6: error: Argument 1 to "add" has incompatible type "str"; expected "int"
main.py:6: error: Argument 2 to "add" has incompatible type "str"; expected "int"
I also tried to setMYPYPATH to stubtyping/ but I still get the same results.
Next time please use the Gitter channel (see project README).
The answer is that mypy ignores main.py because it finds main.pyi in the same directory. This is by design. If you want type annotations, put then in main.py and get rid of main.pyi.
Activity
ashleyh commentedon Dec 13, 2012
I wonder if it would be better to be able to specify additional stubs dirs as a command line argument or something...
JukkaL commentedon Dec 13, 2012
You can specify an additional module path (for looking up stubs or any modules) by using the MYPYPATH environment variable. The implicit 'stubs' directory would just be a convenience.
JukkaL commentedon Dec 8, 2014
As we now have an alternative extension for stubs, this is no longer as urgent: #345
JukkaL commentedon May 17, 2015
The .pyi extension seems to address this issue, so closing this now.
DionysusG commentedon May 31, 2016
Hi,
This might be a stupid question, but I have tried the both methods to use a local stub: putting the pyi file in the same directory as the py file to run and exporting MYPMPATH to a stub directory and put the pyi file there. I named both pyi file the same name as the py file except for the extension. However, both situations does not throw an error when I run a test program with the incorrect typing hint.
Is there some new way to use the new local stub or I was just stupid and misunderstand the instruction?
gvanrossum commentedon May 31, 2016
Kamforka commentedon Jul 12, 2019
So as a follow up I also can't seem to sort out the problems with local stub files.
I have the following project structure:
main.pyhas the following content:and
main.pyihas:When
MYPYPATHis unset and I executemypy stubtyping/I have a blank output instead of the expected:I also tried to set
MYPYPATHtostubtyping/but I still get the same results.What am I missing?
gvanrossum commentedon Jul 12, 2019
Next time please use the Gitter channel (see project README).
The answer is that mypy ignores
main.pybecause it findsmain.pyiin the same directory. This is by design. If you want type annotations, put then inmain.pyand get rid ofmain.pyi.