diff options
| author | Anthony Sottile <asottile@umich.edu> | 2019-11-28 17:30:52 -0800 |
|---|---|---|
| committer | Anthony Sottile <asottile@umich.edu> | 2019-11-28 17:30:52 -0800 |
| commit | d3c95f00d0ac14adcceb423173ff7106aa42d116 (patch) | |
| tree | b38f7fe434ac2adffaf171a92b0c13082af0f0d9 /src/flake8 | |
| parent | 15de413f9e04a9fe360d6a22a66ba163d21a7cf4 (diff) | |
| download | flake8-d3c95f00d0ac14adcceb423173ff7106aa42d116.tar.gz | |
Switch from entrypoints to importlib_metadata
Diffstat (limited to 'src/flake8')
| -rw-r--r-- | src/flake8/_compat.py | 14 | ||||
| -rw-r--r-- | src/flake8/exceptions.py | 15 | ||||
| -rw-r--r-- | src/flake8/main/application.py | 4 | ||||
| -rw-r--r-- | src/flake8/main/debug.py | 7 | ||||
| -rw-r--r-- | src/flake8/plugins/manager.py | 14 | ||||
| -rw-r--r-- | src/flake8/style_guide.py | 8 |
6 files changed, 33 insertions, 29 deletions
diff --git a/src/flake8/_compat.py b/src/flake8/_compat.py new file mode 100644 index 0000000..85af0a3 --- /dev/null +++ b/src/flake8/_compat.py @@ -0,0 +1,14 @@ +"""Expose backports in a single place.""" +import sys + +if sys.version_info >= (3,): # pragma: no cover (PY3+) + from functools import lru_cache +else: # pragma: no cover (<PY3) + from functools32 import lru_cache + +if sys.version_info >= (3, 8): # pragma: no cover (PY38+) + import importlib.metadata as importlib_metadata +else: # pragma: no cover (<PY38) + import importlib_metadata + +__all__ = ("lru_cache", "importlib_metadata") diff --git a/src/flake8/exceptions.py b/src/flake8/exceptions.py index 53ca4b7..bef6f4b 100644 --- a/src/flake8/exceptions.py +++ b/src/flake8/exceptions.py @@ -1,10 +1,6 @@ """Exception classes for all of Flake8.""" - from typing import Dict -if False: # `typing.TYPE_CHECKING` was introduced in 3.5.2 - from flake8.plugins.manager import Plugin - class Flake8Exception(Exception): """Plain Flake8 exception.""" @@ -23,18 +19,17 @@ class FailedToLoadPlugin(Flake8Exception): FORMAT = 'Flake8 failed to load plugin "%(name)s" due to %(exc)s.' - def __init__(self, plugin, exception): - # type: (Plugin, Exception) -> None + def __init__(self, plugin_name, exception): + # type: (str, Exception) -> None """Initialize our FailedToLoadPlugin exception.""" - self.plugin = plugin - self.ep_name = self.plugin.name + self.plugin_name = plugin_name self.original_exception = exception - super(FailedToLoadPlugin, self).__init__(plugin, exception) + super(FailedToLoadPlugin, self).__init__(plugin_name, exception) def __str__(self): # type: () -> str """Format our exception message.""" return self.FORMAT % { - "name": self.ep_name, + "name": self.plugin_name, "exc": self.original_exception, } diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 42e365f..a20c1d4 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -168,9 +168,7 @@ class Application(object): sys.path.extend(local_plugins.paths) - self.check_plugins = plugin_manager.Checkers( - local_plugins.extension - ) + self.check_plugins = plugin_manager.Checkers(local_plugins.extension) self.formatting_plugins = plugin_manager.ReportFormatters( local_plugins.report diff --git a/src/flake8/main/debug.py b/src/flake8/main/debug.py index 1d56189..83a5a11 100644 --- a/src/flake8/main/debug.py +++ b/src/flake8/main/debug.py @@ -4,8 +4,7 @@ from __future__ import print_function import argparse import json import platform - -import entrypoints +from typing import Dict, List class DebugAction(argparse.Action): @@ -61,6 +60,6 @@ def plugins_from(option_manager): ] -def dependencies(): +def dependencies(): # type: () -> List[Dict[str, str]] """Generate the list of dependencies we care about.""" - return [{"dependency": "entrypoints", "version": entrypoints.__version__}] + return [] diff --git a/src/flake8/plugins/manager.py b/src/flake8/plugins/manager.py index 75cc1ab..903a130 100644 --- a/src/flake8/plugins/manager.py +++ b/src/flake8/plugins/manager.py @@ -2,10 +2,9 @@ import logging from typing import Any, Dict, List, Optional, Set -import entrypoints - from flake8 import exceptions from flake8 import utils +from flake8._compat import importlib_metadata LOG = logging.getLogger(__name__) @@ -159,7 +158,7 @@ class Plugin(object): except Exception as load_exception: LOG.exception(load_exception) failed_to_load = exceptions.FailedToLoadPlugin( - plugin=self, exception=load_exception + plugin_name=self.name, exception=load_exception ) LOG.critical(str(failed_to_load)) raise failed_to_load @@ -224,6 +223,7 @@ class PluginManager(object): # pylint: disable=too-few-public-methods """Find and manage plugins consistently.""" def __init__(self, namespace, local_plugins=None): + # type: (str, Optional[List[str]]) -> None """Initialize the manager. :param str namespace: @@ -246,12 +246,16 @@ class PluginManager(object): # pylint: disable=too-few-public-methods for plugin_str in local_plugins: name, _, entry_str = plugin_str.partition("=") name, entry_str = name.strip(), entry_str.strip() - entry_point = entrypoints.EntryPoint.from_string(entry_str, name) + entry_point = importlib_metadata.EntryPoint(name, entry_str, None) self._load_plugin_from_entrypoint(entry_point, local=True) def _load_entrypoint_plugins(self): LOG.info('Loading entry-points for "%s".', self.namespace) - for entry_point in entrypoints.get_group_all(self.namespace): + eps = importlib_metadata.entry_points().get(self.namespace, ()) + # python2.7 occasionally gives duplicate results due to redundant + # `local/lib` -> `../lib` symlink on linux in virtualenvs so we + # eliminate duplicates here + for entry_point in sorted(frozenset(eps)): if entry_point.name == "per-file-ignores": LOG.warning( "flake8-per-file-ignores plugin is incompatible with " diff --git a/src/flake8/style_guide.py b/src/flake8/style_guide.py index ef9c6e9..9be0fe8 100644 --- a/src/flake8/style_guide.py +++ b/src/flake8/style_guide.py @@ -7,13 +7,13 @@ import enum import itertools import linecache import logging -import sys from typing import Dict, Generator, List, Match, Optional, Sequence, Set from typing import Tuple, Union from flake8 import defaults from flake8 import statistics from flake8 import utils +from flake8._compat import lru_cache from flake8.formatting import base as base_formatter __all__ = ("StyleGuide",) @@ -21,12 +21,6 @@ __all__ = ("StyleGuide",) LOG = logging.getLogger(__name__) -if sys.version_info < (3, 2): - from functools32 import lru_cache -else: - from functools import lru_cache - - class Selected(enum.Enum): """Enum representing an explicitly or implicitly selected code.""" |
