Repository: dask/dask
Pull request #10955 (2024-02-27 02:48:49)

Use stdlib importlib.metadata on Python 3.12+
Follow-up to https://github.com/dask/dask/pull/10070


github-actions[bot]: ## Unit Test Results
_See [test report](https://dask.github.io/dask/test_report.html) for an extended history of previous test failures. This is useful for diagnosing flaky tests._

     15 files  ±0       15 suites  ±0   3h 20m 41s :stopwatch: + 1m 40s
 13 104 tests ±0   12 175 :white_check_mark: ±0     929 :zzz: ±0  0 :x: ±0 
162 287 runs  ±0  145 755 :white_check_mark:  - 4  16 532 :zzz: +4  0 :x: ±0 

Results for commit 893f6114. ± Comparison against base commit 9531ebe9.

[test-results]:data:application/gzip;base64,H4sIAH5V3WUC/02MSw7DIAwFrxKx7gKbX+hlKgJBQs2nIrCqevdSmpBsLM08a97Eh2ncyL0DcevIlkM6yeVoUliXHyPlUFRZU90ZUH7wY8vW1idQ4pTP8CpSo27KmzAVRZsYY1zjbmJealki9mrnVuZCCdHkvwxSMDzcJV35WrbrPIdUgPSaeQnAebl6tMMggTsj0fhBKemcA4bUoSGfL+C+mhAZAQAA


Patch:
diff --git a/continuous_integration/environment-3.12.yaml b/continuous_integration/environment-3.12.yaml
index fac2a8f5f..7e112d4a3 100644
--- a/continuous_integration/environment-3.12.yaml
+++ b/continuous_integration/environment-3.12.yaml
@@ -12,7 +12,6 @@ dependencies:
   - cloudpickle
   - partd
   - fsspec
-  - importlib_metadata
   - toolz
   # test dependencies
   - pre-commit
diff --git a/continuous_integration/recipe/meta.yaml b/continuous_integration/recipe/meta.yaml
index b55aa028c..f5834645f 100644
--- a/continuous_integration/recipe/meta.yaml
+++ b/continuous_integration/recipe/meta.yaml
@@ -34,7 +34,7 @@ requirements:
     - partd >=1.2.0
     - pyyaml >=5.3.1
     - toolz >=0.10.0
-    - importlib_metadata >=4.13.0
+    - importlib_metadata >=4.13.0 # [py<312]
 
 test:
   imports:
diff --git a/dask/_compatibility.py b/dask/_compatibility.py
index 2272c905e..b5ac9389f 100644
--- a/dask/_compatibility.py
+++ b/dask/_compatibility.py
@@ -3,7 +3,10 @@ from __future__ import annotations
 import sys
 import warnings
 
-from importlib_metadata import entry_points as _entry_points
+if sys.version_info >= (3, 12):
+    import importlib.metadata as importlib_metadata
+else:
+    import importlib_metadata
 from packaging.version import parse as parse_version
 
 PY_VERSION = parse_version(".".join(map(str, sys.version_info[:3])))
@@ -18,4 +21,4 @@ def entry_points(group=None):
         DeprecationWarning,
         stacklevel=2,
     )
-    return _entry_points(group=group)
+    return importlib_metadata.entry_points(group=group)
diff --git a/dask/backends.py b/dask/backends.py
index 4dc11fcfd..276429be7 100644
--- a/dask/backends.py
+++ b/dask/backends.py
@@ -4,9 +4,8 @@ from collections.abc import Callable
 from functools import lru_cache, wraps
 from typing import TYPE_CHECKING, Generic, TypeVar
 
-import importlib_metadata
-
 from dask import config
+from dask._compatibility import importlib_metadata
 from dask.utils import funcname
 
 if TYPE_CHECKING:
diff --git a/dask/cli.py b/dask/cli.py
index 16a02d592..5185c40bc 100644
--- a/dask/cli.py
+++ b/dask/cli.py
@@ -6,11 +6,11 @@ from functools import reduce
 from typing import Any
 
 import click
-import importlib_metadata
 import yaml
 
 import dask
 from dask import __version__
+from dask._compatibility import importlib_metadata
 
 CONTEXT_SETTINGS = {
     "help_option_names": ["-h", "--help"],
diff --git a/dask/sizeof.py b/dask/sizeof.py
index 9ad81b2a1..fd997d2d9 100644
--- a/dask/sizeof.py
+++ b/dask/sizeof.py
@@ -6,9 +6,9 @@ import random
 import sys
 from array import array
 
-import importlib_metadata
 from packaging.version import parse as parse_version
 
+from dask._compatibility import importlib_metadata
 from dask.utils import Dispatch
 
 sizeof = Dispatch(name="sizeof")
diff --git a/dask/utils.py b/dask/utils.py
index 0e974fe47..5090b38f4 100644
--- a/dask/utils.py
+++ b/dask/utils.py
@@ -2174,11 +2174,12 @@ def cached_cumsum(seq, initial_zero=False):
 def show_versions() -> None:
     """Provide version information for bug reports."""
 
-    from importlib.metadata import PackageNotFoundError, version
     from json import dumps
     from platform import uname
     from sys import stdout, version_info
 
+    from dask._compatibility import importlib_metadata
+
     try:
         from distributed import __version__ as distributed_version
     except ImportError:
@@ -2206,8 +2207,8 @@ def show_versions() -> None:
 
     for modname in deps:
         try:
-            result[modname] = version(modname)
-        except PackageNotFoundError:
+            result[modname] = importlib_metadata.version(modname)
+        except importlib_metadata.PackageNotFoundError:
             result[modname] = None
 
     stdout.writelines(dumps(result, indent=2))
diff --git a/pyproject.toml b/pyproject.toml
index 349520224..f1a50b6d6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,9 +40,7 @@ dependencies = [
     # importlib.metadata has the following bugs fixed in 3.10.9 and 3.11.1
     # https://github.com/python/cpython/issues/99130
     # https://github.com/python/cpython/issues/98706
-    # TODO: when Python 3.12 is support is added this could be a
-    # conditional dependency
-    "importlib_metadata >= 4.13.0",
+    "importlib_metadata >= 4.13.0; python_version < '3.12' ",
 ]
 dynamic = ["version"]
 


Test patch:
diff --git a/dask/tests/test_ci.py b/dask/tests/test_ci.py
index 10f0efb98..d21146bdf 100644
--- a/dask/tests/test_ci.py
+++ b/dask/tests/test_ci.py
@@ -2,10 +2,11 @@ from __future__ import annotations
 
 import os
 
-import importlib_metadata
 import pytest
 from packaging.version import Version
 
+from dask._compatibility import importlib_metadata
+
 
 @pytest.mark.xfail(reason="https://github.com/dask/dask/issues/9735", strict=False)
 @pytest.mark.skipif(
diff --git a/dask/tests/test_cli.py b/dask/tests/test_cli.py
index 5bc3d5486..5f0df156d 100644
--- a/dask/tests/test_cli.py
+++ b/dask/tests/test_cli.py
@@ -7,13 +7,13 @@ import platform
 import sys
 
 import click
-import importlib_metadata
 import pytest
 import yaml
 from click.testing import CliRunner
 
 import dask
 import dask.cli
+from dask._compatibility import importlib_metadata
 
 
 def test_config_get_no_key():