Skip to content

Commit a1fca10

Browse files
Filip Figielddfisher
Filip Figiel
authored andcommitted
Add mypy_path config option (#2323) (#2329)
* Allow to specify mypy_path in config (#2323) * Docs on the new stubs_dir config option * Accept either a colon or a comma in config.mypy_path * Add a test for mypy_path config setting * Use os.pathsep instead of a colon * Use typeshed commit from master * Avoid cross-platform issues with mypy_path separators
1 parent c17b8bd commit a1fca10

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

docs/source/config_file.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ The following global flags may only be set in the global section
5151
alternative directory which is used to look for stubs instead of the
5252
default ``typeshed`` directory.
5353

54+
- ``mypy_path`` (string) specifies the paths to use, after trying the paths
55+
from ``MYPYPATH`` environment variable. Useful if you'd like to keep stubs
56+
in your repo, along with the config file.
57+
5458
- ``warn_incomplete_stub`` (Boolean, default False) warns for missing
5559
type annotation in typeshed. This is only relevant in combination
5660
with ``check_untyped_defs``.

mypy/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ def build(sources: List[BuildSource],
153153
# to the lib_path
154154
lib_path.insert(0, os.getcwd())
155155

156+
# Prepend a config-defined mypy path.
157+
lib_path[:0] = options.mypy_path
158+
156159
# Add MYPYPATH environment variable to front of library path, if defined.
157160
lib_path[:0] = mypy_path()
158161

mypy/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def get_init_file(dir: str) -> Optional[str]:
454454
'strict_optional_whitelist': lambda s: s.split(),
455455
'custom_typing_module': str,
456456
'custom_typeshed_dir': str,
457+
'mypy_path': lambda s: [p.strip() for p in re.split('[,:]', s)],
457458
'junit_xml': str,
458459
}
459460

mypy/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self) -> None:
3636
self.platform = sys.platform
3737
self.custom_typing_module = None # type: Optional[str]
3838
self.custom_typeshed_dir = None # type: Optional[str]
39+
self.mypy_path = [] # type: List[str]
3940
self.report_dirs = {} # type: Dict[str, str]
4041
self.silent_imports = False
4142
self.almost_silent = False

test-data/unit/cmdline.test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,28 @@ def untyped_function():
296296
</package>
297297
</packages>
298298
</coverage>
299+
300+
[case testConfigMypyPath]
301+
# cmd: mypy file.py
302+
[file mypy.ini]
303+
[[mypy]
304+
mypy_path =
305+
foo:bar
306+
, baz
307+
[file foo/foo.pyi]
308+
def foo(x: int) -> str: ...
309+
[file bar/bar.pyi]
310+
def bar(x: str) -> list: ...
311+
[file baz/baz.pyi]
312+
def baz(x: list) -> dict: ...
313+
[file file.py]
314+
import no_stubs
315+
from foo import foo
316+
from bar import bar
317+
from baz import baz
318+
baz(bar(foo(42)))
319+
baz(bar(foo('oof')))
320+
[out]
321+
file.py:1: error: Cannot find module named 'no_stubs'
322+
file.py:1: note: (Perhaps setting MYPYPATH or using the "--silent-imports" flag would help)
323+
file.py:6: error: Argument 1 to "foo" has incompatible type "str"; expected "int"

0 commit comments

Comments
 (0)