From c7ed4b291e0bd53bafbde857b9c827f30cf42245 Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Tue, 25 Oct 2016 22:08:45 +0200 Subject: [PATCH 1/7] Allow to specify mypy_path in config (#2323) --- mypy/build.py | 3 +++ mypy/main.py | 1 + mypy/options.py | 1 + typeshed | 2 +- 4 files changed, 6 insertions(+), 1 deletion(-) 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..f35574a0e559 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 s.split(',')], '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/typeshed b/typeshed index fed966cf7f5b..f6bbc46c35b4 160000 --- a/typeshed +++ b/typeshed @@ -1 +1 @@ -Subproject commit fed966cf7f5b3cddb731b846f7781b863b47a7cc +Subproject commit f6bbc46c35b4c54cc6dd3268062df447ac81e718 From c7e298d9357ca2895a028f50012b38c89a56007b Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Tue, 25 Oct 2016 22:24:02 +0200 Subject: [PATCH 2/7] Docs on the new stubs_dir config option --- docs/source/config_file.rst | 4 ++++ 1 file changed, 4 insertions(+) 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``. From 5162c0ff5d871328b9211d4e29894c595c8cbac9 Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 28 Oct 2016 18:56:12 +0200 Subject: [PATCH 3/7] Accept either a colon or a comma in config.mypy_path --- mypy/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index f35574a0e559..19656e189e36 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -454,7 +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 s.split(',')], + 'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)], 'junit_xml': str, } From 61b5fcde6281490f017974864a4aef03b336264c Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 28 Oct 2016 18:59:05 +0200 Subject: [PATCH 4/7] Add a test for mypy_path config setting --- test-data/unit/cmdline.test | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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" From 12ca13702a9787bccc98ff3867c254f5dbf3f40a Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 28 Oct 2016 19:08:15 +0200 Subject: [PATCH 5/7] Use os.pathsep instead of a colon --- mypy/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mypy/main.py b/mypy/main.py index 19656e189e36..e56ab953f972 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -454,7 +454,9 @@ 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)], + 'mypy_path': lambda s: [ + p.strip() for p in re.split('[,{}]'.format(os.pathsep), s) + ], 'junit_xml': str, } From 4084ac44a1f7fc5afb4931ab4b462f3e7f4b9092 Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 28 Oct 2016 21:21:14 +0200 Subject: [PATCH 6/7] Use typeshed commit from master --- typeshed | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeshed b/typeshed index f6bbc46c35b4..fed966cf7f5b 160000 --- a/typeshed +++ b/typeshed @@ -1 +1 @@ -Subproject commit f6bbc46c35b4c54cc6dd3268062df447ac81e718 +Subproject commit fed966cf7f5b3cddb731b846f7781b863b47a7cc From 60dff4bb8a195fe9dd1e041a77d3b35c8862dfad Mon Sep 17 00:00:00 2001 From: Filip Figiel Date: Fri, 28 Oct 2016 21:23:49 +0200 Subject: [PATCH 7/7] Avoid cross-platform issues with mypy_path separators --- mypy/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index e56ab953f972..19656e189e36 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -454,9 +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('[,{}]'.format(os.pathsep), s) - ], + 'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)], 'junit_xml': str, }