test and fix absolute import handling. Closes #106191

authorSylvain Th?nault <sylvain.thenault@logilab.fr>
changeset55333bb6658e
branchstable
phasepublic
hiddenno
parent revision#77a24b16b91f fix function fromlineno when some decorator is splited on multiple lines. Closes #50395
child revision#31dd11229614 backport pylint-brain 27:2c83031cb6a3. Closes #4294, #46273
files modified by this revision
rebuilder.py
test/data/absimport.py
test/data/email.py
test/unittest_manager.py
test/unittest_nodes.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1349433346 -7200
# Fri Oct 05 12:35:46 2012 +0200
# Branch stable
# Node ID 55333bb6658e5fe9b0d0217193497ba9153e35be
# Parent 77a24b16b91f383dd21829d10f9eed0f788d6b91
test and fix absolute import handling. Closes #106191

diff --git a/rebuilder.py b/rebuilder.py
@@ -455,11 +455,11 @@
1          return newnode
2 
3      def visit_from(self, node, parent):
4          """visit a From node by returning a fresh instance of it"""
5          names = [(alias.name, alias.asname) for alias in node.names]
6 -        newnode = new.From(node.module or '', names, node.level)
7 +        newnode = new.From(node.module or '', names, node.level or None)
8          _set_infos(node, newnode, parent)
9          # store From names to add them to locals after building
10          self._from_nodes.append(newnode)
11          return newnode
12 
diff --git a/test/data/absimport.py b/test/data/absimport.py
@@ -0,0 +1,3 @@
13 +from __future__ import absolute_import
14 +import email
15 +from email import message
diff --git a/test/data/email.py b/test/data/email.py
@@ -0,0 +1,1 @@
16 +"""fake email module to test absolute import doesn't grab this one"""
diff --git a/test/unittest_manager.py b/test/unittest_manager.py
@@ -81,12 +81,13 @@
17 
18      def test_project_node(self):
19          obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
20          expected = set(['SSL1', '__init__', 'all', 'appl', 'format', 'module',
21                          'module2', 'noendingnewline', 'nonregr', 'notall'])
22 -        expected = ['data', 'data.SSL1', 'data.SSL1.Connection1', 'data.all',
23 -                    'data.appl', 'data.appl.myConnection', 'data.format',
24 +        expected = ['data', 'data.SSL1', 'data.SSL1.Connection1',
25 +                    'data.absimport', 'data.all',
26 +                    'data.appl', 'data.appl.myConnection', 'data.email', 'data.format',
27                      'data.module', 'data.module2', 'data.noendingnewline',
28                      'data.nonregr', 'data.notall']
29          self.assertListEqual(sorted(k for k in obj.keys()), expected)
30 
31      def test_do_not_expose_main(self):
diff --git a/test/unittest_nodes.py b/test/unittest_nodes.py
@@ -33,11 +33,11 @@
32  """
33  import sys
34 
35  from logilab.common import testlib
36  from logilab.astng.node_classes import unpack_infer
37 -from logilab.astng.bases import YES
38 +from logilab.astng.bases import YES, InferenceContext
39  from logilab.astng.exceptions import ASTNGBuildingException, NotFoundError
40  from logilab.astng import BUILTINS_MODULE, builder, nodes
41  from logilab.astng.as_string import as_string
42 
43  from data import module as test_module
@@ -304,10 +304,21 @@
44          from_node = astng.body[1].handlers[0].body[0]
45          handler_type = astng.body[1].handlers[0].type
46 
47          excs = list(unpack_infer(handler_type))
48 
49 +    def test_absolute_import(self):
50 +        astng = abuilder.file_build(self.datapath('absimport.py'))
51 +        ctx = InferenceContext()
52 +        ctx.lookupname = 'message'
53 +        # will fail if absolute import failed
54 +        astng['message'].infer(ctx).next()
55 +        ctx.lookupname = 'email'
56 +        m = astng['email'].infer(ctx).next()
57 +        self.assertFalse(m.file.startswith(self.datapath('email.py')))
58 +
59 +
60  class CmpNodeTC(testlib.TestCase):
61      def test_as_string(self):
62          ast = abuilder.string_build("a == 2").body[0]
63          self.assertEqual(as_string(ast), "a == 2")
64