Skip to content

Commit

Permalink
from importlib.metadata import version for Python >= 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Oct 15, 2024
1 parent faa3f86 commit 0bb1cc3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
11 changes: 9 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
from docutils import nodes, utils
from docutils.parsers.rst import roles
from docutils.parsers.rst.roles import set_classes
from pkg_resources import get_distribution

try:
from importlib import metadata
except ImportError: # Python < 3.8
from pkg_resources import get_distribution

# -- General configuration ------------------------------------------------

Expand Down Expand Up @@ -47,7 +51,10 @@
# built documents.

# The full version, including alpha/beta/rc tags.
release = get_distribution('drf_yasg').version
try:
release = metadata.version('drf_yasg')
except NameError: # Python < 3.8
release = get_distribution('drf_yasg').version
if 'noscm' in release:
raise AssertionError('Invalid package version string: %s. \n'
'The documentation must be built with drf_yasg installed from a distribution package, '
Expand Down
14 changes: 9 additions & 5 deletions src/drf_yasg/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# coding=utf-8
from pkg_resources import DistributionNotFound, get_distribution

__author__ = """Cristi V."""
__email__ = '[email protected]'

try:
__version__ = get_distribution(__name__).version
except DistributionNotFound: # pragma: no cover
# package is not installed
pass
from importlib.metadata import version
__version__ = version(__name__)
except ImportError: # Python < 3.8
try:
from pkg_resources import DistributionNotFound, get_distribution
__version__ = get_distribution(__name__).version
except DistributionNotFound: # pragma: no cover
# package is not installed
pass
8 changes: 6 additions & 2 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import typing
from django.core import validators
from django.db import models
from packaging import version
from rest_framework import serializers
from rest_framework.settings import api_settings as rest_framework_settings

Expand All @@ -23,7 +22,12 @@
decimal_as_float, field_value_to_representation, filter_none, get_serializer_class, get_serializer_ref_name
)

drf_version = pkg_resources.get_distribution("djangorestframework").version
try:
from importlib import metadata
drf_version = metadata.version("djangorestframework")
except ImportError: # Python < 3.8
from packaging import version
drf_version = pkg_resources.get_distribution("djangorestframework").version

logger = logging.getLogger(__name__)

Expand Down

0 comments on commit 0bb1cc3

Please sign in to comment.