Commits

Joe Amenta committed 381fa83

Fixing an issue where the import fixer would improperly fix attributes
that happened to share the same name as an imported module

Comments (0)

Files changed (2)

lib3to2/fixes/fix_imports.py

         elif name_import or from_import:
             self.fix_simple_name(name)
         elif name and not attr:
-            if does_tree_import(None, MAPPING[name.value], node):
+            if does_tree_import(None, MAPPING[name.value], node) and \
+               is_probably_builtin(name):
                 self.fix_simple_name(name)
         elif name and attr:
             # Note that this will fix a dotted name that was never imported.  This will probably not matter.

lib3to2/tests/test_imports.py

         import __builtin__
         hasattr(__builtin__, "quit")"""
         self.check(b, a)
+
+    def test_no_attribute(self):
+        b = """
+        import collections
+        import queue
+
+        MyTuple = collections.namedtuple('MyTuple', ['queue', 'queue1'])
+
+        tuple_instance = MyTuple(queue.Queue(), queue.Queue())
+
+        tuple_instance.queue.put(1)
+        tuple_instance.queue1.put(1)"""
+
+        a = """
+        import collections
+        import Queue
+
+        MyTuple = collections.namedtuple('MyTuple', ['queue', 'queue1'])
+
+        tuple_instance = MyTuple(Queue.Queue(), Queue.Queue())
+
+        tuple_instance.queue.put(1)
+        tuple_instance.queue1.put(1)"""
+        self.check(b, a)