Skip to content

Search procedure for inline annotations #1190

Closed
@bdarnell

Description

@bdarnell

(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:

  1. 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 the site-packages of a given virtualenv (perhaps defaulting to the environment in which mypy is running); this would come in between $PYTHONPATH and typeshed.
  2. 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.
  3. If no type information was found but we remembered a file in 2b, use whatever we can squeeze out of it.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions