Skip to content

Commit bccec8a

Browse files
Show warning on invalid toml configuration (#4165)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 7d78946 commit bccec8a

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ release:
6262

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

65+
- Print warning when toml config contains an invalid key (#4165)
6566
- Fix symlink handling, properly catch and ignore symlinks that point outside of root
6667
(#4161)
6768
- Fix cache mtime logic that resulted in false positive cache hits (#4128)

src/black/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def read_pyproject_toml(
142142
if not config:
143143
return None
144144
else:
145+
spellcheck_pyproject_toml_keys(ctx, list(config), value)
145146
# Sanitize the values to be Click friendly. For more information please see:
146147
# https://github.com/psf/black/issues/1458
147148
# https://github.com/pallets/click/issues/1567
@@ -181,6 +182,22 @@ def read_pyproject_toml(
181182
return value
182183

183184

185+
def spellcheck_pyproject_toml_keys(
186+
ctx: click.Context, config_keys: List[str], config_file_path: str
187+
) -> None:
188+
invalid_keys: List[str] = []
189+
available_config_options = {param.name for param in ctx.command.params}
190+
for key in config_keys:
191+
if key not in available_config_options:
192+
invalid_keys.append(key)
193+
if invalid_keys:
194+
keys_str = ", ".join(map(repr, invalid_keys))
195+
out(
196+
f"Invalid config keys detected: {keys_str} (in {config_file_path})",
197+
fg="red",
198+
)
199+
200+
184201
def target_version_option_callback(
185202
c: click.Context, p: Union[click.Option, click.Parameter], v: Tuple[str, ...]
186203
) -> List[TargetVersion]:

tests/data/incorrect_spelling.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tool.black]
2+
ine_length = 50
3+
target-ersion = ['py37']
4+
exclude='\.pyi?$'
5+
include='\.py?$'

tests/test_black.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class FakeContext(click.Context):
106106
def __init__(self) -> None:
107107
self.default_map: Dict[str, Any] = {}
108108
self.params: Dict[str, Any] = {}
109+
self.command: click.Command = black.main
109110
# Dummy root, since most of the tests don't care about it
110111
self.obj: Dict[str, Any] = {"root": PROJECT_ROOT}
111112

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

1542+
def test_spellcheck_pyproject_toml(self) -> None:
1543+
test_toml_file = THIS_DIR / "data" / "incorrect_spelling.toml"
1544+
result = BlackRunner().invoke(
1545+
black.main,
1546+
[
1547+
"--code=print('hello world')",
1548+
"--verbose",
1549+
f"--config={str(test_toml_file)}",
1550+
],
1551+
)
1552+
1553+
assert (
1554+
r"Invalid config keys detected: 'ine_length', 'target_ersion' (in"
1555+
rf" {test_toml_file})" in result.stderr
1556+
)
1557+
15411558
def test_parse_pyproject_toml_project_metadata(self) -> None:
15421559
for test_toml, expected in [
15431560
("only_black_pyproject.toml", ["py310"]),

0 commit comments

Comments
 (0)