Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tomllib on Python 3.11 #2903

Merged
merged 2 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -45,6 +45,9 @@

<!-- Changes to how Black is packaged, such as dependency requirements -->

- On Python 3.11 and newer, use the standard library's `tomllib` instead of `tomli`
(#2903)

### Parser

<!-- Changes to the parser or to version autodetection -->
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -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'",
Expand Down
10 changes: 7 additions & 3 deletions src/black/files.py
Expand Up @@ -20,7 +20,11 @@
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 as tomllib

from black.output import err
from black.report import Report
Expand Down Expand Up @@ -97,10 +101,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()}

Expand Down