Skip to content

Commit 80403a7

Browse files
authored
Upgrade to black 19.3b0 (#20)
* Fixes #16 * Bump version * Upgrade black * Remove skip_numeric_underscore_normalization * Test target_version option * Use 19.3b0 * Remove py36 * Lint * Add new fixtures
1 parent 0df2163 commit 80403a7

File tree

7 files changed

+50
-26
lines changed

7 files changed

+50
-26
lines changed

pyls_black/plugin.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,22 @@ def format_document(document, range=None):
5050

5151

5252
def format_text(*, text, config):
53-
line_length = config["line_length"]
54-
fast = config["fast"]
55-
mode = black.FileMode.from_configuration(
56-
py36=config["py36"],
57-
pyi=config["pyi"],
58-
skip_string_normalization=config["skip_string_normalization"],
59-
skip_numeric_underscore_normalization=config[
60-
"skip_numeric_underscore_normalization"
61-
],
62-
)
63-
return black.format_file_contents(
64-
text, line_length=line_length, fast=fast, mode=mode
53+
mode = black.FileMode(
54+
target_versions=config["target_version"],
55+
line_length=config["line_length"],
56+
is_pyi=config["pyi"],
57+
string_normalization=not config["skip_string_normalization"],
6558
)
6659

60+
return black.format_file_contents(text, fast=config["fast"], mode=mode)
61+
6762

6863
def load_config(filename: str) -> Dict:
6964
defaults = {
7065
"line_length": 88,
7166
"fast": False,
72-
"py36": False,
7367
"pyi": filename.endswith(".pyi"),
7468
"skip_string_normalization": False,
75-
"skip_numeric_underscore_normalization": False,
7669
}
7770

7871
root = black.find_project_root((filename,))
@@ -87,9 +80,26 @@ def load_config(filename: str) -> Dict:
8780
except (toml.TomlDecodeError, OSError):
8881
return defaults
8982

90-
config = pyproject_toml.get("tool", {}).get("black", {})
83+
file_config = pyproject_toml.get("tool", {}).get("black", {})
84+
file_config = {
85+
key.replace("--", "").replace("-", "_"): value
86+
for key, value in file_config.items()
87+
}
88+
9189
config = {
92-
key.replace("--", "").replace("-", "_"): value for key, value in config.items()
90+
key: file_config.get(key, default_value)
91+
for key, default_value in defaults.items()
9392
}
9493

95-
return {**defaults, **config}
94+
if file_config.get("target_version"):
95+
target_version = set(
96+
black.TargetVersion[x.upper()] for x in file_config["target_version"]
97+
)
98+
elif file_config.get("py36"):
99+
target_version = black.PY36_VERSIONS
100+
else:
101+
target_version = set()
102+
103+
config["target_version"] = target_version
104+
105+
return config

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.black]
2-
py36 = true
2+
target-version = ['py36', 'py37', 'py38']
33
exclude = '''
44
/(
55
\.venv

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
setup(
77
name="pyls-black",
8-
version="0.4.2",
8+
version="0.4.3",
99
description="Black plugin for the Python Language Server",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",
1212
url="https://github.com/rupert/pyls-black",
1313
author="Rupert Bedford",
1414
author_email="[email protected]",
1515
packages=find_packages(exclude=["tests"]),
16-
install_requires=["python-language-server", "black==18.9b0", "toml"],
16+
install_requires=["python-language-server", "black>=19.3b0", "toml"],
1717
extras_require={"dev": ["isort", "flake8", "pytest", "mypy", "pytest"]},
1818
entry_points={"pyls": ["pyls_black = pyls_black.plugin"]},
1919
classifiers=(

tests/fixtures/config/pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[tool.black]
22
line-length = 20
33
--fast = true
4-
py36 = true
54
pyi = true
65
skip-string-normalization = true
7-
skip-numeric-underscore-normalization = true

tests/fixtures/py36/pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
py36 = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tool.black]
2+
target-version = ['py27']

tests/test_plugin.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22

3+
import black
34
import pytest
45
from pyls.workspace import Document
56

@@ -161,24 +162,35 @@ def test_pyls_format_range_syntax_error(invalid_document):
161162
def test_load_config():
162163
config = load_config(str(fixtures_dir / "config" / "example.py"))
163164

165+
# TODO split into smaller tests
164166
assert config == {
165167
"line_length": 20,
166-
"py36": True,
168+
"target_version": set(),
167169
"pyi": True,
168170
"fast": True,
169171
"skip_string_normalization": True,
170-
"skip_numeric_underscore_normalization": True,
171172
}
172173

173174

175+
def test_load_config_target_version():
176+
config = load_config(str(fixtures_dir / "target_version" / "example.py"))
177+
178+
assert config["target_version"] == {black.TargetVersion.PY27}
179+
180+
181+
def test_load_config_py36():
182+
config = load_config(str(fixtures_dir / "py36" / "example.py"))
183+
184+
assert config["target_version"] == black.PY36_VERSIONS
185+
186+
174187
def test_load_config_defaults():
175188
config = load_config(str(fixtures_dir / "example.py"))
176189

177190
assert config == {
178191
"line_length": 88,
179-
"py36": False,
192+
"target_version": set(),
180193
"pyi": False,
181194
"fast": False,
182195
"skip_string_normalization": False,
183-
"skip_numeric_underscore_normalization": False,
184196
}

0 commit comments

Comments
 (0)