Set literals should be treated as inference leaves. Closes #47957

authorTorsten Marek <tmarek@google.com>
changeset41db38a1c28e
branchstable
phasepublic
hiddenno
parent revision#99125c558945 Fix inference for generator methods to correctly handle yields in lambdas. Closes #123068
child revision#6e82e28d4a0d Add support for inference of subscript operations on dict literals. Closes #123074
files modified by this revision
ChangeLog
inference.py
rebuilder.py
test/unittest_inference.py
# HG changeset patch
# User Torsten Marek <tmarek@google.com>
# Date 1364387275 -3600
# Wed Mar 27 13:27:55 2013 +0100
# Branch stable
# Node ID 41db38a1c28ede2883202a1eb872d199753d03e9
# Parent 99125c558945037fc619d1eebd47f9238c502968
Set literals should be treated as inference leaves. Closes #47957

diff --git a/ChangeLog b/ChangeLog
@@ -7,10 +7,11 @@
1      * #123056 [pylint-brain]: Add missing methods for hashlib
2      * #123068: Fix inference for generator methods to correctly handle yields
3        in lambdas.
4      * #123068: Make sure .as_string() returns valid code for yields in
5        expressions.
6 +    * #47957: Set literals are now correctly treated as inference leaves.
7 
8  2013-02-27  -- 0.24.2
9      * pylint-brain: more subprocess.Popen faking (see #46273)
10      * #109562 [jython]: java modules have no __doc__, causing crash
11      * #120646 [py3]: fix for python3.3 _ast changes which may cause crash
diff --git a/inference.py b/inference.py
@@ -135,11 +135,11 @@
12  nodes.Lambda.infer = infer_end
13  nodes.Const.infer = infer_end
14  nodes.List.infer = infer_end
15  nodes.Tuple.infer = infer_end
16  nodes.Dict.infer = infer_end
17 -
18 +nodes.Set.infer = infer_end
19 
20  def infer_name(self, context=None):
21      """infer a Name: use name lookup rules"""
22      frame, stmts = self.lookup(self.name)
23      if not stmts:
diff --git a/rebuilder.py b/rebuilder.py
@@ -696,11 +696,11 @@
24              newnode.value = self.visit(node.value, newnode)
25          newnode.set_line_info(newnode.last_child())
26          return newnode
27 
28      def visit_set(self, node, parent):
29 -        """visit a Tuple node by returning a fresh instance of it"""
30 +        """visit a Set node by returning a fresh instance of it"""
31          newnode = new.Set()
32          _lineno_parent(node, newnode, parent)
33          newnode.elts = [self.visit(child, newnode) for child in node.elts]
34          newnode.set_line_info(newnode.last_child())
35          return newnode
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
@@ -1,6 +1,6 @@
36 -# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
37 +# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
38  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
39  #
40  # This file is part of logilab-astng.
41  #
42  # logilab-astng is free software: you can redistribute it and/or modify it
@@ -430,10 +430,22 @@
43          self.assertIn('lower', infered._proxied.locals)
44          n = astng['s2']
45          infered = n.infer().next()
46          self.assertEqual(infered.getitem(0).value, '_')
47 
48 +    def test_builtin_types_py27(self):
49 +        if sys.version_info < (2, 7):
50 +            self.skipTest('set literal introduced in python 2.7')
51 +        code = 's = {1}'
52 +        astng = builder.string_build(code, __name__, __file__)
53 +        n = astng['s']
54 +        infered = n.infer().next()
55 +        self.assertIsInstance(infered, nodes.Set)
56 +        self.assertIsInstance(infered, Instance)
57 +        self.assertEqual(infered.name, 'set')
58 +        self.assertIn('remove', infered._proxied.locals)
59 +
60      def test_unicode_type(self):
61          if sys.version_info >= (3, 0):
62              self.skipTest('unicode removed on py >= 3.0')
63          code = '''u = u""'''
64          astng = builder.string_build(code, __name__, __file__)