Skip to content

Commit

Permalink
Merge pull request #8857 from hukkin/tomli
Browse files Browse the repository at this point in the history
Support TOML v1.0.0 syntax in `pyproject.toml`
  • Loading branch information
nicoddemus committed Jul 9, 2021
2 parents 6740fb9 + 5987251 commit e86bf7f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Expand Up @@ -58,7 +58,7 @@ repos:
- py>=1.8.2
- attrs>=19.2.0
- packaging
- types-toml
- tomli
- types-pkg_resources
- repo: local
hooks:
Expand Down
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -302,6 +302,7 @@ Sven-Hendrik Haase
Sylvain Marié
Tadek Teleżyński
Takafumi Arakaki
Taneli Hukkinen
Tanvi Mehta
Tarcisio Fischer
Tareq Alayan
Expand Down
1 change: 1 addition & 0 deletions changelog/8789.feature.rst
@@ -0,0 +1 @@
Switch TOML parser from ``toml`` to ``tomli`` for TOML v1.0.0 support in ``pyproject.toml``.
2 changes: 1 addition & 1 deletion setup.cfg
Expand Up @@ -47,7 +47,7 @@ install_requires =
packaging
pluggy>=0.12,<1.0.0a1
py>=1.8.2
toml
tomli>=1.0.0,<2.0.0
atomicwrites>=1.0;sys_platform=="win32"
colorama;sys_platform=="win32"
importlib-metadata>=0.12;python_version<"3.8"
Expand Down
8 changes: 6 additions & 2 deletions src/_pytest/config/findpaths.py
Expand Up @@ -64,9 +64,13 @@ def load_config_dict_from_file(

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

config = toml.load(str(filepath))
toml_text = filepath.read_text(encoding="utf-8")
try:
config = tomli.loads(toml_text)
except tomli.TOMLDecodeError as exc:
raise UsageError(str(exc)) from exc

result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
if result is not None:
Expand Down
10 changes: 10 additions & 0 deletions testing/test_findpaths.py
Expand Up @@ -2,6 +2,7 @@
from textwrap import dedent

import pytest
from _pytest.config import UsageError
from _pytest.config.findpaths import get_common_ancestor
from _pytest.config.findpaths import get_dirs_from_args
from _pytest.config.findpaths import load_config_dict_from_file
Expand Down Expand Up @@ -52,6 +53,13 @@ def test_unsupported_pytest_section_in_cfg_file(self, tmp_path: Path) -> None:
load_config_dict_from_file(fn)

def test_invalid_toml_file(self, tmp_path: Path) -> None:
"""Invalid .toml files should raise `UsageError`."""
fn = tmp_path / "myconfig.toml"
fn.write_text("]invalid toml[", encoding="utf-8")
with pytest.raises(UsageError):
load_config_dict_from_file(fn)

def test_custom_toml_file(self, tmp_path: Path) -> None:
""".toml files without [tool.pytest.ini_options] are not considered for configuration."""
fn = tmp_path / "myconfig.toml"
fn.write_text(
Expand All @@ -77,6 +85,7 @@ def test_valid_toml_file(self, tmp_path: Path) -> None:
y = 20.0
values = ["tests", "integration"]
name = "foo"
heterogeneous_array = [1, "str"]
"""
),
encoding="utf-8",
Expand All @@ -86,6 +95,7 @@ def test_valid_toml_file(self, tmp_path: Path) -> None:
"y": "20.0",
"values": ["tests", "integration"],
"name": "foo",
"heterogeneous_array": [1, "str"],
}


Expand Down

0 comments on commit e86bf7f

Please sign in to comment.