Skip to content

Commit

Permalink
fix error in handling config file when using smart mode
Browse files Browse the repository at this point in the history
  • Loading branch information
thep0y committed Sep 7, 2023
1 parent f6dfee4 commit a7fb249
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
38 changes: 21 additions & 17 deletions python_black/black.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: thepoy
# @Email: [email protected]
# @File Name: black.py
# @Created At: 2022-02-04 10:51:04
# @Modified At: 2023-02-12 19:14:13
# @Modified By: thepoy

import sublime
import sys

from pathlib import Path
from typing import Optional, Any, Dict, Tuple, List, TypedDict
from typing import Optional, Tuple, List

from .lib import tomli as tomllib
from .lib.black import format_str
from .lib.black.files import parse_pyproject_toml
from .lib.black.files import infer_target_version
from .lib.black.mode import Mode, TargetVersion
from .lib.black.const import DEFAULT_LINE_LENGTH, DEFAULT_INCLUDES
from .types import BlackConfig, SublimeSettings
Expand Down Expand Up @@ -76,10 +71,10 @@ def read_pyproject_toml(
Returns:
Tuple[Optional[BlackConfig], Optional[str]]: config and config file
"""
try:
config: BlackConfig = parse_pyproject_toml(str(config_file)) # type: ignore
except (OSError, ValueError, FileNotFoundError) as e:
raise Exception(f"Error reading configuration file: {e}")
with config_file.open("rb") as f:
pyproject_toml = tomllib.load(f)
config = pyproject_toml.get("tool", {}).get("black", {})
config = {k.replace("--", "").replace("-", "_"): v for k, v in config.items()}

logger.debug("project config: %s", config)

Expand All @@ -91,15 +86,23 @@ def read_pyproject_toml(
if not config:
return default_config, None

target_version = config.get("target_version")
if target_version is not None and not isinstance(target_version, list):
raise AttributeError("target-version: Config key target-version must be a list")
if "target_version" not in config:
inferred_target_version = infer_target_version(pyproject_toml)
if inferred_target_version is not None:
config["target_version"] = [v.name.lower() for v in inferred_target_version]
logger.debug("inferred target version: %s", inferred_target_version)
else:
target_version = config["target_version"]
if target_version is not None and not isinstance(target_version, list):
raise AttributeError(
"target-version: Config key target-version must be a list"
)

default_map: BlackConfig = {} # type: ignore
if default_config:
default_map.update(default_config)

default_map.update(config)
default_map.update(config) # type: ignore

logger.debug("configuration after applying `pyproject.toml`: %s", default_map)

Expand Down Expand Up @@ -164,7 +167,8 @@ def black_format_str(

if not default_config:
logger.info(
"smart mode is in use, but the black section is not found in the config file"
"Using smart mode, but no [black] section found in config file, "
"so no Python files in the current project will be formatted."
)
sublime.status_message("black: Black section is not found")

Expand Down
1 change: 1 addition & 0 deletions python_black/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,6 @@ def child_logger(name: str) -> logging.Logger:
log = __logger.getChild(name.replace(f"{LOGGER_NAME}.", ""))
log.setLevel(__logger.level)
log.handlers = __logger.handlers
log.propagate = False

return log
4 changes: 2 additions & 2 deletions python_black/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
class BlackConfig(TypedDict):
target_version: List[str]
line_length: int
string_normalization: bool
is_pyi: bool
skip_source_first_line: bool
magic_trailing_comma: bool
skip_string_normalization: bool
skip_magic_trailing_comma: bool
include: str


Expand Down
2 changes: 1 addition & 1 deletion sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": [
"string"
],
"default": true,
"default": "on",
"enum": [
"on",
"off",
Expand Down

0 comments on commit a7fb249

Please sign in to comment.