diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-11-29 18:35:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-29 18:35:36 +0100 |
commit | 3652faeb5e220a420fba2bb7131c9525beaa8365 (patch) | |
tree | 18db0661792d8fd8cdc27636ce761cd7b4e4ea8c | |
parent | 8a17bb557a23708ebbe64174c564f9c5741fb5dd (diff) | |
download | pylint-git-3652faeb5e220a420fba2bb7131c9525beaa8365.tar.gz |
Move most tests out of ``TestImportsChecker`` (#5434)
-rw-r--r-- | tests/checkers/unittest_imports.py | 100 | ||||
-rw-r--r-- | tests/functional/i/import_error.py | 17 | ||||
-rw-r--r-- | tests/functional/i/import_error.rc | 4 | ||||
-rw-r--r-- | tests/functional/i/import_error.txt | 1 | ||||
-rw-r--r-- | tests/functional/i/import_outside_toplevel.py | 8 | ||||
-rw-r--r-- | tests/functional/i/import_outside_toplevel.rc | 2 | ||||
-rw-r--r-- | tests/functional/i/import_outside_toplevel.txt | 2 |
7 files changed, 35 insertions, 99 deletions
diff --git a/tests/checkers/unittest_imports.py b/tests/checkers/unittest_imports.py index 8f03e5f49..9dbcf2fe1 100644 --- a/tests/checkers/unittest_imports.py +++ b/tests/checkers/unittest_imports.py @@ -26,7 +26,7 @@ import astroid from pylint import epylint as lint from pylint.checkers import imports from pylint.interfaces import UNDEFINED -from pylint.testutils import CheckerTestCase, MessageTest, set_config +from pylint.testutils import CheckerTestCase, MessageTest REGR_DATA = os.path.join(os.path.dirname(__file__), "..", "regrtest_data", "") @@ -35,104 +35,6 @@ class TestImportsChecker(CheckerTestCase): CHECKER_CLASS = imports.ImportsChecker - @set_config(allow_any_import_level=("astroid",)) - def test_import_outside_toplevel(self) -> None: - node = astroid.extract_node( - """ - def f(): - import astroid - """ - ).body[0] - - with self.assertNoMessages(): - self.checker.visit_import(node) - - node = astroid.extract_node( - """ - def g(): - import pylint - """ - ).body[0] - - with self.assertAddsMessages( - MessageTest("import-outside-toplevel", node=node, args="pylint") - ): - self.checker.visit_import(node) - - @set_config( - ignored_modules=("external_module", "fake_module.submodule", "foo", "bar") - ) - def test_import_error_skipped(self) -> None: - """Make sure that imports do not emit an 'import-error' when the - module is configured to be ignored.""" - - node = astroid.extract_node( - """ - from external_module import anything - """ - ) - with self.assertNoMessages(): - self.checker.visit_importfrom(node) - - node = astroid.extract_node( - """ - from external_module.another_module import anything - """ - ) - with self.assertNoMessages(): - self.checker.visit_importfrom(node) - - node = astroid.extract_node( - """ - import external_module - """ - ) - with self.assertNoMessages(): - self.checker.visit_import(node) - - node = astroid.extract_node( - """ - from fake_module.submodule import anything - """ - ) - with self.assertNoMessages(): - self.checker.visit_importfrom(node) - - node = astroid.extract_node( - """ - from fake_module.submodule.deeper import anything - """ - ) - with self.assertNoMessages(): - self.checker.visit_importfrom(node) - - node = astroid.extract_node( - """ - import foo, bar - """ - ) - msg = MessageTest("multiple-imports", node=node, args="foo, bar") - with self.assertAddsMessages(msg): - self.checker.visit_import(node) - - node = astroid.extract_node( - """ - import foo - import bar - """ - ) - with self.assertNoMessages(): - self.checker.visit_import(node) - - def test_reimported_same_line(self) -> None: - """ - Test that duplicate imports on single line raise 'reimported'. - """ - node = astroid.extract_node("from time import sleep, sleep, time") - msg = MessageTest(msg_id="reimported", node=node, args=("sleep", 1)) - with self.assertAddsMessages(msg): - self.checker.visit_importfrom(node) - def test_relative_beyond_top_level(self) -> None: module = astroid.MANAGER.ast_from_module_name("beyond_top", REGR_DATA) import_from = module.body[0] diff --git a/tests/functional/i/import_error.py b/tests/functional/i/import_error.py index 79d8b3416..a2183fcfb 100644 --- a/tests/functional/i/import_error.py +++ b/tests/functional/i/import_error.py @@ -62,3 +62,20 @@ if tp.TYPE_CHECKING: if TYPE_CHECKING: import stub_import from stub_import import stub_class + + +# Test ignore-modules option +from external_module import anything + +from external_module.another_module import anything + +import external_module + +from fake_module.submodule import anything + +from fake_module.submodule.deeper import anything + +import foo, bar # [multiple-imports] + +import foo +import bar diff --git a/tests/functional/i/import_error.rc b/tests/functional/i/import_error.rc index 2901db32d..6bf224fab 100644 --- a/tests/functional/i/import_error.rc +++ b/tests/functional/i/import_error.rc @@ -1,5 +1,9 @@ [Messages Control] disable=C,R,W +enable=multiple-imports + +[TYPECHECK] +ignored-modules=external_module,fake_module.submodule,foo,bar [testoptions] except_implementations=PyPy diff --git a/tests/functional/i/import_error.txt b/tests/functional/i/import_error.txt index 7147acecb..4e7ab2423 100644 --- a/tests/functional/i/import_error.txt +++ b/tests/functional/i/import_error.txt @@ -2,3 +2,4 @@ import-error:3:0:3:22::Unable to import 'totally_missing':UNDEFINED import-error:21:4:21:26::Unable to import 'maybe_missing_2':UNDEFINED no-name-in-module:33:0:33:42::No name 'syntax_error' in module 'functional.s':UNDEFINED syntax-error:33:0:None:None::Cannot import 'functional.s.syntax_error' due to syntax error 'invalid syntax (<unknown>, line 1)':UNDEFINED +multiple-imports:78:0:78:15::Multiple imports on one line (foo, bar):UNDEFINED diff --git a/tests/functional/i/import_outside_toplevel.py b/tests/functional/i/import_outside_toplevel.py index 3721db613..3ed7701cc 100644 --- a/tests/functional/i/import_outside_toplevel.py +++ b/tests/functional/i/import_outside_toplevel.py @@ -40,3 +40,11 @@ def j(): def m(): from math import sin as sign, cos as cosplay # [import-outside-toplevel] + + +# Test allow-any-import-level setting +def n(): + import astroid + +def o(): + import notastroid # [import-error, import-outside-toplevel] diff --git a/tests/functional/i/import_outside_toplevel.rc b/tests/functional/i/import_outside_toplevel.rc new file mode 100644 index 000000000..beef42d69 --- /dev/null +++ b/tests/functional/i/import_outside_toplevel.rc @@ -0,0 +1,2 @@ +[IMPORTS] +allow-any-import-level=astroid diff --git a/tests/functional/i/import_outside_toplevel.txt b/tests/functional/i/import_outside_toplevel.txt index db44956f8..934112224 100644 --- a/tests/functional/i/import_outside_toplevel.txt +++ b/tests/functional/i/import_outside_toplevel.txt @@ -7,3 +7,5 @@ import-outside-toplevel:29:8:29:21:C.j:Import outside toplevel (turtle):UNDEFINE import-outside-toplevel:34:8:34:23:k:Import outside toplevel (tabnanny):UNDEFINED import-outside-toplevel:38:4:38:39:j:Import outside toplevel (collections.defaultdict):UNDEFINED import-outside-toplevel:42:4:42:48:m:Import outside toplevel (math.sin, math.cos):UNDEFINED +import-error:50:4:50:21:o:Unable to import 'notastroid':UNDEFINED +import-outside-toplevel:50:4:50:21:o:Import outside toplevel (notastroid):UNDEFINED |