From 55ed7ab7a4ff7dae028ad8cf10f3168352a6f19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Wed, 24 Jul 2024 18:40:39 +0200 Subject: [PATCH] DEP: drop importlib_metadata as a dependency on Python 3.12 and newer --- CHANGES.rst | 2 ++ asdf/_entry_points.py | 13 +++++++++---- asdf/_tests/test_entry_points.py | 18 ++++++++++++------ asdf/util.py | 11 ++++++++--- docs/conf.py | 9 +++++++-- pyproject.toml | 2 +- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index d1af1b8c3..74ce0414b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,8 @@ this inheritance may be dropped in a future asdf version. Please migrate to the new ``AsdfSerializationError``. [#1809] +- Drop ``importlib_metadata`` as a dependency on Python 3.12 and newer [#1810] + 3.3.0 (2024-07-12) ------------------ diff --git a/asdf/_entry_points.py b/asdf/_entry_points.py index f8d73283a..7c37c82ce 100644 --- a/asdf/_entry_points.py +++ b/asdf/_entry_points.py @@ -1,15 +1,20 @@ +import sys import warnings +from .exceptions import AsdfWarning +from .extension import ExtensionProxy +from .resource import ResourceMappingProxy + # The standard library importlib.metadata returns duplicate entrypoints # for all python versions up to and including 3.11 # https://github.com/python/importlib_metadata/issues/410#issuecomment-1304258228 # see PR https://github.com/asdf-format/asdf/pull/1260 # see issue https://github.com/asdf-format/asdf/issues/1254 -from importlib_metadata import entry_points +if sys.version_info >= (3, 12): + from importlib.metadata import entry_points +else: + from importlib_metadata import entry_points -from .exceptions import AsdfWarning -from .extension import ExtensionProxy -from .resource import ResourceMappingProxy RESOURCE_MAPPINGS_GROUP = "asdf.resource_mappings" EXTENSIONS_GROUP = "asdf.extensions" diff --git a/asdf/_tests/test_entry_points.py b/asdf/_tests/test_entry_points.py index d53c85afc..cc047f0f6 100644 --- a/asdf/_tests/test_entry_points.py +++ b/asdf/_tests/test_entry_points.py @@ -1,9 +1,5 @@ -# The standard library importlib.metadata returns duplicate entrypoints -# for all python versions up to and including 3.11 -# https://github.com/python/importlib_metadata/issues/410#issuecomment-1304258228 -# see PR https://github.com/asdf-format/asdf/pull/1260 -# see issue https://github.com/asdf-format/asdf/issues/1254 -import importlib_metadata as metadata +import sys + import pytest from asdf import _entry_points @@ -12,6 +8,16 @@ from asdf.extension import ExtensionProxy from asdf.resource import ResourceMappingProxy +# The standard library importlib.metadata returns duplicate entrypoints +# for all python versions up to and including 3.11 +# https://github.com/python/importlib_metadata/issues/410#issuecomment-1304258228 +# see PR https://github.com/asdf-format/asdf/pull/1260 +# see issue https://github.com/asdf-format/asdf/issues/1254 +if sys.version_info >= (3, 12): + import importlib.metadata as metadata +else: + import importlib_metadata as metadata + @pytest.fixture() def mock_entry_points(): diff --git a/asdf/util.py b/asdf/util.py index 7db32eabc..bbd873d99 100644 --- a/asdf/util.py +++ b/asdf/util.py @@ -4,6 +4,7 @@ import math import re import struct +import sys import types import warnings from functools import lru_cache @@ -12,16 +13,20 @@ import numpy as np import yaml +from packaging.version import Version + +from . import constants, exceptions # The standard library importlib.metadata returns duplicate entrypoints # for all python versions up to and including 3.11 # https://github.com/python/importlib_metadata/issues/410#issuecomment-1304258228 # see PR https://github.com/asdf-format/asdf/pull/1260 # see issue https://github.com/asdf-format/asdf/issues/1254 -from importlib_metadata import packages_distributions -from packaging.version import Version +if sys.version_info >= (3, 12): + from importlib.metadata import packages_distributions +else: + from importlib_metadata import packages_distributions -from . import constants, exceptions # We're importing our own copy of urllib.parse because # we need to patch it to support asdf:// URIs, but it'd diff --git a/docs/conf.py b/docs/conf.py index a570016bd..f4877acba 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -6,13 +6,18 @@ else: import tomllib +from sphinx_asdf.conf import * # noqa: F403 + # The standard library importlib.metadata returns duplicate entrypoints # for all python versions up to and including 3.11 # https://github.com/python/importlib_metadata/issues/410#issuecomment-1304258228 # see PR https://github.com/asdf-format/asdf/pull/1260 # see issue https://github.com/asdf-format/asdf/issues/1254 -from importlib_metadata import distribution -from sphinx_asdf.conf import * # noqa: F403 +if sys.version_info >= (3, 12): + from importlib.metadata import distribution +else: + from importlib_metadata import distribution + # Get configuration information from `pyproject.toml` with open(Path(__file__).parent.parent / "pyproject.toml", "rb") as configuration_file: diff --git a/pyproject.toml b/pyproject.toml index 98e11e3be..698d908e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ dynamic = [ dependencies = [ "asdf-standard>=1.1.0", "asdf-transform-schemas>=0.3", # required for asdf-1.0.0 schema - "importlib-metadata>=4.11.4", + "importlib-metadata>=4.11.4 ; python_version<='3.11'", "jmespath>=0.6.2", "numpy>=1.22", "packaging>=19",