Skip to content

Commit

Permalink
Raise an error when referencing a nonexistent pyi file in another pyi…
Browse files Browse the repository at this point in the history
… file.

PiperOrigin-RevId: 448589096
  • Loading branch information
martindemello authored and rchen152 committed May 16, 2022
1 parent f9be18d commit 1613003
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pytype/load_pytd.py
Expand Up @@ -624,13 +624,17 @@ def finish_and_verify_ast(self, mod_ast):
if mod_ast:
try:
self._resolver.verify(mod_ast)
except (BadDependencyError, visitors.ContainerError):
except (BadDependencyError, visitors.ContainerError) as e:
# In the case of a circular import, an external type may be left
# unresolved, so we re-resolve lookups in this module and its direct
# dependencies. Technically speaking, we should re-resolve all
# transitive imports, but lookups are expensive.
dependencies = self._resolver.collect_dependencies(mod_ast)
for k in dependencies:
if k not in self._modules:
raise (
BadDependencyError("Can't find pyi for %r" % k, mod_ast.name)
) from e
self._modules[k].ast = self._resolve_external_types(
self._modules[k].ast)
mod_ast = self._resolve_external_types(mod_ast)
Expand Down
13 changes: 13 additions & 0 deletions pytype/tests/test_pyi1.py
Expand Up @@ -898,6 +898,19 @@ class X:
import bad # pyi-error
""", pythonpath=[d.path])

def test_nonexistent_import(self):
with file_utils.Tempdir() as d:
d.create_file("bad.pyi", """
import nonexistent
x = nonexistent.x
""")
err = self.CheckWithErrors("""
import bad # pyi-error[e]
""", pythonpath=[d.path])
self.assertErrorSequences(err, {
"e": ["Couldn't import pyi", "nonexistent", "referenced from", "bad"]
})


if __name__ == "__main__":
test_base.main()

0 comments on commit 1613003

Please sign in to comment.