Skip to content

Commit 3444db9

Browse files
authored
refactor: fix most ruff issues in generated types (#29)
* refactor: fix most ruff issues in generated types * run tox * unused * remove mypy * update readme
1 parent 55946f3 commit 3444db9

17 files changed

Lines changed: 4412 additions & 9079 deletions

.github/workflows/main.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ jobs:
1616
- uses: actions/setup-python@v6
1717
with:
1818
python-version: '3.14'
19-
- run: pip3 install pyright==1.1.271 flake8==5.0.4 --user
19+
- run: pip3 install tox --user
2020
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
21-
- run: pyright .
22-
- run: flake8 .
21+
- run: tox

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ python ./generate.py
1818
```
1919
Copy the `lsp_types.py` file to your project.
2020

21-
The `lsp_types_sublime_text_33.py` file is made specifically for use in Sublime's LSP package using Python 3.3 host that doesn't support class-based `TypedDict` syntax. For all other cases stick with `lsp_types.py`.
22-
2321
NOTE: Do not import types that begin with `__`. These types are internal types and are not meant to be used.

download_schemas.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from pathlib import Path
12
from urllib.request import urlopen
23

3-
lsp_json_schema = urlopen("https://raw.githubusercontent.com/microsoft/vscode-languageserver-node/main/protocol/metaModel.schema.json").read().decode('utf-8')
4-
open("./lsprotocol/lsp.schema.json", "w").write(lsp_json_schema)
54

6-
lsp_meta_model = urlopen("https://raw.githubusercontent.com/microsoft/vscode-languageserver-node/main/protocol/metaModel.json").read().decode('utf-8')
7-
open("./lsprotocol/lsp.json", "w").write(lsp_meta_model)
5+
REPO_URL = 'https://raw.githubusercontent.com/microsoft/vscode-languageserver-node'
6+
7+
with urlopen(f'{REPO_URL}/main/protocol/metaModel.schema.json') as url:
8+
Path('./lsprotocol/lsp.schema.json', 'w').write_text(url.read().decode('utf-8'))
9+
10+
with urlopen(f'{REPO_URL}/main/protocol/metaModel.json'):
11+
Path('./lsprotocol/lsp.json', 'w').write_text(url.read().decode('utf-8'))

generate.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#!/usr/bin/env python3
2-
import json
32

4-
from lsp_schema import MetaModel
5-
from typing import Dict, Literal
3+
from __future__ import annotations
4+
from pathlib import Path
5+
from typing import Literal, cast, TYPE_CHECKING
66
from utils.generate_enumerations import generate_enumerations
77
from utils.generate_structures import generate_structures
88
from utils.generate_type_aliases import generate_type_aliases
9-
from utils.helpers import get_new_literal_structures, reset_new_literal_structures, StructureKind
9+
from utils.helpers import get_new_literal_structures, reset_new_literal_structures
10+
import json
11+
12+
if TYPE_CHECKING:
13+
from lsp_schema import MetaModel
1014

1115

12-
ENUM_OVERRIDES = {
16+
ENUM_OVERRIDES: dict[str, Literal['StrEnum', 'IntFlag']] = {
1317
'CodeActionKind': 'StrEnum',
1418
'DocumentDiagnosticReportKind': 'StrEnum',
1519
'FailureHandlingKind': 'StrEnum',
@@ -26,46 +30,48 @@
2630
'TraceValue': 'StrEnum',
2731
'UniquenessLevel': 'StrEnum',
2832
'WatchKind': 'IntFlag',
29-
'ApplyKind': 'IntFlag'
30-
} # type: Dict[str, Literal['StrEnum', 'IntFlag']]
33+
'ApplyKind': 'IntFlag',
34+
}
3135

3236

33-
def generate(preferred_structure_kind: StructureKind, output: str) -> None:
37+
def generate(output: str) -> None:
3438
reset_new_literal_structures()
3539

36-
with open('./lsprotocol/lsp.json') as file:
37-
lsp_json: MetaModel = json.load(file)
38-
specification_version = lsp_json.get('metaData')['version']
40+
schema = Path('./lsprotocol/lsp.json').read_text(encoding='utf-8')
41+
lsp_json = cast('MetaModel', json.loads(schema))
42+
specification_version = lsp_json.get('metaData')['version']
3943

40-
content = "\n".join([
41-
"# Code generated. DO NOT EDIT.",
42-
f"# LSP v{specification_version}\n",
43-
"from typing_extensions import NotRequired",
44-
"from typing import Dict, List, Literal, TypedDict, Union",
45-
"from enum import IntEnum, IntFlag, StrEnum\n\n",
46-
"URI = str",
47-
"DocumentUri = str",
48-
"Uint = int",
49-
"RegExp = str",
50-
])
44+
content = '\n'.join(
45+
[
46+
'# ruff: noqa: E501, UP006, UP007',
47+
'# Code generated. DO NOT EDIT.',
48+
f'# LSP v{specification_version}\n',
49+
'from __future__ import annotations',
50+
'from enum import IntEnum, IntFlag, StrEnum',
51+
'from typing import Dict, List, Literal, TypedDict, Union',
52+
'from typing_extensions import NotRequired\n\n',
53+
'URI = str',
54+
'DocumentUri = str',
55+
'Uint = int',
56+
'RegExp = str',
57+
]
58+
)
5159

52-
content += '\n\n\n'
53-
content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES))
54-
content += '\n\n'
55-
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'], preferred_structure_kind))
56-
content += '\n\n\n'
57-
content += '\n\n\n'.join(generate_structures(lsp_json['structures'], preferred_structure_kind))
58-
content += '\n\n'
59-
content += '\n'.join(get_new_literal_structures())
60+
content += '\n\n\n'
61+
content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES))
62+
content += '\n\n'
63+
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases']))
64+
content += '\n\n\n'
65+
content += '\n\n\n'.join(generate_structures(lsp_json['structures']))
66+
content += '\n\n'
67+
content += '\n'.join(get_new_literal_structures())
6068

61-
# Remove trailing spaces.
62-
lines = content.split('\n')
63-
lines = [line.rstrip() for line in lines]
64-
content = '\n'.join(lines)
69+
# Remove trailing spaces.
70+
lines = content.split('\n')
71+
lines = [line.rstrip() for line in lines]
72+
content = '\n'.join(lines)
6573

66-
with open(output, "w") as new_file:
67-
new_file.write(content)
74+
Path(output).write_text(content)
6875

6976

70-
generate(preferred_structure_kind=StructureKind.Class, output="./lsp_types.py")
71-
generate(preferred_structure_kind=StructureKind.Function, output="./lsp_types_sublime_text_33.py")
77+
generate(output='./generated/lsp_types.py')

generated/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)