Skip to content

Commit

Permalink
Merge e0013dc into 1e0ec54
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Nov 14, 2021
2 parents 1e0ec54 + e0013dc commit 01c5b2d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
12 changes: 8 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
.venv
.coverage
.coverage.*
_build
.DS_Store
.vscode
docs/_static/pypi.svg
.tox
__pycache__

# Packaging artifacts
Expand All @@ -21,6 +18,13 @@ src/_black_version.py

.dmypy.json
*.swp
.hypothesis/
venv/
.ipynb_checkpoints/

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.hypothesis/
.pytest_cache/
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add partial support for the match statement. As it's experimental, it's only enabled
when `--target-version py310` is explicitly specified (#2586)
- Add support for parenthesized with (#2586)
- Allow specifying `config` in config files (#2525)

## 21.10b0

Expand Down
34 changes: 18 additions & 16 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,30 @@ def read_pyproject_toml(

if not config:
return None
else:
# 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
config = {
k: str(v) if not isinstance(v, (list, dict)) else v
for k, v in config.items()
}

target_version = config.get("target_version")

if ctx.default_map:
config.update(ctx.default_map)

black_config = config.get("config")
if black_config:
black_config_path = Path(Path(value).parent, black_config).resolve()
return read_pyproject_toml(ctx, param, str(black_config_path))

# 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
default_map: Dict[str, Any] = {
k: str(v) if not isinstance(v, (list, dict)) else v for k, v in config.items()
}

target_version = default_map.get("target_version")
if target_version is not None and not isinstance(target_version, list):
raise click.BadOptionUsage(
"target-version", "Config key target-version must be a list"
)

default_map: Dict[str, Any] = {}
if ctx.default_map:
default_map.update(ctx.default_map)
default_map.update(config)

ctx.default_map = default_map
return value
return str(value)


def target_version_option_callback(
Expand Down
9 changes: 9 additions & 0 deletions tests/_black_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.black]
verbose = 1
--check = "no"
diff = "y"
color = true
line-length = 88
target-version = ["py36", "py37", "py38"]
exclude='\.pyi?$'
include='\.py?$'
2 changes: 2 additions & 0 deletions tests/invalid_test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
config = "tests/bazqux.toml"
22 changes: 22 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,28 @@ def test_read_pyproject_toml(self) -> None:
self.assertEqual(config["exclude"], r"\.pyi?$")
self.assertEqual(config["include"], r"\.py?$")

def test_black_replace_config(self) -> None:
test_toml_file = THIS_DIR / "test_replace.toml"
fake_ctx = FakeContext()
black.read_pyproject_toml(fake_ctx, FakeParameter(), str(test_toml_file))
config = fake_ctx.default_map
# `0` in `test_replace.toml` and `1` in `_black_config.toml`. Should be `1` as
# the one linked should overwrite the config
self.assertEqual(config["verbose"], "1")
self.assertEqual(config["check"], "no")
self.assertEqual(config["diff"], "y")
self.assertEqual(config["color"], "True")
self.assertEqual(config["line_length"], "88")
self.assertEqual(config["target_version"], ["py36", "py37", "py38"])
self.assertEqual(config["exclude"], r"\.pyi?$")
self.assertEqual(config["include"], r"\.py?$")

def test_invalid_black_config(self) -> None:
test_toml_file = THIS_DIR / "invalid_test.toml"
fake_ctx = FakeContext()
with self.assertRaises(click.exceptions.FileError):
black.read_pyproject_toml(fake_ctx, FakeParameter(), str(test_toml_file))

def test_find_project_root(self) -> None:
with TemporaryDirectory() as workspace:
root = Path(workspace)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_replace.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tool.black]
config = "_black_config.toml"
verbose = 0
color = false

0 comments on commit 01c5b2d

Please sign in to comment.