From b7f934de0d5ded5a120685d92ae07c2eb54b9ff1 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 6 Dec 2015 18:41:34 +0200 Subject: Added a new error, 'relative-beyond-top-level'. This is emitted when a relative import was attempted beyond the top level package. For instance, if a package has X levels, trying to climb X + n levels with a relative import, as in `from ..stuff import Stuff`, will result in an error. Closes issue #588. --- pylint/test/unittest_checker_imports.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'pylint/test/unittest_checker_imports.py') diff --git a/pylint/test/unittest_checker_imports.py b/pylint/test/unittest_checker_imports.py index f5e6bbb..0069be2 100644 --- a/pylint/test/unittest_checker_imports.py +++ b/pylint/test/unittest_checker_imports.py @@ -1,6 +1,8 @@ """Unit tests for the imports checker.""" +import os import unittest +import astroid from astroid import test_utils from pylint.checkers import imports from pylint.testutils import CheckerTestCase, Message, set_config @@ -62,7 +64,7 @@ class ImportsCheckerTC(CheckerTestCase): with self.assertNoMessages(): self.checker.visit_import(node) - def test_visit_importfrom(self): + def test_reimported_same_line(self): """ Test that duplicate imports on single line raise 'reimported'. """ @@ -71,5 +73,23 @@ class ImportsCheckerTC(CheckerTestCase): with self.assertAddsMessages(msg): self.checker.visit_importfrom(node) + def test_relative_beyond_top_level(self): + here = os.path.abspath(os.path.dirname(__file__)) + path = os.path.join(here, 'regrtest_data', 'beyond_top', '__init__.py') + with open(path) as stream: + data = stream.read() + module = astroid.parse(data, module_name='beyond_top', path=path) + import_from = module.body[0] + + msg = Message(msg_id='relative-beyond-top-level', + node=import_from) + with self.assertAddsMessages(msg): + self.checker.visit_importfrom(import_from) + with self.assertNoMessages(): + self.checker.visit_importfrom(module.body[1]) + with self.assertNoMessages(): + self.checker.visit_importfrom(module.body[2].body[0]) + + if __name__ == '__main__': unittest.main() -- cgit v1.2.1