From 72d0b719807fde4012ccc757d2aa2c870475e69c Mon Sep 17 00:00:00 2001 From: Hiroki Teranishi Date: Sat, 3 May 2025 01:43:26 +0900 Subject: [PATCH 1/3] Update and apply toolchain. --- pylsp_isort/plugin.py | 14 ++++---------- pyproject.toml | 30 ++++++++++++++++++++++++------ tests/test_plugin.py | 14 ++++---------- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/pylsp_isort/plugin.py b/pylsp_isort/plugin.py index 87f4b92..a53fe9e 100644 --- a/pylsp_isort/plugin.py +++ b/pylsp_isort/plugin.py @@ -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) @@ -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"] @@ -104,9 +100,7 @@ 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: @@ -114,7 +108,7 @@ def isort_config( 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.", diff --git a/pyproject.toml b/pyproject.toml index d95a369..9e9f108 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,8 +13,8 @@ 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] @@ -22,7 +22,11 @@ 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", +] [project.entry-points.pylsp] isort = "pylsp_isort.plugin" @@ -34,12 +38,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/*"] diff --git a/tests/test_plugin.py b/tests/test_plugin.py index cab5958..00ef4f6 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -77,7 +77,7 @@ def test_run_isort(text, settings, expected): "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"], @@ -108,12 +108,8 @@ 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) range = plugin.Range( @@ -130,9 +126,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( [ From cb9e125e70334d3e0b90e7bf52402b6fe906c78b Mon Sep 17 00:00:00 2001 From: Hiroki Teranishi Date: Sat, 3 May 2025 02:18:25 +0900 Subject: [PATCH 2/3] Set up CI. --- .github/workflows/ci.yml | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8b1954f --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 . From 230d03d6f2e3ebc6217886fdec29ef8098a77a5f Mon Sep 17 00:00:00 2001 From: Hiroki Teranishi Date: Sat, 3 May 2025 02:58:18 +0900 Subject: [PATCH 3/3] Fix tests. --- pyproject.toml | 1 + tests/test_plugin.py | 21 ++++++++------------- tests/utils.py | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 tests/utils.py diff --git a/pyproject.toml b/pyproject.toml index 9e9f108..9e0e48d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ dev = [ "pytest>=8.3", "ruff>=0.8", "mypy>=1.13", + "tomli>=1.1.0; python_version<'3.11'", ] [project.entry-points.pylsp] diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 00ef4f6..dbf800a 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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"), ), ], ) @@ -63,13 +58,13 @@ 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, ), ( @@ -111,7 +106,7 @@ def test_pylsp_settings(config): 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}, diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..ed68fc8 --- /dev/null +++ b/tests/utils.py @@ -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()