def test_other_junk(self): self.assertFalse( model.compute_all_imports( util.File('some_file.py', '# import foo\n'))) self.assertFalse( model.compute_all_imports( util.File('some_file.py', ' # import foo\n'))) self.assertFalse( model.compute_all_imports( util.File('some_file.py', 'def foo(): pass\n'))) self.assertFalse( model.compute_all_imports( util.File('some_file.py', '"""imports are "fun" in a multiline string"""\n'))) self.assertFalse( model.compute_all_imports( util.File('some_file.py', 'from __future__ import absolute_import\n')))
def test_late_import(self): file_info = util.File('some_file.py', ('def f():\n' ' import foo\n')) self._assert_localnames( model.localnames_from_localnames(file_info, {'foo'}), {('foo', 'foo', ('foo', 'foo', 13, 23))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'foo'}, imports=model.compute_all_imports(file_info)), {('foo', 'foo', ('foo', 'foo', 13, 23))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'foo'}, imports=model.compute_all_imports(file_info, toplevel_only=True)), set())
def test_relative_import(self): self._assert_imports( model.compute_all_imports( util.File('foo/bar/some_file.py', 'from . import baz\n')), {('foo.bar.baz', 'baz', 0, 17, 'explicit')}) self._assert_imports( model.compute_all_imports( util.File('foo/some_file.py', 'from .bar import baz\n')), {('foo.bar.baz', 'baz', 0, 20, 'explicit')}) self._assert_imports( model.compute_all_imports( util.File('foo/bar/junk/some_file.py', 'from .. import baz\n')), {('foo.bar.baz', 'baz', 0, 18, 'explicit')}) self._assert_imports( model.compute_all_imports( util.File('foo/bar/some_file.py', 'from ..bar import baz\n')), {('foo.bar.baz', 'baz', 0, 21, 'explicit')}) self._assert_imports( model.compute_all_imports( util.File('junk/junk/some_file.py', 'from ...foo.bar import baz\n')), {('foo.bar.baz', 'baz', 0, 26, 'explicit')}) self._assert_imports( model.compute_all_imports( util.File('junk/junk/some_file.py', 'from ... import foo\n')), {('foo', 'foo', 0, 19, 'explicit')})
def test_within_node(self): file_info = util.File('some_file.py', ('import bar\n\n\n' 'def f():\n' ' import foo as bar\n')) def_node = file_info.tree.body[1] self._assert_localnames( model.localnames_from_localnames(file_info, {'bar'}), {('bar', 'bar', ('bar', 'bar', 0, 10)), ('foo', 'bar', ('foo', 'bar', 26, 43))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'bar'}, imports=model.compute_all_imports(file_info)), {('bar', 'bar', ('bar', 'bar', 0, 10)), ('foo', 'bar', ('foo', 'bar', 26, 43))}) self._assert_localnames( model.localnames_from_localnames(file_info, {'bar'}, imports=model.compute_all_imports( file_info, within_node=def_node)), {('foo', 'bar', ('foo', 'bar', 26, 43))})
def test_simple(self): self._assert_imports( model.compute_all_imports(util.File('some_file.py', 'import foo\n')), {('foo', 'foo', 0, 10)})
def _create_import(self, import_text): """Return an Import object for the given text.""" (imp, ) = list( model.compute_all_imports(util.File('some_file.py', import_text))) return imp