Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent duplicate plugin discovery on misconfigured pythons #1631

Merged
merged 1 commit into from Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/flake8/plugins/finder.py
Expand Up @@ -179,6 +179,8 @@ def _flake8_plugins(


def _find_importlib_plugins() -> Generator[Plugin, None, None]:
# some misconfigured pythons (RHEL) have things on `sys.path` twice
seen = set()
for dist in importlib_metadata.distributions():
# assigned to prevent continual reparsing
eps = dist.entry_points
Expand All @@ -190,6 +192,11 @@ def _find_importlib_plugins() -> Generator[Plugin, None, None]:
# assigned to prevent continual reparsing
meta = dist.metadata

if meta["name"] in seen:
continue
else:
seen.add(meta["name"])

if meta["name"] in BANNED_PLUGINS:
LOG.warning(
"%s plugin is obsolete in flake8>=%s",
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/plugins/finder_test.py
Expand Up @@ -361,6 +361,23 @@ def test_importlib_plugins(
]


def test_duplicate_dists(flake8_dist):
# some poorly packaged pythons put lib and lib64 on sys.path resulting in
# duplicates from `importlib.metadata.distributions`
with mock.patch.object(
importlib_metadata,
"distributions",
return_value=[
flake8_dist,
flake8_dist,
],
):
ret = list(finder._find_importlib_plugins())

# we should not have duplicates
assert len(ret) == len(set(ret))


def test_find_local_plugins_nothing():
cfg = configparser.RawConfigParser()
assert set(finder._find_local_plugins(cfg)) == set()
Expand Down