Use tomllib on Python 3.11 (#9741)

This commit is contained in:
Shantanu
2022-04-17 11:11:17 -07:00
committed by GitHub
parent 752a059cc2
commit 2e8a319828
3 changed files with 11 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import os
import sys
from pathlib import Path
from typing import Dict
from typing import Iterable
@@ -64,12 +65,15 @@ def load_config_dict_from_file(
# '.toml' files are considered if they contain a [tool.pytest.ini_options] table.
elif filepath.suffix == ".toml":
import tomli
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
toml_text = filepath.read_text(encoding="utf-8")
try:
config = tomli.loads(toml_text)
except tomli.TOMLDecodeError as exc:
config = tomllib.loads(toml_text)
except tomllib.TOMLDecodeError as exc:
raise UsageError(f"{filepath}: {exc}") from exc
result = config.get("tool", {}).get("pytest", {}).get("ini_options", None)