diff options
author | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2023-03-22 08:31:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-22 08:31:58 +0100 |
commit | 7d26dcf3ee9a0c033f1529d09fe1f600b527c1cc (patch) | |
tree | 9185b9589588d9cd950d34af4536b7b0f7ecf716 | |
parent | d6f3ae8d72e65e89560403ca2241ec604b418bdf (diff) | |
download | pylint-git-7d26dcf3ee9a0c033f1529d09fe1f600b527c1cc.tar.gz |
Fix incorrect preferred-modules matches (#8481)
Co-authored-by: d33bs <dave.bunten@cuanschutz.edu>
(cherry picked from commit d64c0cc1484b0cbea5fd3a9dfac4c0d6ddc7d1aa)
Co-authored-by: Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>
-rw-r--r-- | doc/whatsnew/fragments/8453.bugfix | 3 | ||||
-rw-r--r-- | pylint/checkers/imports.py | 10 | ||||
-rw-r--r-- | tests/checkers/unittest_imports.py | 17 |
3 files changed, 29 insertions, 1 deletions
diff --git a/doc/whatsnew/fragments/8453.bugfix b/doc/whatsnew/fragments/8453.bugfix new file mode 100644 index 000000000..6bec2b0b9 --- /dev/null +++ b/doc/whatsnew/fragments/8453.bugfix @@ -0,0 +1,3 @@ +Fix a regression of ``preferred-modules`` where a partial match was used instead of the required full match. + +Closes #8453 diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py index dee12e591..1ed70d958 100644 --- a/pylint/checkers/imports.py +++ b/pylint/checkers/imports.py @@ -922,7 +922,15 @@ class ImportsChecker(DeprecatedMixin, BaseChecker): mod_compare = [f"{node.modname}.{name[0]}" for name in node.names] # find whether there are matches with the import vs preferred_modules keys - matches = [k for k in self.preferred_modules for mod in mod_compare if k in mod] + matches = [ + k + for k in self.preferred_modules + for mod in mod_compare + # exact match + if k == mod + # checks for base module matches + or k in mod.split(".")[0] + ] # if we have matches, add message if matches: diff --git a/tests/checkers/unittest_imports.py b/tests/checkers/unittest_imports.py index 7544e3422..d454c8161 100644 --- a/tests/checkers/unittest_imports.py +++ b/tests/checkers/unittest_imports.py @@ -192,6 +192,23 @@ class TestImportsChecker(CheckerTestCase): # assert there were no errors assert len(errors) == 0 + # Test for challenges with preferred modules indefinite matches + Run( + [ + f"{os.path.join(REGR_DATA, 'preferred_module/unpreferred_submodule.py')}", + "-d all", + "-e preferred-module", + # prefer pathlib instead of random (testing to avoid regression) + # pathlib shouldn't match with path, which is in the test file + "--preferred-modules=random:pathlib", + ], + exit=False, + ) + _, errors = capsys.readouterr() + + # Assert there were no errors + assert len(errors) == 0 + @staticmethod def test_allow_reexport_package(capsys: CaptureFixture[str]) -> None: """Test --allow-reexport-from-package option.""" |