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.
  • Loading branch information
tari committed Jul 31, 2024
1 parent ba6be8c commit 60c1839
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 16 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: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
install_requires=[
# BEGIN requirements
"Django>=4.2",
"setuptools",
"requests",
# END requirements
],
Expand Down
14 changes: 3 additions & 11 deletions tests/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@ 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."""
try:
import pkg_resources
except ImportError:
self.fail(
"Cannot import pkg_resources module. It is part of "
"setuptools, which is a dependency of "
"django_downloadview."
)
distribution = pkg_resources.get_distribution("django-downloadview")
"""django_downloadview.__version__ matches importlib metadata."""
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 60c1839

Please sign in to comment.