Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Upgrade to black 19.3b0 (#20)
Browse files Browse the repository at this point in the history
* Fixes #16

* Bump version

* Upgrade black

* Remove skip_numeric_underscore_normalization

* Test target_version option

* Use 19.3b0

* Remove py36

* Lint

* Add new fixtures
  • Loading branch information
rupert authored Mar 26, 2019
1 parent 0df2163 commit 80403a7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 26 deletions.
44 changes: 27 additions & 17 deletions pyls_black/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,22 @@ def format_document(document, range=None):


def format_text(*, text, config):
line_length = config["line_length"]
fast = config["fast"]
mode = black.FileMode.from_configuration(
py36=config["py36"],
pyi=config["pyi"],
skip_string_normalization=config["skip_string_normalization"],
skip_numeric_underscore_normalization=config[
"skip_numeric_underscore_normalization"
],
)
return black.format_file_contents(
text, line_length=line_length, fast=fast, mode=mode
mode = black.FileMode(
target_versions=config["target_version"],
line_length=config["line_length"],
is_pyi=config["pyi"],
string_normalization=not config["skip_string_normalization"],
)

return black.format_file_contents(text, fast=config["fast"], mode=mode)


def load_config(filename: str) -> Dict:
defaults = {
"line_length": 88,
"fast": False,
"py36": False,
"pyi": filename.endswith(".pyi"),
"skip_string_normalization": False,
"skip_numeric_underscore_normalization": False,
}

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

config = pyproject_toml.get("tool", {}).get("black", {})
file_config = pyproject_toml.get("tool", {}).get("black", {})
file_config = {
key.replace("--", "").replace("-", "_"): value
for key, value in file_config.items()
}

config = {
key.replace("--", "").replace("-", "_"): value for key, value in config.items()
key: file_config.get(key, default_value)
for key, default_value in defaults.items()
}

return {**defaults, **config}
if file_config.get("target_version"):
target_version = set(
black.TargetVersion[x.upper()] for x in file_config["target_version"]
)
elif file_config.get("py36"):
target_version = black.PY36_VERSIONS
else:
target_version = set()

config["target_version"] = target_version

return config
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tool.black]
py36 = true
target-version = ['py36', 'py37', 'py38']
exclude = '''
/(
\.venv
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

setup(
name="pyls-black",
version="0.4.2",
version="0.4.3",
description="Black plugin for the Python Language Server",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/rupert/pyls-black",
author="Rupert Bedford",
author_email="[email protected]",
packages=find_packages(exclude=["tests"]),
install_requires=["python-language-server", "black==18.9b0", "toml"],
install_requires=["python-language-server", "black>=19.3b0", "toml"],
extras_require={"dev": ["isort", "flake8", "pytest", "mypy", "pytest"]},
entry_points={"pyls": ["pyls_black = pyls_black.plugin"]},
classifiers=(
Expand Down
2 changes: 0 additions & 2 deletions tests/fixtures/config/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[tool.black]
line-length = 20
--fast = true
py36 = true
pyi = true
skip-string-normalization = true
skip-numeric-underscore-normalization = true
2 changes: 2 additions & 0 deletions tests/fixtures/py36/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
py36 = true
2 changes: 2 additions & 0 deletions tests/fixtures/target_version/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
target-version = ['py27']
20 changes: 16 additions & 4 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

import black
import pytest
from pyls.workspace import Document

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

# TODO split into smaller tests
assert config == {
"line_length": 20,
"py36": True,
"target_version": set(),
"pyi": True,
"fast": True,
"skip_string_normalization": True,
"skip_numeric_underscore_normalization": True,
}


def test_load_config_target_version():
config = load_config(str(fixtures_dir / "target_version" / "example.py"))

assert config["target_version"] == {black.TargetVersion.PY27}


def test_load_config_py36():
config = load_config(str(fixtures_dir / "py36" / "example.py"))

assert config["target_version"] == black.PY36_VERSIONS


def test_load_config_defaults():
config = load_config(str(fixtures_dir / "example.py"))

assert config == {
"line_length": 88,
"py36": False,
"target_version": set(),
"pyi": False,
"fast": False,
"skip_string_normalization": False,
"skip_numeric_underscore_normalization": False,
}

0 comments on commit 80403a7

Please sign in to comment.