From 12a4b8bceb5807c279ed3af78441ce83de8b923d Mon Sep 17 00:00:00 2001 From: Rafal Chlodnicki Date: Thu, 26 Feb 2026 23:16:13 +0100 Subject: [PATCH] refactor: align formatting with LSP --- download_schemas.py | 2 + generate.py | 13 +++--- generated/lsp_types.py | 16 ++++++-- lsp_schema.py | 20 ++++++++-- pyproject.toml | 51 +++++++++++++++--------- utils/generate_enumerations.py | 11 +++-- utils/generate_notifications.py | 5 ++- utils/generate_requests_and_responses.py | 5 ++- utils/generate_structures.py | 19 +++++---- utils/generate_type_aliases.py | 5 ++- utils/helpers.py | 13 ++++-- 11 files changed, 111 insertions(+), 49 deletions(-) diff --git a/download_schemas.py b/download_schemas.py index 20b3082..1092ea8 100755 --- a/download_schemas.py +++ b/download_schemas.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +from __future__ import annotations + from pathlib import Path from urllib.request import urlopen diff --git a/generate.py b/generate.py index ab1990f..530d460 100755 --- a/generate.py +++ b/generate.py @@ -1,14 +1,18 @@ #!/usr/bin/env python3 from __future__ import annotations + from pathlib import Path -from typing import Literal, cast, TYPE_CHECKING +from typing import cast +from typing import Literal +from typing import TYPE_CHECKING from utils.generate_enumerations import generate_enumerations from utils.generate_notifications import generate_notifications from utils.generate_requests_and_responses import generate_requests_and_responses from utils.generate_structures import generate_structures from utils.generate_type_aliases import generate_type_aliases -from utils.helpers import get_new_literal_structures, reset_new_literal_structures +from utils.helpers import get_new_literal_structures +from utils.helpers import reset_new_literal_structures import json if TYPE_CHECKING: @@ -35,10 +39,7 @@ 'ApplyKind': 'IntFlag', } -ALIAS_OVERRIDES: dict[str, str] = { - 'LSPArray': "Sequence['LSPAny']", - 'LSPObject': 'Mapping[str, Any]' -} +ALIAS_OVERRIDES: dict[str, str] = {'LSPArray': "Sequence['LSPAny']", 'LSPObject': 'Mapping[str, Any]'} def generate(output: str) -> None: diff --git a/generated/lsp_types.py b/generated/lsp_types.py index 42d3081..f809d0c 100644 --- a/generated/lsp_types.py +++ b/generated/lsp_types.py @@ -3,10 +3,20 @@ # LSP v3.17.0 from __future__ import annotations -from enum import IntEnum, IntFlag, StrEnum -from typing import Any, Dict, List, Literal, Mapping, Sequence, TypedDict, Union -from typing_extensions import NotRequired, TypeAlias +from enum import IntEnum +from enum import IntFlag +from enum import StrEnum +from typing import Any +from typing import Dict +from typing import List +from typing import Literal +from typing import Mapping +from typing import Sequence +from typing import TypedDict +from typing import Union +from typing_extensions import NotRequired +from typing_extensions import TypeAlias URI = str DocumentUri = str diff --git a/lsp_schema.py b/lsp_schema.py index a36141f..c23578d 100644 --- a/lsp_schema.py +++ b/lsp_schema.py @@ -1,6 +1,8 @@ from __future__ import annotations -from typing import Literal, TypedDict -from typing_extensions import NotRequired + +from typing import Literal +from typing import NotRequired +from typing import TypedDict _BaseTypes = Literal['URI', 'DocumentUri', 'integer', 'uinteger', 'decimal', 'RegExp', 'string', 'boolean', 'null'] @@ -221,4 +223,16 @@ class MetaModel(TypedDict): typeAliases: list[TypeAlias] -EveryType = BaseType | ReferenceType | ArrayType | MapType | AndType | OrType | TupleType | StructureLiteralType | StringLiteralType | IntegerLiteralType | BooleanLiteralType # noqa: E501 +EveryType = ( + BaseType + | ReferenceType + | ArrayType + | MapType + | AndType + | OrType + | TupleType + | StructureLiteralType + | StringLiteralType + | IntegerLiteralType + | BooleanLiteralType +) diff --git a/pyproject.toml b/pyproject.toml index 2aa4943..3e338a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "lsp-types" version = "1.0.0" license = "MIT" -requires-python = ">=3.8" +requires-python = ">=3.13" classifiers = [ "Programming Language :: Python :: 3", "Operating System :: OS Independent", @@ -12,7 +12,7 @@ dependencies = [ ] [tool.pyright] -pythonVersion = "3.11" +pythonVersion = "3.13" reportUnusedCallResult = "none" [[tool.pyright.executionEnvironments]] @@ -21,12 +21,32 @@ pythonVersion = "3.8" [tool.ruff] line-length = 120 +target-version = "py313" + +[tool.ruff.per-file-target-version] +"generated/*.py" = "py38" [tool.ruff.format] quote-style = "single" +exclude = [ + "generated/*.py" +] + +[tool.ruff.lint.isort] +case-sensitive = false +force-single-line = true +from-first = true +no-sections = true +order-by-type = false +required-imports = ["from __future__ import annotations"] + +[tool.ruff.lint.flake8-quotes] +inline-quotes = "single" [tool.ruff.lint] extend-select = ["ALL"] +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] ignore = [ "COM812", # https://docs.astral.sh/ruff/rules/missing-trailing-comma/ "C901", # https://docs.astral.sh/ruff/rules/complex-structure/ @@ -41,7 +61,6 @@ ignore = [ "D213", # https://docs.astral.sh/ruff/rules/multi-line-summary-second-line/ "D400", # https://docs.astral.sh/ruff/rules/missing-trailing-period/ "D415", # https://docs.astral.sh/ruff/rules/missing-terminal-punctuation/ - "I001", # https://docs.astral.sh/ruff/rules/unsorted-imports/ "N801", # https://docs.astral.sh/ruff/rules/invalid-class-name/ "PLR0911", # https://docs.astral.sh/ruff/rules/too-many-return-statements/ "PLR0912", # https://docs.astral.sh/ruff/rules/too-many-branches/ @@ -51,20 +70,16 @@ ignore = [ "UP037" # https://docs.astral.sh/ruff/rules/quoted-annotation/ ] -[tool.ruff.lint.flake8-quotes] -inline-quotes = "single" - [tool.tox] -legacy_tox_ini = """ -[tox] -envlist = py3 -skipsdist = True +env_list = ["py3"] +skipsdist = true -[testenv] -deps = - pyright==1.1.407 - ruff==0.14.14 -commands = - pyright . - ruff check . -""" +[tool.tox.env_run_base] +deps = [ + "pyright==1.1.408", + "ruff==0.15.0", +] +commands = [ + ["pyright"], + ["ruff", "check"], +] diff --git a/utils/generate_enumerations.py b/utils/generate_enumerations.py index 45ae969..64a2fe6 100644 --- a/utils/generate_enumerations.py +++ b/utils/generate_enumerations.py @@ -1,11 +1,16 @@ from __future__ import annotations + from enum import Enum -from typing import Literal, TYPE_CHECKING -from utils.helpers import capitalize, format_comment, indentation +from typing import Literal +from typing import TYPE_CHECKING +from utils.helpers import capitalize +from utils.helpers import format_comment +from utils.helpers import indentation import keyword if TYPE_CHECKING: - from lsp_schema import Enumeration, EnumerationEntry + from lsp_schema import Enumeration + from lsp_schema import EnumerationEntry class EnumKind(Enum): diff --git a/utils/generate_notifications.py b/utils/generate_notifications.py index 3036fbe..76b834e 100644 --- a/utils/generate_notifications.py +++ b/utils/generate_notifications.py @@ -1,6 +1,9 @@ from __future__ import annotations + from typing import TYPE_CHECKING -from utils.helpers import StructureKind, format_type, indentation +from utils.helpers import format_type +from utils.helpers import indentation +from utils.helpers import StructureKind if TYPE_CHECKING: from lsp_schema import Notification diff --git a/utils/generate_requests_and_responses.py b/utils/generate_requests_and_responses.py index 373cc0e..e3f9f6f 100644 --- a/utils/generate_requests_and_responses.py +++ b/utils/generate_requests_and_responses.py @@ -1,6 +1,9 @@ from __future__ import annotations + from typing import TYPE_CHECKING -from utils.helpers import StructureKind, format_type, indentation +from utils.helpers import format_type +from utils.helpers import indentation +from utils.helpers import StructureKind if TYPE_CHECKING: from lsp_schema import Request diff --git a/utils/generate_structures.py b/utils/generate_structures.py index 80729f0..96bec0e 100644 --- a/utils/generate_structures.py +++ b/utils/generate_structures.py @@ -1,15 +1,14 @@ from __future__ import annotations + from typing import TYPE_CHECKING -from utils.helpers import ( - FormattedProperty, - format_comment, - indentation, - format_class_properties, - format_dict_properties, - get_formatted_properties, - has_invalid_property_name, - StructureKind, -) +from utils.helpers import format_class_properties +from utils.helpers import format_comment +from utils.helpers import format_dict_properties +from utils.helpers import FormattedProperty +from utils.helpers import get_formatted_properties +from utils.helpers import has_invalid_property_name +from utils.helpers import indentation +from utils.helpers import StructureKind if TYPE_CHECKING: from lsp_schema import Structure diff --git a/utils/generate_type_aliases.py b/utils/generate_type_aliases.py index 69dc029..b9721dd 100644 --- a/utils/generate_type_aliases.py +++ b/utils/generate_type_aliases.py @@ -1,6 +1,9 @@ from __future__ import annotations -from utils.helpers import format_comment, format_type, StructureKind + from typing import TYPE_CHECKING +from utils.helpers import format_comment +from utils.helpers import format_type +from utils.helpers import StructureKind if TYPE_CHECKING: from lsp_schema import TypeAlias diff --git a/utils/helpers.py b/utils/helpers.py index f562144..1ff368f 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -1,10 +1,17 @@ from __future__ import annotations + from enum import Enum -from typing import Any, ClassVar, TypedDict, TYPE_CHECKING +from typing import Any +from typing import ClassVar +from typing import TYPE_CHECKING +from typing import TypedDict import keyword if TYPE_CHECKING: - from lsp_schema import EveryType, BaseType, MapKeyType, Property + from lsp_schema import BaseType + from lsp_schema import EveryType + from lsp_schema import MapKeyType + from lsp_schema import Property indentation = ' ' @@ -127,7 +134,7 @@ def format_base_types(base_type: BaseType | MapKeyType) -> str: 'decimal': 'float', 'string': 'str', 'boolean': 'bool', - 'null': 'None' + 'null': 'None', } name = base_type['name']