Skip to content

Find entry_points with importlib(.|_)metadata, drop setuptools from dependencies #385

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

Merged
merged 1 commit into from
Aug 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions pylsp/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
# pylint: disable=import-outside-toplevel

import logging
import sys
from functools import lru_cache
from typing import List, Mapping, Sequence, Union

import pkg_resources
import pluggy
from pluggy._hooks import HookImpl

from pylsp import _utils, hookspecs, uris, PYLSP

# See compatibility note on `group` keyword:
# https://docs.python.org/3/library/importlib.metadata.html#entry-points
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that from importlib.metadata import entry_points will work even before 3.10.

Reproduced here from this link, emphasis mine:

The “selectable” entry points were introduced in importlib_metadata 3.6 and Python 3.10. Prior to those changes, entry_points accepted no parameters and always returned a dictionary of entry points, keyed by group. For compatibility, if no parameters are passed to entry_points, a SelectableGroups object is returned, implementing that dict interface. In the future, calling entry_points with no parameters will return an EntryPoints object. Users should rely on the selection interface to retrieve entry points by group.

Before 3.10 stdlib, and older importlib_metadata, all of the entry_points in $PREFIX/lib/python-3* are discovered and parsed without any kind of cache. In a large environment, this can take a number of seconds. The venerable entrypoints did this a bit better, but is now deprecated, leaving this half-measure until 3.9 EOL to avoid getting bitten by inconsistent return types and performance cliffs.

if sys.version_info < (3, 10): # pragma: no cover
from importlib_metadata import entry_points
else: # pragma: no cover
from importlib.metadata import entry_points


log = logging.getLogger(__name__)

# Sources of config, first source overrides next source
Expand Down Expand Up @@ -67,14 +75,15 @@ def __init__(self, root_uri, init_opts, process_id, capabilities):
# Pluggy will skip loading a plugin if it throws a DistributionNotFound exception.
# However I don't want all plugins to have to catch ImportError and re-throw. So here we'll filter
# out any entry points that throw ImportError assuming one or more of their dependencies isn't present.
for entry_point in pkg_resources.iter_entry_points(PYLSP):
for entry_point in entry_points(group=PYLSP):
try:
entry_point.load()
except Exception as e: # pylint: disable=broad-except
log.info("Failed to load %s entry point '%s': %s", PYLSP, entry_point.name, e)
self._pm.set_blocked(entry_point.name)

# Load the entry points into pluggy, having blocked any failing ones
# Load the entry points into pluggy, having blocked any failing ones.
# Despite the API name, recent Pluggy versions will use ``importlib_metadata``.
self._pm.load_setuptools_entrypoints(PYLSP)

for name, plugin in self._pm.list_name_plugin():
Expand Down
7 changes: 3 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ readme = "README.md"
license = {text = "MIT"}
requires-python = ">=3.8"
dependencies = [
"docstring-to-markdown",
"importlib_metadata>=4.8.3;python_version<\"3.10\"",
"jedi>=0.17.2,<0.20.0",
"python-lsp-jsonrpc>=1.0.0",
"pluggy>=1.0.0",
"docstring-to-markdown",
"python-lsp-jsonrpc>=1.0.0",
"ujson>=3.0.0",
"setuptools>=39.0.0",
]
dynamic = ["version"]

Expand Down Expand Up @@ -104,4 +104,3 @@ addopts = "--cov-report html --cov-report term --junitxml=pytest.xml --cov pylsp

[tool.coverage.run]
concurrency = ["multiprocessing", "thread"]