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 #11061

Merged
merged 3 commits into from
Apr 5, 2021
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
8 changes: 1 addition & 7 deletions src/transformers/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@

import requests
from filelock import FileLock
from transformers.utils.versions import importlib_metadata

from . import __version__
from .hf_api import HfFolder
from .utils import logging


# The package importlib_metadata is in a different place, depending on the python version.
if sys.version_info < (3, 8):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata


logger = logging.get_logger(__name__) # pylint: disable=invalid-name

ENV_VARS_TRUE_VALUES = {"1", "ON", "YES", "TRUE"}
Expand Down
21 changes: 14 additions & 7 deletions src/transformers/utils/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

from packaging import version

import pkg_resources

# The package importlib_metadata is in a different place, depending on the python version.
if sys.version_info < (3, 8):
import importlib_metadata
else:
import importlib.metadata as importlib_metadata


ops = {
Expand All @@ -39,7 +44,7 @@ def require_version(requirement: str, hint: Optional[str] = None) -> None:
"""
Perform a runtime check of the dependency versions, using the exact same syntax used by pip.

The installed module version comes from the `site-packages` dir via `pkg_resources`.
The installed module version comes from the `site-packages` dir via `importlib_metadata`.

Args:
requirement (:obj:`str`): pip style definition, e.g., "tokenizers==0.9.4", "tqdm>=4.27", "numpy"
Expand Down Expand Up @@ -70,20 +75,22 @@ def require_version(requirement: str, hint: Optional[str] = None) -> None:
if pkg == "python":
got_ver = ".".join([str(x) for x in sys.version_info[:3]])
if not ops[op](version.parse(got_ver), version.parse(want_ver)):
raise pkg_resources.VersionConflict(
raise ImportError(
f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}."
)
return

# check if any version is installed
try:
got_ver = pkg_resources.get_distribution(pkg).version
except pkg_resources.DistributionNotFound:
raise pkg_resources.DistributionNotFound(requirement, ["this application", hint])
got_ver = importlib_metadata.version(pkg)
except importlib_metadata.PackageNotFoundError:
raise importlib_metadata.PackageNotFoundError(
f"The '{requirement}' distribution was not found and is required by this application. {hint}"
)

# check that the right version is installed if version number was provided
if want_ver is not None and not ops[op](version.parse(got_ver), version.parse(want_ver)):
raise pkg_resources.VersionConflict(
raise ImportError(
f"{requirement} is required for a normal functioning of this module, but found {pkg}=={got_ver}.{hint}"
)

Expand Down
16 changes: 10 additions & 6 deletions tests/test_versions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

import numpy

import pkg_resources
from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import require_version, require_version_core, require_version_examples
from transformers.utils.versions import (
importlib_metadata,
require_version,
require_version_core,
require_version_examples,
)


numpy_ver = numpy.__version__
Expand Down Expand Up @@ -57,15 +61,15 @@ def test_core(self):
for req in ["numpy==1.0.0", "numpy>=1000.0.0", f"numpy<{numpy_ver}"]:
try:
require_version_core(req)
except pkg_resources.VersionConflict as e:
except ImportError as e:
self.assertIn(f"{req} is required", str(e))
self.assertIn("but found", str(e))

# unmet requirements due to missing module
for req in ["numpipypie>1", "numpipypie2"]:
try:
require_version_core(req)
except pkg_resources.DistributionNotFound as e:
except importlib_metadata.PackageNotFoundError as e:
self.assertIn(f"The '{req}' distribution was not found and is required by this application", str(e))
self.assertIn("Try: pip install transformers -U", str(e))

Expand All @@ -87,7 +91,7 @@ def test_examples(self):
# the main functionality is tested in `test_core`, this is just the hint check
try:
require_version_examples("numpy>1000.4.5")
except pkg_resources.VersionConflict as e:
except ImportError as e:
self.assertIn("is required", str(e))
self.assertIn("pip install -r examples/requirements.txt", str(e))

Expand All @@ -100,6 +104,6 @@ def test_python(self):
for req in ["python>9.9.9", "python<3.0.0"]:
try:
require_version_core(req)
except pkg_resources.VersionConflict as e:
except ImportError as e:
self.assertIn(f"{req} is required", str(e))
self.assertIn(f"but found python=={python_ver}", str(e))