Skip to content

Commit

Permalink
Replace use of pkg_resources (setuptools)
Browse files Browse the repository at this point in the history
Since Python 3.12, setuptools isn't included with Python
and importlib is the recommended replacement, available
since Python 3.8. To support back to Python 3.7, this
adds a dependency on importlib_metadata which provides
a backport of importlib.metadata via PyPI.
  • Loading branch information
tari committed Jul 30, 2024
1 parent 338e171 commit 312ecc0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions django_downloadview/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Serve files with Django and reverse proxies."""
from django_downloadview.api import * # NoQA

import pkg_resources
import importlib_metadata

#: Module version, as defined in PEP-0396.
__version__ = pkg_resources.get_distribution(__package__.replace("-", "_")).version
__version__ = importlib_metadata.version(__package__.replace("-", "_"))
5 changes: 3 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"""django-downloadview documentation build configuration file."""
import os
import re
from pkg_resources import get_distribution

import importlib_metadata

# Minimal Django settings. Required to use sphinx.ext.autodoc, because
# django-downloadview depends on Django...
Expand Down Expand Up @@ -48,7 +49,7 @@
# built documents.

# The full version, including alpha/beta/rc tags.
release = get_distribution("django-downloadview").version
release = importlib_metadata.version("django-downloadview")
# The short X.Y version.
version = '.'.join(release.split('.')[:2])

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
install_requires=[
# BEGIN requirements
"Django>=2.2",
"importlib_metadata",
"requests",
# END requirements
],
Expand Down
13 changes: 6 additions & 7 deletions tests/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@ def test_version_present(self):
self.fail("django_downloadview package has no __version__.")

def test_version_match(self):
"""django_downloadview.__version__ matches pkg_resources info."""
"""django_downloadview.__version__ matches importlib metadata."""
try:
import pkg_resources
import importlib_metadata
except ImportError:
self.fail(
"Cannot import pkg_resources module. It is part of "
"setuptools, which is a dependency of "
"django_downloadview."
"Cannot import importlib_metadata module. It is a "
"dependency of django_downloadview."
)
distribution = pkg_resources.get_distribution("django-downloadview")
distribution = importlib_metadata.distribution("django-downloadview")
installed_version = distribution.version
self.assertEqual(
installed_version,
self.get_version(),
"Version mismatch: django_downloadview.__version__ "
'is "%s" whereas pkg_resources tells "%s". '
'is "%s" whereas importlib_metadata tells "%s". '
"You may need to run ``make develop`` to update the "
"installed version in development environment."
% (self.get_version(), installed_version),
Expand Down

0 comments on commit 312ecc0

Please sign in to comment.