diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-06 18:41:34 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-06 18:41:34 +0200 |
commit | b7f934de0d5ded5a120685d92ae07c2eb54b9ff1 (patch) | |
tree | d5ae59bdcf3d8099f907fe854c9ee315bdfa9281 /pylint/test/unittest_checker_imports.py | |
parent | b700c2f8a0e6b8d61c3abea8d41438f66cd55025 (diff) | |
download | pylint-b7f934de0d5ded5a120685d92ae07c2eb54b9ff1.tar.gz |
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.
Diffstat (limited to 'pylint/test/unittest_checker_imports.py')
-rw-r--r-- | pylint/test/unittest_checker_imports.py | 22 |
1 files changed, 21 insertions, 1 deletions
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() |