From ca52f3e86f289a6a9c35d489223cbb2923a842be Mon Sep 17 00:00:00 2001
From: Hugo van Kemenade <hugovk@users.noreply.github.com>
Date: Mon, 11 Jul 2022 15:56:29 +0300
Subject: [PATCH] Use stdlib tomllib instead of tomli for Python 3.11+
---
setup.cfg | 2 +-
src/setuptools_scm/_integration/pyproject_reading.py | 10 +++++++---
testing/test_integration.py | 6 ++++--
tox.ini | 3 +--
4 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/setup.cfg b/setup.cfg
index a95503a..dd5e0c8 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -29,9 +29,9 @@ packages = find:
install_requires =
packaging>=20.0
setuptools
- tomli>=1.0.0 # keep in sync
typing-extensions
importlib-metadata;python_version < '3.8'
+ tomli>=1.0.0;python_version < '3.11' # keep in sync
python_requires = >=3.7
package_dir =
=src
diff --git a/src/setuptools_scm/_integration/pyproject_reading.py b/src/setuptools_scm/_integration/pyproject_reading.py
index f43e6b1c..04801b5e 100644
--- a/src/setuptools_scm/_integration/pyproject_reading.py
+++ b/src/setuptools_scm/_integration/pyproject_reading.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import sys
import warnings
from typing import Any
from typing import Callable
@@ -28,8 +29,11 @@ def project_name(self) -> str | None:
return self.project.get("name")
-def lazy_tomli_load(data: str) -> TOML_RESULT:
- from tomli import loads
+def lazy_toml_load(data: str) -> TOML_RESULT:
+ if sys.version_info >= (3, 11):
+ from tomllib import loads
+ else:
+ from tomli import loads
return loads(data)
@@ -40,7 +44,7 @@ def read_pyproject(
_load_toml: TOML_LOADER | None = None,
) -> PyProjectData:
if _load_toml is None:
- _load_toml = lazy_tomli_load
+ _load_toml = lazy_toml_load
with open(name, encoding="UTF-8") as strm:
data = strm.read()
defn = _load_toml(data)
diff --git a/testing/test_integration.py b/testing/test_integration.py
index 939bd4b6..f21b9e0c 100644
--- a/testing/test_integration.py
+++ b/testing/test_integration.py
@@ -24,7 +24,8 @@ def wd(wd: WorkDir) -> WorkDir:
def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
- pytest.importorskip("tomli")
+ if sys.version_info <= (3, 10):
+ pytest.importorskip("tomli")
monkeypatch.delenv("SETUPTOOLS_SCM_DEBUG")
pkg = tmp_path / "package"
pkg.mkdir()
@@ -83,7 +84,8 @@ def test_pyproject_support(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> N
@with_metadata_in
def test_pyproject_support_with_git(wd: WorkDir, metadata_in: str) -> None:
- pytest.importorskip("tomli")
+ if sys.version_info <= (3, 10):
+ pytest.importorskip("tomli")
wd.write("pyproject.toml", PYPROJECT_FILES[metadata_in])
wd.write("setup.py", SETUP_PY_FILES[metadata_in])
wd.write("setup.cfg", SETUP_CFG_FILES[metadata_in])
diff --git a/tox.ini b/tox.ini
index 1662bdfa..ae698972 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
[tox]
-envlist=py{37,38,39,310}-{test,selfcheck},check_readme,check-dist,check-bootstrap
+envlist=py{37,38,39,310,311}-{test,selfcheck},check_readme,check-dist,check-bootstrap
[pytest]
testpaths=testing
@@ -27,7 +27,6 @@ skip_install=
deps=
pytest
setuptools >= 45
- tomli
virtualenv>20
typing_extensions
commands=