Skip to content

Commit

Permalink
Use tomllib on Python 3.11 (#2903)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Mar 8, 2022
1 parent 24ffc54 commit 71e71e5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -48,6 +48,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

- Black can now parse starred expressions in the target of `for` and `async for`
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

0 comments on commit 71e71e5

Please sign in to comment.