Skip to content

Commit e86bf7f

Browse files
authored
Merge pull request #8857 from hukkin/tomli
Support TOML v1.0.0 syntax in `pyproject.toml`
2 parents 6740fb9 + 5987251 commit e86bf7f

File tree

6 files changed

+20
-4
lines changed

6 files changed

+20
-4
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ repos:
5858
- py>=1.8.2
5959
- attrs>=19.2.0
6060
- packaging
61-
- types-toml
61+
- tomli
6262
- types-pkg_resources
6363
- repo: local
6464
hooks:

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ Sven-Hendrik Haase
302302
Sylvain Marié
303303
Tadek Teleżyński
304304
Takafumi Arakaki
305+
Taneli Hukkinen
305306
Tanvi Mehta
306307
Tarcisio Fischer
307308
Tareq Alayan

changelog/8789.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Switch TOML parser from ``toml`` to ``tomli`` for TOML v1.0.0 support in ``pyproject.toml``.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ install_requires =
4747
packaging
4848
pluggy>=0.12,<1.0.0a1
4949
py>=1.8.2
50-
toml
50+
tomli>=1.0.0,<2.0.0
5151
atomicwrites>=1.0;sys_platform=="win32"
5252
colorama;sys_platform=="win32"
5353
importlib-metadata>=0.12;python_version<"3.8"

src/_pytest/config/findpaths.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ def load_config_dict_from_file(
6464

6565
# '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
6666
elif filepath.suffix == ".toml":
67-
import toml
67+
import tomli
6868

69-
config = toml.load(str(filepath))
69+
toml_text = filepath.read_text(encoding="utf-8")
70+
try:
71+
config = tomli.loads(toml_text)
72+
except tomli.TOMLDecodeError as exc:
73+
raise UsageError(str(exc)) from exc
7074

7175
result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
7276
if result is not None:

testing/test_findpaths.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from textwrap import dedent
33

44
import pytest
5+
from _pytest.config import UsageError
56
from _pytest.config.findpaths import get_common_ancestor
67
from _pytest.config.findpaths import get_dirs_from_args
78
from _pytest.config.findpaths import load_config_dict_from_file
@@ -52,6 +53,13 @@ def test_unsupported_pytest_section_in_cfg_file(self, tmp_path: Path) -> None:
5253
load_config_dict_from_file(fn)
5354

5455
def test_invalid_toml_file(self, tmp_path: Path) -> None:
56+
"""Invalid .toml files should raise `UsageError`."""
57+
fn = tmp_path / "myconfig.toml"
58+
fn.write_text("]invalid toml[", encoding="utf-8")
59+
with pytest.raises(UsageError):
60+
load_config_dict_from_file(fn)
61+
62+
def test_custom_toml_file(self, tmp_path: Path) -> None:
5563
""".toml files without [tool.pytest.ini_options] are not considered for configuration."""
5664
fn = tmp_path / "myconfig.toml"
5765
fn.write_text(
@@ -77,6 +85,7 @@ def test_valid_toml_file(self, tmp_path: Path) -> None:
7785
y = 20.0
7886
values = ["tests", "integration"]
7987
name = "foo"
88+
heterogeneous_array = [1, "str"]
8089
"""
8190
),
8291
encoding="utf-8",
@@ -86,6 +95,7 @@ def test_valid_toml_file(self, tmp_path: Path) -> None:
8695
"y": "20.0",
8796
"values": ["tests", "integration"],
8897
"name": "foo",
98+
"heterogeneous_array": [1, "str"],
8999
}
90100

91101

0 commit comments

Comments
 (0)