From a430282220476ad0fe87fadf586c70f7f4113430 Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Tue, 15 Sep 2020 11:39:46 -0500 Subject: [PATCH 1/2] Recognize -stubs directories as valid package directories --- mypy/find_sources.py | 4 ++++ test-data/unit/cmdline.test | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/mypy/find_sources.py b/mypy/find_sources.py index e96dc2d617ce..bdf30fbbd12b 100644 --- a/mypy/find_sources.py +++ b/mypy/find_sources.py @@ -127,6 +127,10 @@ def crawl_up_dir(self, dir: str) -> Tuple[str, str]: res = '' base_dir = dir or '.' else: + # Remove -stubs to treat PEP-561 stub-only directories as packages + if base.endswith('-stubs'): + base = base[:-6] + # Ensure that base is a valid python module name if not base.isidentifier(): raise InvalidSourceList('{} is not a valid Python package name'.format(base)) diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 8fc563dce249..04f9d3f0276e 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -1176,3 +1176,39 @@ usage: mypy [-h] [-v] [-V] [more options; see below] [-m MODULE] [-p PACKAGE] [-c PROGRAM_TEXT] [files ...] mypy: error: Invalid error code(s): YOLO == Return code: 2 + +[case testStubsDirectory] +# cmd: mypy --error-summary pkg-stubs +[file pkg-stubs/__init__.pyi] +[file pkg-stubs/thing.pyi] +class Thing: ... +[out] +Success: no issues found in 2 source files +== Return code: 0 + +[case testStubsDirectoryFile] +# cmd: mypy --error-summary pkg-stubs/thing.pyi +[file pkg-stubs/__init__.pyi] +[file pkg-stubs/thing.pyi] +class Thing: ... +[out] +Success: no issues found in 1 source file +== Return code: 0 + +[case testStubsSubDirectory] +# cmd: mypy --error-summary src/pkg-stubs +[file src/pkg-stubs/__init__.pyi] +[file src/pkg-stubs/thing.pyi] +class Thing: ... +[out] +Success: no issues found in 2 source files +== Return code: 0 + +[case testStubsSubDirectoryFile] +# cmd: mypy --error-summary src/pkg-stubs/thing.pyi +[file src/pkg-stubs/__init__.pyi] +[file src/pkg-stubs/thing.pyi] +class Thing: ... +[out] +Success: no issues found in 1 source file +== Return code: 0 From 1c65c1e14b6fc4883b26504e020d03576dde675b Mon Sep 17 00:00:00 2001 From: Bryan Forbes Date: Wed, 16 Sep 2020 09:46:34 -0500 Subject: [PATCH 2/2] Update mypy/find_sources.py based on feedback Co-authored-by: Guido van Rossum --- mypy/find_sources.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mypy/find_sources.py b/mypy/find_sources.py index bdf30fbbd12b..e9dd9edecec5 100644 --- a/mypy/find_sources.py +++ b/mypy/find_sources.py @@ -127,11 +127,9 @@ def crawl_up_dir(self, dir: str) -> Tuple[str, str]: res = '' base_dir = dir or '.' else: - # Remove -stubs to treat PEP-561 stub-only directories as packages - if base.endswith('-stubs'): - base = base[:-6] - # Ensure that base is a valid python module name + if base.endswith('-stubs'): + base = base[:-6] # PEP-561 stub-only directory if not base.isidentifier(): raise InvalidSourceList('{} is not a valid Python package name'.format(base)) parent, base_dir = self.crawl_up_dir(parent_dir)