-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
380006a
f1566b9
8b5c2b8
6f48b71
b5dc6b7
9e5548f
983b6eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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 | ||
|
@@ -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 | ||
gvanrossum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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') | ||
|
@@ -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() | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I prefer the explicitness of using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case it would behoove you to change the |
||
# 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], | ||
|
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 |
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']} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def nested_func(a: str) -> str: | ||
return a + " nested" |
Uh oh!
There was an error while loading. Please reload this page.