Merge pull request #8857 from hukkin/tomli

Support TOML v1.0.0 syntax in `pyproject.toml`
This commit is contained in:
Bruno Oliveira 2021-07-09 11:05:46 -03:00 committed by GitHub
commit e86bf7fda0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 4 deletions

View File

@ -58,7 +58,7 @@ repos:
- py>=1.8.2 - py>=1.8.2
- attrs>=19.2.0 - attrs>=19.2.0
- packaging - packaging
- types-toml - tomli
- types-pkg_resources - types-pkg_resources
- repo: local - repo: local
hooks: hooks:

View File

@ -302,6 +302,7 @@ Sven-Hendrik Haase
Sylvain Marié Sylvain Marié
Tadek Teleżyński Tadek Teleżyński
Takafumi Arakaki Takafumi Arakaki
Taneli Hukkinen
Tanvi Mehta Tanvi Mehta
Tarcisio Fischer Tarcisio Fischer
Tareq Alayan Tareq Alayan

View File

@ -0,0 +1 @@
Switch TOML parser from ``toml`` to ``tomli`` for TOML v1.0.0 support in ``pyproject.toml``.

View File

@ -47,7 +47,7 @@ install_requires =
packaging packaging
pluggy>=0.12,<1.0.0a1 pluggy>=0.12,<1.0.0a1
py>=1.8.2 py>=1.8.2
toml tomli>=1.0.0,<2.0.0
atomicwrites>=1.0;sys_platform=="win32" atomicwrites>=1.0;sys_platform=="win32"
colorama;sys_platform=="win32" colorama;sys_platform=="win32"
importlib-metadata>=0.12;python_version<"3.8" importlib-metadata>=0.12;python_version<"3.8"

View File

@ -64,9 +64,13 @@ def load_config_dict_from_file(
# '.toml' files are considered if they contain a [tool.pytest.ini_options] table. # '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
elif filepath.suffix == ".toml": 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) result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)
if result is not None: if result is not None:

View File

@ -2,6 +2,7 @@ from pathlib import Path
from textwrap import dedent from textwrap import dedent
import pytest import pytest
from _pytest.config import UsageError
from _pytest.config.findpaths import get_common_ancestor from _pytest.config.findpaths import get_common_ancestor
from _pytest.config.findpaths import get_dirs_from_args from _pytest.config.findpaths import get_dirs_from_args
from _pytest.config.findpaths import load_config_dict_from_file from _pytest.config.findpaths import load_config_dict_from_file
@ -52,6 +53,13 @@ class TestLoadConfigDictFromFile:
load_config_dict_from_file(fn) load_config_dict_from_file(fn)
def test_invalid_toml_file(self, tmp_path: Path) -> None: 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.""" """.toml files without [tool.pytest.ini_options] are not considered for configuration."""
fn = tmp_path / "myconfig.toml" fn = tmp_path / "myconfig.toml"
fn.write_text( fn.write_text(
@ -77,6 +85,7 @@ class TestLoadConfigDictFromFile:
y = 20.0 y = 20.0
values = ["tests", "integration"] values = ["tests", "integration"]
name = "foo" name = "foo"
heterogeneous_array = [1, "str"]
""" """
), ),
encoding="utf-8", encoding="utf-8",
@ -86,6 +95,7 @@ class TestLoadConfigDictFromFile:
"y": "20.0", "y": "20.0",
"values": ["tests", "integration"], "values": ["tests", "integration"],
"name": "foo", "name": "foo",
"heterogeneous_array": [1, "str"],
} }