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

Replace pkg_resources with importlib.metadata #10007

Merged
merged 5 commits into from Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions setup.py
Expand Up @@ -29,8 +29,8 @@
'alabaster>=0.7,<0.8',
'imagesize',
'requests>=2.5.0',
'setuptools',
'packaging',
"importlib-metadata>=4.4; python_version < '3.10'",
]

extras_require = {
Expand All @@ -47,7 +47,6 @@
'mypy>=0.920',
'docutils-stubs',
"types-typed-ast",
"types-pkg_resources",
"types-requests",
],
'test': [
Expand Down
14 changes: 9 additions & 5 deletions sphinx/registry.py
Expand Up @@ -21,7 +21,11 @@
from docutils.parsers import Parser
from docutils.parsers.rst import Directive
from docutils.transforms import Transform
from pkg_resources import iter_entry_points

try: # Python < 3.10 (backport)
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
from importlib_metadata import entry_points
except ImportError:
from importlib.metadata import entry_points
Comment on lines +25 to +28

Choose a reason for hiding this comment

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

Is there a reason the builtin isn't tried before the backport?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes: the code is using APIs that were changed in 3.10, so for 3.8 and 3.9 we want to use the backport over the standard library module. 🙂

Choose a reason for hiding this comment

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

Got it. Thanks for the context! 😄


from sphinx.builders import Builder
from sphinx.config import Config
Expand Down Expand Up @@ -143,14 +147,14 @@ def preload_builder(self, app: "Sphinx", name: str) -> None:
return

if name not in self.builders:
entry_points = iter_entry_points('sphinx.builders', name)
builder_eps = entry_points(group='sphinx.builders')
tk0miya marked this conversation as resolved.
Show resolved Hide resolved
try:
entry_point = next(entry_points)
except StopIteration as exc:
entry_point = builder_eps[name]
except KeyError as exc:
raise SphinxError(__('Builder name %s not registered or available'
' through entry point') % name) from exc

self.load_extension(app, entry_point.module_name)
self.load_extension(app, entry_point.module)

def create_builder(self, app: "Sphinx", name: str) -> Builder:
if name not in self.builders:
Expand Down
13 changes: 8 additions & 5 deletions sphinx/theming.py
Expand Up @@ -16,7 +16,10 @@
from typing import TYPE_CHECKING, Any, Dict, List
from zipfile import ZipFile

import pkg_resources
try: # Python < 3.10 (backport)
from importlib_metadata import entry_points
except ImportError:
from importlib.metadata import entry_points

from sphinx import package_dir
from sphinx.errors import ThemeError
Expand Down Expand Up @@ -201,12 +204,12 @@ def load_external_theme(self, name: str) -> None:
Sphinx refers to ``sphinx_themes`` entry_points.
"""
# look up for new styled entry_points at first
entry_points = pkg_resources.iter_entry_points('sphinx.html_themes', name)
theme_eps = entry_points(group='sphinx.html_themes')
try:
entry_point = next(entry_points)
self.app.registry.load_extension(self.app, entry_point.module_name)
entry_point = theme_eps[name]
self.app.registry.load_extension(self.app, entry_point.module)
return
except StopIteration:
except KeyError:
pass

def find_themes(self, theme_path: str) -> Dict[str, str]:
Expand Down