Skip to content

Commit

Permalink
handle duplicate entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Feb 9, 2021
1 parent f813bcb commit a6d3d57
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
2 changes: 0 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ tasks:
- "{{.MYPY_ENV}}/bin/mypy --ignore-missing-imports --allow-redefinition deal/"

pytest:run:
sources:
- deal/
deps:
- task: flit:install
vars:
Expand Down
45 changes: 27 additions & 18 deletions flake8_codes/_plugins/_discover.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# built-in
import re
from collections import defaultdict
from typing import DefaultDict, Iterable, Iterator, List, NamedTuple, Tuple
from typing import DefaultDict, Dict, Iterable, Iterator, List, NamedTuple, Tuple

from flake8.main.application import Application
from flake8.plugins.manager import Plugin as Flake8Plugin

# app
from ._plugin import get_plugin_name

try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata


REX_CODE = re.compile(r'^[A-Z]{1,5}[0-9]{0,5}$')

Expand All @@ -21,6 +29,7 @@
'pycodestyle': ('W', 'E'),
'pylint': ('C', 'E', 'F', 'I', 'R', 'W'),
}
NAMESPACE = 'flake8.extension'


class Plugin(NamedTuple):
Expand All @@ -30,30 +39,30 @@ class Plugin(NamedTuple):
version: str


def get_installed(app) -> Iterator[Plugin]:
def get_installed(app: Application) -> Iterator[Plugin]:
plugins_codes: DefaultDict[Tuple[str, str], List[str]]
plugins_codes = defaultdict(list)
versions = dict()

app.initialize([])
codes: Iterable[str]

for check_type in ('ast_plugins', 'logical_line_plugins', 'physical_line_plugins'):
for plugin in getattr(app.check_plugins, check_type):
key = (check_type, get_plugin_name(plugin.to_dictionary()))
versions[key[-1]] = plugin.version
entry_points = importlib_metadata.entry_points()[NAMESPACE]
for entry_point in entry_points:
plugin = Flake8Plugin(entry_point.name, entry_point, local=False)
check_type = plugin.parameter_names[0]
key = (check_type, get_plugin_name(plugin.to_dictionary()))
versions[key[-1]] = plugin.version

# if codes for plugin specified explicitly in ALIASES, use it
codes = ALIASES.get(plugin.plugin_name, [])
if codes:
plugins_codes[key] = list(codes)
continue
# if codes for plugin specified explicitly in ALIASES, use it
codes = ALIASES.get(plugin.plugin_name, [])
if codes:
plugins_codes[key] = list(codes)
continue

# otherwise get codes from plugin entrypoint
code = plugin.name
if not REX_CODE.match(code):
raise ValueError('Invalid code format: {}'.format(code))
plugins_codes[key].append(code)
# otherwise get codes from plugin entrypoint
code = plugin.name
if not REX_CODE.match(code):
raise ValueError('Invalid code format: {}'.format(code))
plugins_codes[key].append(code)

if 'flake8-docstrings' in versions:
versions['flake8-docstrings'] = versions['flake8-docstrings'].split(',')[0]
Expand Down

0 comments on commit a6d3d57

Please sign in to comment.