Skip to content

Commit

Permalink
scuba: Use importlib.metadata over pkg_resources for getting version
Browse files Browse the repository at this point in the history
pkg_resources is deprecated:
https://setuptools.pypa.io/en/latest/pkg_resources.html

This prefers importlib.metadata (introduced in Python 3.8) and uses the
backport importlib_metadata for Python 3.7 (to be removed in #242).

Fixes #245
  • Loading branch information
JonathonReinhart committed Jan 3, 2024
1 parent 3dc54d6 commit c5c5233
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Changed
- Removed use of deprecated `pkg_resources` (#247)


## [2.12.0] - 2023-09-15
### Added
- Enable the use of relative paths in a volume hostpath (#227)
Expand Down
3 changes: 2 additions & 1 deletion VERSIONING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ versioning scheme. Consider a base version of `1.2.3`:
created via `python setup.py sdist`. In this case, the version comes from
the egg info generated by setuptools.

In any case, when Scuba is installed, the version comes from `pkg_resources`.
In any case, when Scuba is installed, the version comes from
`importlib.metadata`.
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ classifiers = [
]
dependencies = [
"PyYAML",
# TODO(#242): Remove when Python 3.7 support is removed.
"importlib_metadata; python_version<'3.8'",
]
description = "Simplify use of Docker containers for building software"
keywords = ["docker"]
Expand All @@ -42,6 +44,8 @@ changelog = "https://github.com/JonathonReinhart/scuba/blob/main/CHANGELOG.md"
requires = [
"wheel",
"setuptools >= 42.0.0",
# TODO(#242): Remove when Python 3.7 support is removed.
"importlib_metadata; python_version<'3.8'",
]


Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ PyYAML
pytest
pytest-cov
types-PyYAML
types-pkg-resources
coverage
black~=23.0 # Must match .github/workflows/black.yml
-r docs/requirements.txt
16 changes: 7 additions & 9 deletions scuba/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,17 @@ def get_version() -> str:
if not git_archive_rev.startswith("$"):
return f"{BASE_VERSION}+g{git_archive_rev}"

# Package resource
# Otherwise, we're either installed (e.g. via pip), or running from
# an 'sdist' source distribution, and have a local PKG_INFO file.
import pkg_resources

try:
return pkg_resources.get_distribution(DIST_SPEC).version
except pkg_resources.DistributionNotFound:
pass
# TODO(#242): Remove backport when Python 3.7 support is removed.
if sys.version_info >= (3, 8):
import importlib.metadata as importlib_metadata
else:
import importlib_metadata # backport

# This shouldn't be able to happen
sys.stderr.write("WARNING: Failed to determine version!\n")
return BASE_VERSION
# Can raise importlib.metadata.PackageNotFoundError
return importlib_metadata.version(DIST_SPEC)


__version__ = get_version()
Expand Down

0 comments on commit c5c5233

Please sign in to comment.