Skip to content

Commit

Permalink
Use tomllib on Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Mar 2, 2022
1 parent 1475264 commit 86cf49a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -44,6 +44,7 @@
### Packaging

<!-- 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

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
9 changes: 6 additions & 3 deletions src/black/files.py
Expand Up @@ -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 as tomllib

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

Expand Down

0 comments on commit 86cf49a

Please sign in to comment.