Skip to content

Commit 86cf49a

Browse files
author
hauntsaninja
committed
Use tomllib on Python 3.11
1 parent 1475264 commit 86cf49a

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
### Packaging
4545

4646
<!-- Changes to how Black is packaged, such as dependency requirements -->
47+
- On Python 3.11 and newer, use the standard library's `tomllib` instead of `tomli` (#2903)
4748

4849
### Parser
4950

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def find_python_files(base: Path) -> List[Path]:
9999
install_requires=[
100100
"click>=8.0.0",
101101
"platformdirs>=2",
102-
"tomli>=1.1.0",
102+
"tomli>=1.1.0; python_version < '3.11'",
103103
"typed-ast>=1.4.2; python_version < '3.8' and implementation_name == 'cpython'",
104104
"pathspec>=0.9.0",
105105
"dataclasses>=0.6; python_version < '3.7'",

src/black/files.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
from mypy_extensions import mypyc_attr
2121
from pathspec import PathSpec
2222
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
23-
import tomli
23+
if sys.version_info >= (3, 11):
24+
import tomllib
25+
else:
26+
import tomli as tomllib
2427

2528
from black.output import err
2629
from black.report import Report
@@ -97,10 +100,10 @@ def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]:
97100
def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:
98101
"""Parse a pyproject toml file, pulling out relevant parts for Black
99102
100-
If parsing fails, will raise a tomli.TOMLDecodeError
103+
If parsing fails, will raise a tomllib.TOMLDecodeError
101104
"""
102105
with open(path_config, "rb") as f:
103-
pyproject_toml = tomli.load(f)
106+
pyproject_toml = tomllib.load(f)
104107
config = pyproject_toml.get("tool", {}).get("black", {})
105108
return {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}
106109

0 commit comments

Comments
 (0)