Skip to content

Support importing types from nested packages #5591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,8 @@ def stats_summary(self) -> Mapping[str, object]:


# Package dirs are a two-tuple of path to search and whether to verify the module
PackageDirs = List[Tuple[str, bool]]
OnePackageDir = Tuple[str, bool]
PackageDirs = List[OnePackageDir]


class FindModuleCache:
Expand Down Expand Up @@ -896,6 +897,15 @@ def find_module(self, id: str, search_paths: SearchPaths,
self.results[key] = self._find_module(id, search_paths, python_executable)
return self.results[key]

def _find_module_non_stub_helper(self, components: List[str],
pkg_dir: str) -> Optional[OnePackageDir]:
dir_path = pkg_dir
for index, component in enumerate(components):
dir_path = os.path.join(dir_path, component)
if self.fscache.isfile(os.path.join(dir_path, 'py.typed')):
return os.path.join(pkg_dir, *components[:-1]), index == 0
return None

def _find_module(self, id: str, search_paths: SearchPaths,
python_executable: Optional[str]) -> Optional[str]:
fscache = self.fscache
Expand All @@ -910,12 +920,11 @@ def _find_module(self, id: str, search_paths: SearchPaths,

# We have two sets of folders so that we collect *all* stubs folders and
# put them in the front of the search path
third_party_inline_dirs = []
third_party_stubs_dirs = []
third_party_inline_dirs = [] # type: PackageDirs
third_party_stubs_dirs = [] # type: PackageDirs
# Third-party stub/typed packages
for pkg_dir in search_paths.package_path:
stub_name = components[0] + '-stubs'
typed_file = os.path.join(pkg_dir, components[0], 'py.typed')
stub_dir = os.path.join(pkg_dir, stub_name)
if fscache.isdir(stub_dir):
stub_typed_file = os.path.join(stub_dir, 'py.typed')
Expand All @@ -935,9 +944,9 @@ def _find_module(self, id: str, search_paths: SearchPaths,
third_party_stubs_dirs.append((path, False))
else:
third_party_stubs_dirs.append((path, True))
elif fscache.isfile(typed_file):
path = os.path.join(pkg_dir, dir_chain)
third_party_inline_dirs.append((path, True))
non_stub_match = self._find_module_non_stub_helper(components, pkg_dir)
if non_stub_match:
third_party_inline_dirs.append(non_stub_match)
if self.options and self.options.use_builtins_fixtures:
# Everything should be in fixtures.
third_party_inline_dirs.clear()
Expand Down Expand Up @@ -2357,11 +2366,13 @@ def find_module_and_diagnose(manager: BuildManager,
if not (options.ignore_missing_imports or in_partial_package(id, manager)):
module_not_found(manager, caller_line, caller_state, id)
raise ModuleNotFound
else:
elif root_source:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be if since the previous if ḅlock always raises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I prefer the explicitness of using elif when chaining if statements but if its an issue I can change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case it would behoove you to change the if on L2375 to elif as well. :-) I'll let it slide.

# If we can't find a root source it's always fatal.
# TODO: This might hide non-fatal errors from
# root sources processed earlier.
raise CompileError(["mypy: can't find module '%s'" % id])
else:
raise ModuleNotFound


def exist_added_packages(suppressed: List[str],
Expand Down
34 changes: 34 additions & 0 deletions mypy/test/testpep561.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
reveal_type(a)
"""

NAMESPACE_PROGRAM = """
from typedpkg_nested.nested_package.nested_module import nested_func
from typedpkg_namespace.alpha.alpha_module import alpha_func

nested_func("abc")
alpha_func(False)

nested_func(False)
alpha_func(2)
"""


def check_mypy_run(cmd_line: List[str],
python_executable: str = sys.executable,
Expand Down Expand Up @@ -95,13 +106,24 @@ def setUp(self) -> None:
self.tempfile = os.path.join(self.temp_file_dir.name, 'simple.py')
with open(self.tempfile, 'w+') as file:
file.write(SIMPLE_PROGRAM)
self.namespace_tempfile = os.path.join(self.temp_file_dir.name, 'namespace_program.py')
with open(self.namespace_tempfile, 'w+') as file:
file.write(NAMESPACE_PROGRAM)

self.msg_dne = \
"{}:3: error: Module 'typedpkg' has no attribute 'dne'\n".format(self.tempfile)
self.msg_list = \
"{}:5: error: Revealed type is 'builtins.list[builtins.str]'\n".format(self.tempfile)
self.msg_tuple = \
"{}:5: error: Revealed type is 'builtins.tuple[builtins.str]'\n".format(self.tempfile)

self.namespace_msg_bool_str = (
'{0}:8: error: Argument 1 to "nested_func" has incompatible type "bool"; '
'expected "str"\n'.format(self.namespace_tempfile))
self.namespace_msg_int_bool = (
'{0}:9: error: Argument 1 to "alpha_func" has incompatible type "int"; '
'expected "bool"\n'.format(self.namespace_tempfile))

def tearDown(self) -> None:
self.temp_file_dir.cleanup()

Expand Down Expand Up @@ -192,6 +214,18 @@ def test_typedpkg_editable(self) -> None:
venv_dir=venv_dir,
)

def test_nested_and_namespace(self) -> None:
with self.virtualenv() as venv:
venv_dir, python_executable = venv
self.install_package('typedpkg_nested', python_executable)
self.install_package('typedpkg_namespace-alpha', python_executable)
check_mypy_run(
[self.namespace_tempfile],
python_executable,
expected_out=self.namespace_msg_bool_str + self.namespace_msg_int_bool,
venv_dir=venv_dir,
)


if __name__ == '__main__':
main()
10 changes: 10 additions & 0 deletions test-data/packages/typedpkg_namespace-alpha/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from setuptools import setup, find_packages

setup(
name='typedpkg_namespace.alpha',
version='1.0.0',
packages=find_packages(),
namespace_packages=['typedpkg_namespace'],
zip_safe=False,
package_data={'typedpkg_namespace.alpha': ['py.typed']}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# namespace pkg
__import__("pkg_resources").declare_namespace(__name__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def alpha_func(a: bool) -> bool:
return not a
9 changes: 9 additions & 0 deletions test-data/packages/typedpkg_nested/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from setuptools import setup, find_packages

setup(
name='typedpkg_nested',
version='1.0.0',
packages=find_packages(),
zip_safe=False,
package_data={'typedpkg_nested.nested_package': ['py.typed']}
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def nested_func(a: str) -> str:
return a + " nested"