summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-02-18 21:55:32 +0000
committerClaudiu Popa <pcmanticore@gmail.com>2016-02-20 22:35:08 +0000
commit1d5bf7ec3c74496e2b9b5772cbe953a962ccd3d9 (patch)
treeeba358917ab928b17eda30e4a1932be08fb68ee2
parentce80c835a492ccdf05b9786f0e0960327a51826e (diff)
downloadastroid-git-1d5bf7ec3c74496e2b9b5772cbe953a962ccd3d9.tar.gz
imported_names() got replaced by public_names()
Our understanding of wildcard imports through __all__ was half baked to say at least, since we couldn't account for modifications of the list, which results in tons of false positives. Instead, we replaced it with public_names(), a method which returns all the names that are publicly available in a module, that is that don't start with an underscore, even though this means that there is a possibility for other names to be leaked out even though they are not present in the __all__ variable.
-rw-r--r--ChangeLog16
-rw-r--r--astroid/builder.py2
-rw-r--r--astroid/scoped_nodes.py4
-rw-r--r--astroid/tests/unittest_regrtest.py2
-rw-r--r--astroid/tests/unittest_scoped_nodes.py28
5 files changed, 47 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 759b6c18..b2c10474 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,22 @@
Change log for the astroid package (used to be astng)
=====================================================
+--
+
+ * wildcard_imported_names() got replaced by _public_names()
+
+ Our understanding of wildcard imports through __all__ was
+ half baked to say at least, since we couldn't account for
+ modifications of the list, which results in tons of false positives.
+ Instead, we replaced it with _public_names(), a method which returns
+ all the names that are publicly available in a module, that is that
+ don't start with an underscore, even though this means that there
+ is a possibility for other names to be leaked out even though
+ they are not present in the __all__ variable.
+
+ The method is private in 1.4.X.
+
+
2016-01-15 -- 1.4.4
* unpack_infer raises InferenceError if it can't operate
diff --git a/astroid/builder.py b/astroid/builder.py
index 4a9c3569..63c156a1 100644
--- a/astroid/builder.py
+++ b/astroid/builder.py
@@ -202,7 +202,7 @@ class AstroidBuilder(raw_building.InspectBuilder):
imported = node.do_import_module()
except exceptions.InferenceError:
continue
- for name in imported.wildcard_import_names():
+ for name in imported._public_names():
node.parent.set_local(name, node)
sort_locals(node.parent.scope()._locals[name])
else:
diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py
index c0b01790..1a9c7f20 100644
--- a/astroid/scoped_nodes.py
+++ b/astroid/scoped_nodes.py
@@ -568,6 +568,10 @@ class Module(LocalsDictNodeNG):
inferred.append(inferred_node.value)
return inferred
+ def _public_names(self):
+ """Get the list of the names which are publicly available in this module."""
+ return [name for name in self.keys() if not name.startswith('_')]
+
def bool_value(self):
return True
diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py
index 767b8de8..970f639c 100644
--- a/astroid/tests/unittest_regrtest.py
+++ b/astroid/tests/unittest_regrtest.py
@@ -149,7 +149,7 @@ multiply(1, 2, 3)
astroid = builder.string_build(data, __name__, __file__)
callfunc = astroid.body[1].value.func
inferred = callfunc.inferred()
- self.assertEqual(len(inferred), 1)
+ self.assertEqual(len(inferred), 2)
@require_version('3.0')
def test_nameconstant(self):
diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py
index 9a7c99ad..0019ee08 100644
--- a/astroid/tests/unittest_scoped_nodes.py
+++ b/astroid/tests/unittest_scoped_nodes.py
@@ -111,19 +111,41 @@ class ModuleNodeTest(ModuleLoader, unittest.TestCase):
res = sorted(m.wildcard_import_names())
self.assertEqual(res, ['Aaa', 'func', 'name', 'other'])
+ def test_public_names(self):
+ m = builder.parse('''
+ name = 'a'
+ _bla = 2
+ other = 'o'
+ class Aaa: pass
+ def func(): print('yo')
+ __all__ = 'Aaa', '_bla', 'name'
+ ''')
+ values = sorted(['Aaa', 'name', 'other', 'func'])
+ self.assertEqual(sorted(m._public_names()), values)
+ m = builder.parse('''
+ name = 'a'
+ _bla = 2
+ other = 'o'
+ class Aaa: pass
+
+ def func(): return 'yo'
+ ''')
+ res = sorted(m._public_names())
+ self.assertEqual(res, values)
+
m = builder.parse('''
from missing import tzop
trop = "test"
__all__ = (trop, "test1", tzop, 42)
''')
- res = sorted(m.wildcard_import_names())
- self.assertEqual(res, ["test", "test1"])
+ res = sorted(m._public_names())
+ self.assertEqual(res, ["trop", "tzop"])
m = builder.parse('''
test = tzop = 42
__all__ = ('test', ) + ('tzop', )
''')
- res = sorted(m.wildcard_import_names())
+ res = sorted(m._public_names())
self.assertEqual(res, ['test', 'tzop'])
def test_module_getattr(self):