Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test and lint

on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Run pytest
run: python -m pytest

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
- name: Run ruff format
run: python -m ruff format --check
- name: Run ruff lint
run: python -m ruff check
- name: Run isort (check only)
run: python -m isort . --check
- name: Run mypy
run: python -m mypy .
14 changes: 4 additions & 10 deletions pylsp_isort/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def pylsp_settings() -> Dict[str, Any]:


@hookimpl(hookwrapper=True)
def pylsp_format_document(
config: Config, workspace: Workspace, document: Document
) -> Generator:
def pylsp_format_document(config: Config, workspace: Workspace, document: Document) -> Generator:
outcome = yield
with workspace.report_progress("format: isort"):
_format(outcome, config, document)
Expand All @@ -51,9 +49,7 @@ def pylsp_format_range(
_format(outcome, config, document, range)


def _format(
outcome, config: Config, document: Document, range: Optional[Range] = None
) -> None:
def _format(outcome, config: Config, document: Document, range: Optional[Range] = None) -> None:
result = outcome.get_result()
if result:
text = result[0]["newText"]
Expand Down Expand Up @@ -104,17 +100,15 @@ def isort_config(
if "settings_path" in settings:
if os.path.isfile(settings["settings_path"]):
config_kwargs["settings_file"] = os.path.abspath(settings["settings_path"])
config_kwargs["settings_path"] = os.path.dirname(
config_kwargs["settings_file"]
)
config_kwargs["settings_path"] = os.path.dirname(config_kwargs["settings_file"])
else:
config_kwargs["settings_path"] = os.path.abspath(settings["settings_path"])
elif target_path:
settings_path = os.path.abspath(target_path)
if not os.path.isdir(settings_path):
settings_path = os.path.dirname(settings_path)

_, found_settings = isort.settings._find_config(settings_path)
_, found_settings = isort.settings._find_config(str(settings_path))
if found_settings:
logger.info(
"Found a config file: `%s`, skipping given settings.",
Expand Down
31 changes: 25 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ readme = "README.md"
license = {text = "MIT"}
requires-python = ">=3.7"
dependencies = [
"python-lsp-server",
"isort>=5.0",
"python-lsp-server",
"isort>=5.0",
]

[project.urls]
Homepage = "https://github.com/chantera/python-lsp-isort"
"Bug Tracker" = "https://github.com/chantera/python-lsp-isort/issues"

[project.optional-dependencies]
dev = ["pytest"]
dev = [
"pytest>=8.3",
"ruff>=0.8",
"mypy>=1.13",
"tomli>=1.1.0; python_version<'3.11'",
]

[project.entry-points.pylsp]
isort = "pylsp_isort.plugin"
Expand All @@ -34,12 +39,26 @@ only-packages = true
[tool.hatch.build.targets.wheel]
packages = ["pylsp_isort"]

[tool.flake8]
max-line-length = 88
extend-ignore = "E203"
[tool.ruff]
line-length = 99
extend-exclude = ["tests/fixtures/*"]

[tool.ruff.lint]
select = ["E", "W", "F", "B"]

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false

[tool.mypy]
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = ["tests/fixtures/*"]
ignore_errors = true

[tool.isort]
profile = "black"
line_length = 99
extend_skip_glob = ["tests/fixtures/*"]
35 changes: 12 additions & 23 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
import pytest

from pylsp_isort import plugin


def _read_content(filename):
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
with open(path) as f:
return f.read()
from tests.utils import project_settings, read_content


@pytest.mark.parametrize(
("text", "settings", "expected"),
[
(
_read_content("unformatted.py"),
read_content("unformatted.py"),
{},
_read_content("formatted.py"),
read_content("formatted.py"),
),
(
_read_content("unformatted.py"),
read_content("unformatted.py"),
{"profile": "black"},
_read_content("formatted_black.py"),
read_content("formatted_black.py"),
),
],
)
Expand Down Expand Up @@ -63,21 +58,21 @@ def test_run_isort(text, settings, expected):
(
{},
__file__,
isort.Config(profile="black"),
isort.Config(**project_settings()),
False,
),
(
{"profile": "django"},
__file__,
isort.Config(profile="black"),
isort.Config(**project_settings()),
False,
),
(
{
"sections": ["FUTURE", "SECTION_A", "SECTION_B"],
"known_section_a": ["module_a"],
"known_section_b": ["module_b"],
},
},
None,
isort.Config(
sections=["FUTURE", "SECTION_A", "SECTION_B"],
Expand Down Expand Up @@ -108,14 +103,10 @@ def test_pylsp_settings(config):
assert plugins["isort"] not in config.disabled_plugins


def test_pylsp_format_document(
config, workspace, unformatted_document, formatted_document
):
actual = _receive(
plugin.pylsp_format_document, config, workspace, unformatted_document
)
def test_pylsp_format_document(config, workspace, unformatted_document, formatted_document):
actual = _receive(plugin.pylsp_format_document, config, workspace, unformatted_document)

text = _read_content(formatted_document.path)
text = read_content(formatted_document.path)
range = plugin.Range(
start={"line": 0, "character": 0},
end={"line": len(unformatted_document.lines), "character": 0},
Expand All @@ -130,9 +121,7 @@ def test_pylsp_format_range(config, workspace, unformatted_document):
start={"line": 2, "character": 0},
end={"line": 9, "character": 0},
)
actual = _receive(
plugin.pylsp_format_range, config, workspace, unformatted_document, range
)
actual = _receive(plugin.pylsp_format_range, config, workspace, unformatted_document, range)

text = "\n".join(
[
Expand Down
22 changes: 22 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import sys
from functools import lru_cache

if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib


@lru_cache
def project_settings():
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "pyproject.toml")
with open(path, "rb") as f:
data = tomllib.load(f)
return data["tool"]["isort"]


def read_content(filename):
path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
with open(path) as f:
return f.read()