Skip to content

Commit

Permalink
Merge pull request #1631 from PyCQA/dupe-sys-path
Browse files Browse the repository at this point in the history
prevent duplicate plugin discovery on misconfigured pythons
  • Loading branch information
asottile committed Jul 31, 2022
2 parents 3f4872a + fce93b9 commit d20bb97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
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

0 comments on commit d20bb97

Please sign in to comment.