diff --git a/docs/source/config_file.rst b/docs/source/config_file.rst index db05ba686df2..58ee62b24845 100644 --- a/docs/source/config_file.rst +++ b/docs/source/config_file.rst @@ -51,6 +51,10 @@ The following global flags may only be set in the global section alternative directory which is used to look for stubs instead of the default ``typeshed`` directory. +- ``mypy_path`` (string) specifies the paths to use, after trying the paths + from ``MYPYPATH`` environment variable. Useful if you'd like to keep stubs + in your repo, along with the config file. + - ``warn_incomplete_stub`` (Boolean, default False) warns for missing type annotation in typeshed. This is only relevant in combination with ``check_untyped_defs``. diff --git a/mypy/build.py b/mypy/build.py index 0932fb3279f4..cbc088fc2b38 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -153,6 +153,9 @@ def build(sources: List[BuildSource], # to the lib_path lib_path.insert(0, os.getcwd()) + # Prepend a config-defined mypy path. + lib_path[:0] = options.mypy_path + # Add MYPYPATH environment variable to front of library path, if defined. lib_path[:0] = mypy_path() diff --git a/mypy/main.py b/mypy/main.py index fa065277e484..19656e189e36 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -454,6 +454,7 @@ def get_init_file(dir: str) -> Optional[str]: 'strict_optional_whitelist': lambda s: s.split(), 'custom_typing_module': str, 'custom_typeshed_dir': str, + 'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)], 'junit_xml': str, } diff --git a/mypy/options.py b/mypy/options.py index cceb3d6e2516..c491a0d9f617 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -36,6 +36,7 @@ def __init__(self) -> None: self.platform = sys.platform self.custom_typing_module = None # type: Optional[str] self.custom_typeshed_dir = None # type: Optional[str] + self.mypy_path = [] # type: List[str] self.report_dirs = {} # type: Dict[str, str] self.silent_imports = False self.almost_silent = False diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 47c0691922ae..e4c8905112e9 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -296,3 +296,28 @@ def untyped_function(): + +[case testConfigMypyPath] +# cmd: mypy file.py +[file mypy.ini] +[[mypy] +mypy_path = + foo:bar + , baz +[file foo/foo.pyi] +def foo(x: int) -> str: ... +[file bar/bar.pyi] +def bar(x: str) -> list: ... +[file baz/baz.pyi] +def baz(x: list) -> dict: ... +[file file.py] +import no_stubs +from foo import foo +from bar import bar +from baz import baz +baz(bar(foo(42))) +baz(bar(foo('oof'))) +[out] +file.py:1: error: Cannot find module named 'no_stubs' +file.py:1: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help) +file.py:6: error: Argument 1 to "foo" has incompatible type "str"; expected "int"