diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-14 00:13:31 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-07-14 00:13:31 +0300 |
commit | 3f163ad00a0019e987039ed4894d4df8ad479a1f (patch) | |
tree | c432bfddb0c8cdae9615d0a842870fb4130be7e4 | |
parent | 41f07a39030b31eaf7e2a262618bf32014deb7b7 (diff) | |
download | astroid-3f163ad00a0019e987039ed4894d4df8ad479a1f.tar.gz |
do_import_module passes the proper relative_only flag if the level is higher than 1.
This has the side effect that using `from .something import something`
in a non-package will finally result in an import-error on Pylint's side.
Until now relative_only was ignored, leading to the import of `something`,
if it was globally available.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | astroid/mixins.py | 3 | ||||
-rw-r--r-- | astroid/tests/unittest_inference.py | 9 |
3 files changed, 8 insertions, 10 deletions
@@ -228,6 +228,12 @@ Change log for the astroid package (used to be astng) an astroid AST from a source code string, similar to how ast.parse can be used to obtain a Python AST from a source string. This is the test_utils.build_module promoted to a public API. + + * do_import_module passes the proper relative_only flag if the level is higher + than 1. This has the side effect that using `from .something import something` + in a non-package will finally result in an import-error on Pylint's side. + Until now relative_only was ignored, leading to the import of `something`, + if it was globally available. diff --git a/astroid/mixins.py b/astroid/mixins.py index 185ff16..e702190 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -104,7 +104,8 @@ class FromImportMixIn(FilterStmtsMixin): # FIXME: we used to raise InferenceError here, but why ? return mymodule try: - return mymodule.import_module(modname, level=level) + return mymodule.import_module(modname, level=level, + relative_only=level and level >= 1) except AstroidBuildingException as ex: if isinstance(ex.args[0], SyntaxError): raise InferenceError(str(ex)) diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index df048b7..54bc38f 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1098,15 +1098,6 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): "ignored") self.assertNotIn("RuntimeError", output, msg) - def test_python25_relative_import(self): - data = "from ...logilab.common import date; print (date)" - # !! FIXME also this relative import would not work 'in real' (no __init__.py in test/) - # the test works since we pretend we have a package by passing the full modname - ast = builder.string_build(data, 'astroid.test.unittest_inference', __file__) - infered = next(test_utils.get_name_node(ast, 'date').infer()) - self.assertIsInstance(infered, nodes.Module) - self.assertEqual(infered.name, 'logilab.common.date') - def test_python25_no_relative_import(self): ast = resources.build_file('data/package/absimport.py') self.assertTrue(ast.absolute_import_activated(), True) |