Skip to content

Commit c286019

Browse files
avoid the use of pkg_resources when importlib.metadata is available (#799)
* feat: avoid the use of pkg_resources when importlib.metadata, since pkg_resources is removed from Python 3.12 * feat: avoid the use of pkg_resources when importlib.metadata, since pkg_resources is removed from Python 3.12 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: remove flake8 errors * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0dd41a3 commit c286019

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
#
99
# All configuration values have a default; values that are commented out
1010
# serve to show the default.
11-
from pkg_resources import get_distribution
1211

1312
# If extensions (or modules to document with autodoc) are in another directory,
1413
# add these directories to sys.path here. If the directory is relative to the
1514
# documentation root, use os.path.abspath to make it absolute, like shown here.
1615
# sys.path.insert(0, os.path.abspath('.'))
16+
from pipeline import __version__ as pipeline_version
1717

1818
# -- General configuration -----------------------------------------------------
1919

@@ -45,7 +45,7 @@
4545
# built documents.
4646
#
4747
# The full version, including alpha/beta/rc tags.
48-
release = get_distribution("django-pipeline").version
48+
release = pipeline_version
4949
# The short X.Y version.
5050
version = ".".join(release.split(".")[:2])
5151

pipeline/__init__.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
from pkg_resources import DistributionNotFound, get_distribution
2-
1+
PackageNotFoundError = None
2+
DistributionNotFound = None
33
try:
4-
__version__ = get_distribution("django-pipeline").version
5-
except DistributionNotFound:
6-
# package is not installed
7-
__version__ = None
4+
from importlib.metadata import PackageNotFoundError
5+
from importlib.metadata import version as get_version
6+
except ImportError:
7+
get_version = None
8+
PackageNotFoundError = None
9+
if get_version is None:
10+
try:
11+
from pkg_resources import DistributionNotFound, get_distribution
12+
13+
def get_version(x):
14+
return get_distribution(x).version
15+
16+
except ImportError:
17+
get_version = None
18+
DistributionNotFound = None
19+
get_distribution = None
20+
21+
__version__ = None
22+
if get_version is not None:
23+
try:
24+
__version__ = get_version("django-pipeline")
25+
except PackageNotFoundError:
26+
pass
27+
except DistributionNotFound:
28+
pass

0 commit comments

Comments
 (0)