Description
(moved from python/typeshed#52)
Currently mypy (AFAICT) looks for type information in the current directory/location of file being checked, directories on MYPYPATH, and typeshed, in that order, and takes the first file it finds. This will become problematic as more libraries adopt inline type annotations: the user will need to maintain a MYPYPATH that includes all such libraries. The easiest way to do that is to pip install -r requirements.txt
and use that environment's site-packages
as mypy's path, but this will include packages that do not have type annotations and these source files may mask stubs for those libraries in typeshed.
I propose that rather than stopping the search at the first file that exists, mypy examine source files to see whether they have usable type annotations, and if not, continue the search. If the search concludes without any usable type information but it did find some source files, then the first one can be used (to at least get the list of top-level function names, etc)
Completely:
- Construct a search path, mimicking python's sys.path initialization: an initial element based on the command line (the current directory in
-m
mode, the directory containing the given file if a file is given),$MYPYPATH
,$PYTHONPATH
, typeshed. I think it would be convenient if there were a shortcut to add thesite-packages
of a given virtualenv (perhaps defaulting to the environment in which mypy is running); this would come in between$PYTHONPATH
and typeshed. - For each directory in this search path:
2a. Look for a.pyi
stub file. If one is found, we're done.
2b. If no.pyi
file, look for a.py
source file. If it can be parsed and contains type information, we're done. If it can be parsed but does not contain types, and it's the first one we've seen, remember it. - If no type information was found but we remembered a file in 2b, use whatever we can squeeze out of it.