summaryrefslogtreecommitdiff
path: root/tests/test_self.py
diff options
context:
space:
mode:
authorMark Byrne <31762852+mbyrnepr2@users.noreply.github.com>2022-10-29 17:45:18 +0200
committerGitHub <noreply@github.com>2022-10-29 17:45:18 +0200
commitdfd0e5aefe2e90b64099b97fe8dc78f813d5b149 (patch)
tree50d33a3d22104bd3fdea956bffd38d71ca7d86ef /tests/test_self.py
parent7bd847008ed5668ff331f95f4770ddce5acd5d17 (diff)
downloadpylint-git-dfd0e5aefe2e90b64099b97fe8dc78f813d5b149.tar.gz
Fix a false negative for ``unused-import`` (#7621)
When linting multiple modules & one module uses an import in a type annotation & the same import is unused in a subsequent module. Closes #4150 Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests/test_self.py')
-rw-r--r--tests/test_self.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/test_self.py b/tests/test_self.py
index 65b76f661..ae3b41d98 100644
--- a/tests/test_self.py
+++ b/tests/test_self.py
@@ -297,6 +297,39 @@ class TestRunTC:
actual_output = actual_output[actual_output.find("\n") :]
assert self._clean_paths(expected_output.strip()) == actual_output.strip()
+ def test_type_annotation_names(self) -> None:
+ """Test resetting the `_type_annotation_names` list to `[]` when leaving a module.
+
+ An import inside `module_a`, which is used as a type annotation in `module_a`, should not prevent
+ emitting the `unused-import` message when the same import occurs in `module_b` & is unused.
+ See: https://github.com/PyCQA/pylint/issues/4150
+ """
+ module1 = join(
+ HERE, "regrtest_data", "imported_module_in_typehint", "module_a.py"
+ )
+
+ module2 = join(
+ HERE, "regrtest_data", "imported_module_in_typehint", "module_b.py"
+ )
+ expected_output = textwrap.dedent(
+ f"""
+ ************* Module module_b
+ {module2}:1:0: W0611: Unused import uuid (unused-import)
+ """
+ )
+ args = [
+ module1,
+ module2,
+ "--disable=all",
+ "--enable=unused-import",
+ "-rn",
+ "-sn",
+ ]
+ out = StringIO()
+ self._run_pylint(args, out=out)
+ actual_output = self._clean_paths(out.getvalue().strip())
+ assert self._clean_paths(expected_output.strip()) in actual_output.strip()
+
def test_import_itself_not_accounted_for_relative_imports(self) -> None:
expected = "Your code has been rated at 10.00/10"
package = join(HERE, "regrtest_data", "dummy")