Skip to content

Commit

Permalink
Show warning on invalid toml configuration (#4165)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
dankrzeminski32 and JelleZijlstra authored Jan 26, 2024
1 parent 7d78946 commit bccec8a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ release:

<!-- Changes to how Black can be configured -->

- Print warning when toml config contains an invalid key (#4165)
- Fix symlink handling, properly catch and ignore symlinks that point outside of root
(#4161)
- Fix cache mtime logic that resulted in false positive cache hits (#4128)
Expand Down
17 changes: 17 additions & 0 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def read_pyproject_toml(
if not config:
return None
else:
spellcheck_pyproject_toml_keys(ctx, list(config), value)
# Sanitize the values to be Click friendly. For more information please see:
# https://github.com/psf/black/issues/1458
# https://github.com/pallets/click/issues/1567
Expand Down Expand Up @@ -181,6 +182,22 @@ def read_pyproject_toml(
return value


def spellcheck_pyproject_toml_keys(
ctx: click.Context, config_keys: List[str], config_file_path: str
) -> None:
invalid_keys: List[str] = []
available_config_options = {param.name for param in ctx.command.params}
for key in config_keys:
if key not in available_config_options:
invalid_keys.append(key)
if invalid_keys:
keys_str = ", ".join(map(repr, invalid_keys))
out(
f"Invalid config keys detected: {keys_str} (in {config_file_path})",
fg="red",
)


def target_version_option_callback(
c: click.Context, p: Union[click.Option, click.Parameter], v: Tuple[str, ...]
) -> List[TargetVersion]:
Expand Down
5 changes: 5 additions & 0 deletions tests/data/incorrect_spelling.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.black]
ine_length = 50
target-ersion = ['py37']
exclude='\.pyi?$'
include='\.py?$'
17 changes: 17 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class FakeContext(click.Context):
def __init__(self) -> None:
self.default_map: Dict[str, Any] = {}
self.params: Dict[str, Any] = {}
self.command: click.Command = black.main
# Dummy root, since most of the tests don't care about it
self.obj: Dict[str, Any] = {"root": PROJECT_ROOT}

Expand Down Expand Up @@ -1538,6 +1539,22 @@ def test_parse_pyproject_toml(self) -> None:
self.assertEqual(config["exclude"], r"\.pyi?$")
self.assertEqual(config["include"], r"\.py?$")

def test_spellcheck_pyproject_toml(self) -> None:
test_toml_file = THIS_DIR / "data" / "incorrect_spelling.toml"
result = BlackRunner().invoke(
black.main,
[
"--code=print('hello world')",
"--verbose",
f"--config={str(test_toml_file)}",
],
)

assert (
r"Invalid config keys detected: 'ine_length', 'target_ersion' (in"
rf" {test_toml_file})" in result.stderr
)

def test_parse_pyproject_toml_project_metadata(self) -> None:
for test_toml, expected in [
("only_black_pyproject.toml", ["py310"]),
Expand Down

0 comments on commit bccec8a

Please sign in to comment.