Repository: dask/dask

Issue:
Sphinx API documentation for `dask.config` shows the whole config
https://docs.dask.org/en/stable/configuration.html#api

The rendered signature for `get`, `set`, `refresh` contains a snapshot of the whole dask config, which makes it hard and confusing to read.

Discussion:
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 23m 4s :stopwatch: +27s
 13 104 tests ±0   12 174 :white_check_mark:  - 1     929 :zzz: ±0  0 :x: ±0  1 :fire: +1 
162 287 runs  ±0  145 760 :white_check_mark: +1  16 526 :zzz:  - 2  0 :x: ±0  1 :fire: +1 

For more details on these errors, see [this check](https://github.com/dask/dask/runs/22025932651).

Results for commit 1cd3bcf6. ± Comparison against base commit 9531ebe9.

[test-results]:data:application/gzip;base64,H4sIACHB3WUC/02MzQ7CIBAGX6Xh7IFdKEt9GYPbkhD7YyicjO8ubRV7nPl25yV8GIdVXBtoL41Yc0h/6nN0KSzzxghWF1XWtO8KZOXbmpmPIzrJR3gW2WFXlXdhLEpWMcS4xO2zmJjnvWwQLX25lnVLRlZ5lMG0aH7ulN75XOZlmkIqIIB7dWdvlDPeDpI0A5Mk6zqlGXsCqxhBknh/AByLMJkZAQAA


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/config.py b/dask/config.py
index ab3ae93b5..1376ae50f 100644
--- a/dask/config.py
+++ b/dask/config.py
@@ -415,10 +415,13 @@ class set:
     def __init__(
         self,
         arg: Mapping | None = None,
-        config: dict = config,
+        config: dict | None = None,
         lock: threading.Lock = config_lock,
         **kwargs,
     ):
+        if config is None:  # Keep Sphinx autofunction documentation clean
+            config = global_config
+
         with lock:
             self.config = config
             self._record = []
@@ -524,7 +527,7 @@ def collect(paths: list[str] = paths, env: Mapping[str, str] | None = None) -> d
 
 
 def refresh(
-    config: dict = config, defaults: list[Mapping] = defaults, **kwargs
+    config: dict | None = None, defaults: list[Mapping] = defaults, **kwargs
 ) -> None:
     """
     Update configuration by re-reading yaml files and env variables
@@ -550,6 +553,9 @@ def refresh(
     dask.config.collect: for parameters
     dask.config.update_defaults
     """
+    if config is None:  # Keep Sphinx autofunction documentation clean
+        config = global_config
+
     config.clear()
 
     for d in defaults:
@@ -562,7 +568,7 @@ def refresh(
 def get(
     key: str,
     default: Any = no_default,
-    config: dict = config,
+    config: dict | None = None,
     override_with: Any = None,
 ) -> Any:
     """
@@ -597,6 +603,10 @@ def get(
     """
     if override_with is not None:
         return override_with
+
+    if config is None:  # Keep Sphinx autofunction documentation clean
+        config = global_config
+
     keys = key.split(".")
     result = config
     for k in keys:
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():