From 378ba783a8f8dba282fa599f68b6ba212f9b5ab5 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 2 Mar 2022 14:51:04 -0800 Subject: [PATCH] Use tomllib on Python 3.11 --- CHANGES.md | 1 + setup.py | 2 +- src/black/files.py | 9 ++++++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 5f9341d048c..63aa1d216f1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -44,6 +44,7 @@ ### Packaging +- On Python 3.11 and newer, use the standard library's `tomllib` instead of `tomli` (#2903) ### Parser diff --git a/setup.py b/setup.py index 466f1a9c3a6..6b5b957e96f 100644 --- a/setup.py +++ b/setup.py @@ -99,7 +99,7 @@ def find_python_files(base: Path) -> List[Path]: install_requires=[ "click>=8.0.0", "platformdirs>=2", - "tomli>=1.1.0", + "tomli>=1.1.0; python_version < '3.11'", "typed-ast>=1.4.2; python_version < '3.8' and implementation_name == 'cpython'", "pathspec>=0.9.0", "dataclasses>=0.6; python_version < '3.7'", diff --git a/src/black/files.py b/src/black/files.py index 8348e0d8c28..418528c472c 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -20,7 +20,10 @@ from mypy_extensions import mypyc_attr from pathspec import PathSpec from pathspec.patterns.gitwildmatch import GitWildMatchPatternError -import tomli +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli from black.output import err from black.report import Report @@ -97,10 +100,10 @@ def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]: def parse_pyproject_toml(path_config: str) -> Dict[str, Any]: """Parse a pyproject toml file, pulling out relevant parts for Black - If parsing fails, will raise a tomli.TOMLDecodeError + If parsing fails, will raise a tomllib.TOMLDecodeError """ with open(path_config, "rb") as f: - pyproject_toml = tomli.load(f) + pyproject_toml = tomllib.load(f) config = pyproject_toml.get("tool", {}).get("black", {}) return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}