Repository: jaraco/keyring (language: python)

Issue:
Make importlib_metadata optional on Python 3.10.
> That extra dependency will become optional again in Python 3.10 alpha 7.

Could it be marked as required only for Python < 3.10 in `setup.cfg`?

_Originally posted by @dotlambda in https://github.com/jaraco/keyring/issues/503#issuecomment-1134170726_


Discussion / hints:
I noticed that in my project, only keyring depends on importlib_metadata.
What I don't understand is that all libraries put the dependency optional for python less than 3.8. Some examples are below

https://github.com/pytest-dev/pytest/blob/611b579d21f7e62b4c8ed54ab70fbfee7c6f5f64/setup.cfg#L51
https://github.com/PyCQA/flake8/blob/1c85f3d07c3aadf597db205dff597da2987745f7/setup.cfg#L45
https://github.com/sqlalchemy/sqlalchemy/blob/3d00de4cca7a80650316754f338317824f921940/setup.cfg#L38

Fix patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index cd7bff74..500626c4 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,3 +1,8 @@
+v23.6.0
+-------
+
+* #575: Only require ``importlib_metadata`` on older Pythons.
+
 v23.5.1
 -------
 
diff --git a/hook-keyring.backend.py b/hook-keyring.backend.py
index 16d4044f..08be9253 100644
--- a/hook-keyring.backend.py
+++ b/hook-keyring.backend.py
@@ -1,6 +1,7 @@
-# Used by pyinstaller to expose hidden imports
-
-import importlib_metadata as metadata
+"""
+Hook used by pyinstaller to expose hidden imports.
+"""
 
+from keyring.py310compat import metadata
 
 hiddenimports = [ep.value for ep in metadata.entry_points(group='keyring.backends')]
diff --git a/keyring/backend.py b/keyring/backend.py
index 332124f3..be393a7e 100644
--- a/keyring/backend.py
+++ b/keyring/backend.py
@@ -9,8 +9,7 @@
 
 from typing import Optional
 
-import importlib_metadata as metadata
-
+from .py310compat import metadata
 from . import credentials, errors, util
 from .util import properties
 
diff --git a/keyring/py310compat.py b/keyring/py310compat.py
new file mode 100644
index 00000000..3c7ee970
--- /dev/null
+++ b/keyring/py310compat.py
@@ -0,0 +1,10 @@
+import sys
+
+
+__all__ = ['metadata']
+
+
+if sys.version_info > (3, 10):
+    import importlib.metadata as metadata
+else:
+    import importlib_metadata as metadata  # type: ignore
diff --git a/setup.cfg b/setup.cfg
index d3cb1be2..43c09bcb 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -23,7 +23,7 @@ install_requires =
 	pywin32-ctypes!=0.1.0,!=0.1.1; sys_platform=="win32"
 	SecretStorage>=3.2; sys_platform=="linux"
 	jeepney>=0.4.2; sys_platform=="linux"
-	importlib_metadata >= 3.6
+	importlib_metadata >= 3.6; python_version < "3.10"
 
 [options.packages.find]
 exclude =


Test patch:
diff --git a/tests/test_packaging.py b/tests/test_packaging.py
index e707d427..383009d4 100644
--- a/tests/test_packaging.py
+++ b/tests/test_packaging.py
@@ -1,6 +1,5 @@
-import importlib_metadata as metadata
-
 from keyring import backend
+from keyring.py310compat import metadata
 
 
 def test_entry_point():