Skip to content

Commit

Permalink
importlib.metadata is great
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jun 27, 2023
1 parent 71f75e6 commit 3f0a136
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 47 deletions.
6 changes: 0 additions & 6 deletions hypothesis-python/docs/strategies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ and then tell ``setuptools`` that this is your ``"hypothesis"`` entry point:
And that's all it takes!

.. note::
On Python 3.7, where the ``importlib.metadata`` module
is not in the standard library, loading entry points requires either the
:pypi:`importlib_metadata` (preferred) or :pypi:`setuptools` (fallback)
package to be installed.


Interaction with :pypi:`pytest-cov`
-----------------------------------
Expand Down
4 changes: 1 addition & 3 deletions hypothesis-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ def local_file(name):
"django": ["django>=3.2"],
}

extras["all"] = sorted(
set(sum(extras.values(), ["importlib_metadata>=3.6; python_version<'3.8'"]))
)
extras["all"] = sorted(set(sum(extras.values(), [])))


setuptools.setup(
Expand Down
46 changes: 8 additions & 38 deletions hypothesis-python/src/hypothesis/entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,17 @@
your package.
"""

try:
# We prefer to use importlib.metadata, or the backport on Python <= 3.7,
# because it's much faster than pkg_resources (200ms import time speedup).
try:
from importlib import metadata as importlib_metadata
except ImportError:
import importlib_metadata # type: ignore # mypy thinks this is a redefinition
import importlib.metadata

def get_entry_points():
try:
eps = importlib_metadata.entry_points(group="hypothesis")
except TypeError: # pragma: no cover
# Load-time selection requires Python >= 3.10 or importlib_metadata >= 3.6,
# so we'll retain this fallback logic for some time to come. See also
# https://importlib-metadata.readthedocs.io/en/latest/using.html
eps = importlib_metadata.entry_points().get("hypothesis", [])
yield from eps

except ImportError:
# But if we're not on Python >= 3.8 and the importlib_metadata backport
# is not installed, we fall back to pkg_resources anyway.
def get_entry_points():
try:
import pkg_resources
except ImportError:
import warnings

from hypothesis.errors import HypothesisWarning

warnings.warn(
"Under Python <= 3.7, Hypothesis requires either the importlib_metadata "
"or setuptools package in order to load plugins via entrypoints.",
HypothesisWarning,
)

def get_entry_points():
yield from ()

else:

def get_entry_points():
yield from pkg_resources.iter_entry_points("hypothesis")
eps = importlib.metadata.entry_points(group="hypothesis")
except TypeError: # pragma: no cover
# Load-time selection requires Python >= 3.10. See also
# https://importlib-metadata.readthedocs.io/en/latest/using.html
eps = importlib.metadata.entry_points().get("hypothesis", [])
yield from eps


def run():
Expand Down

0 comments on commit 3f0a136

Please sign in to comment.