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 deprecated pkg_resources #281

Merged
merged 7 commits into from
Jul 13, 2024
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

---
## [UnRaleased] - 2024-MM-DD

### Changed

- Replaced deprecated `pkg_resources` with `importlib.metadata` ([#281](https://github.com/Lightning-AI/utilities/pull/281))

### Fixed

-

## [0.11.3] - 2024-06-26

Expand Down
31 changes: 16 additions & 15 deletions src/lightning_utilities/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import os
import warnings
from functools import lru_cache
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as _version
from importlib.util import find_spec
from types import ModuleType
from typing import Any, Callable, List, Optional, TypeVar

import pkg_resources
from packaging.requirements import Requirement
from packaging.version import Version
from packaging.version import InvalidVersion, Version
from typing_extensions import ParamSpec

T = TypeVar("T")
Expand Down Expand Up @@ -75,15 +76,12 @@ def compare_version(package: str, op: Callable, version: str, use_base_version:
"""
try:
pkg = importlib.import_module(package)
except (ImportError, pkg_resources.DistributionNotFound):
except ImportError:
return False
try:
if hasattr(pkg, "__version__"):
pkg_version = Version(pkg.__version__)
else:
# try pkg_resources to infer version
pkg_version = Version(pkg_resources.get_distribution(package).version)
except TypeError:
# Use importlib.metadata to infer version
pkg_version = Version(pkg.__version__) if hasattr(pkg, "__version__") else Version(_version(package))
except (TypeError, PackageNotFoundError):
# this is mocked by Sphinx, so it should return True to generate all summaries
return True
if use_base_version:
Expand Down Expand Up @@ -128,17 +126,20 @@ def __init__(self, requirement: Optional[str] = None, module: Optional[str] = No
def _check_requirement(self) -> None:
assert self.requirement # noqa: S101; needed for typing
try:
# first try the pkg_resources requirement
pkg_resources.require(self.requirement)
self.available = True
self.message = f"Requirement {self.requirement!r} met"
except Exception as ex:
req = Requirement(self.requirement)
pkg_version = Version(_version(req.name))
self.available = req.specifier.contains(pkg_version)
except (PackageNotFoundError, InvalidVersion) as ex:
self.available = False
self.message = f"{ex.__class__.__name__}: {ex}. HINT: Try running `pip install -U {self.requirement!r}`"

if self.available:
self.message = f"Requirement {self.requirement!r} met"
else:
req_include_version = any(c in self.requirement for c in "=<>")
if not req_include_version or self.module is not None:
module = self.requirement if self.module is None else self.module
# sometimes `pkg_resources.require()` fails but the module is importable
# Sometimes `importlib.metadata.version` fails but the module is importable
self.available = module_available(module)
if self.available:
self.message = f"Module {module!r} available"
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/core/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def testcompare_version(monkeypatch):
def test_requirement_cache():
assert RequirementCache(f"pytest>={pytest.__version__}")
assert not RequirementCache(f"pytest<{pytest.__version__}")
assert "pip install -U '-'" in str(RequirementCache("-"))
assert "pip install -U 'not-found-requirement'" in str(RequirementCache("not-found-requirement"))

# invalid requirement is skipped by valid module
assert RequirementCache(f"pytest<{pytest.__version__}", "pytest")
Expand Down
Loading