Replace pkg_resources with importlib_metadata (#11061)

* Replace pkg_resources with importlib_metadata

Fixes #10964. The other reason for this change is that pkg_resources has been [deprecated](8fe85c22ce) in favor of importlib_metadata.

* Reduce to a single importlib_metadata import switch

* Trigger CI

Co-authored-by: Stas Bekman <stas@stason.org>
This commit is contained in:
konstin
2021-04-05 21:12:19 +02:00
committed by GitHub
parent b51b87c41d
commit abb7430003
3 changed files with 25 additions and 20 deletions

View File

@@ -16,9 +16,13 @@ import sys
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__
@@ -57,7 +61,7 @@ class DependencyVersionCheckTest(TestCasePlus):
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))
@@ -65,7 +69,7 @@ class DependencyVersionCheckTest(TestCasePlus):
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))
@@ -87,7 +91,7 @@ class DependencyVersionCheckTest(TestCasePlus):
# 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))
@@ -100,6 +104,6 @@ class DependencyVersionCheckTest(TestCasePlus):
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))