Skip to content

Commit 2ef8d86

Browse files
authored
Change LSPArray and LSPObject types to be more lax (#30)
1 parent 3444db9 commit 2ef8d86

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

generate.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
'ApplyKind': 'IntFlag',
3434
}
3535

36+
ALIAS_OVERRIDES: dict[str, str] = {
37+
'LSPArray': "Sequence['LSPAny']",
38+
'LSPObject': 'Mapping[str, Any]'
39+
}
40+
3641

3742
def generate(output: str) -> None:
3843
reset_new_literal_structures()
@@ -48,7 +53,7 @@ def generate(output: str) -> None:
4853
f'# LSP v{specification_version}\n',
4954
'from __future__ import annotations',
5055
'from enum import IntEnum, IntFlag, StrEnum',
51-
'from typing import Dict, List, Literal, TypedDict, Union',
56+
'from typing import Any, Dict, List, Literal, Mapping, Sequence, TypedDict, Union',
5257
'from typing_extensions import NotRequired\n\n',
5358
'URI = str',
5459
'DocumentUri = str',
@@ -60,7 +65,7 @@ def generate(output: str) -> None:
6065
content += '\n\n\n'
6166
content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES))
6267
content += '\n\n'
63-
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases']))
68+
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'], ALIAS_OVERRIDES))
6469
content += '\n\n\n'
6570
content += '\n\n\n'.join(generate_structures(lsp_json['structures']))
6671
content += '\n\n'

generated/lsp_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66
from enum import IntEnum, IntFlag, StrEnum
7-
from typing import Dict, List, Literal, TypedDict, Union
7+
from typing import Any, Dict, List, Literal, Mapping, Sequence, TypedDict, Union
88
from typing_extensions import NotRequired
99

1010

@@ -865,7 +865,7 @@ class TokenFormat(StrEnum):
865865
the defining symbol
866866
"""
867867

868-
LSPArray = List['LSPAny']
868+
LSPArray = Sequence['LSPAny']
869869
"""
870870
LSP arrays.
871871
@since 3.17.0
@@ -970,7 +970,7 @@ class TokenFormat(StrEnum):
970970
@since 3.17.0 - support for NotebookCellTextDocumentFilter.
971971
"""
972972

973-
LSPObject = Dict[str, 'LSPAny']
973+
LSPObject = Mapping[str, Any]
974974
"""
975975
LSP object definition.
976976
@since 3.17.0
@@ -6079,7 +6079,7 @@ class ParameterInformation(TypedDict):
60796079
have a label and a doc-comment.
60806080
"""
60816081

6082-
label: Union[str, tuple[Uint, Uint]]
6082+
label: Union[str, list[Uint]]
60836083
"""
60846084
The label of this parameter information.
60856085

utils/generate_type_aliases.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
from lsp_schema import TypeAlias
77

88

9-
def generate_type_aliases(type_aliases: list[TypeAlias]) -> list[str]:
9+
def generate_type_aliases(type_aliases: list[TypeAlias], overrides: dict[str, str]) -> list[str]:
1010
def to_string(type_alias: TypeAlias) -> str:
1111
symbol_name = type_alias['name']
1212
documentation = format_comment(type_alias.get('documentation'))
13-
value = format_type(type_alias['type'], {'root_symbol_name': symbol_name}, StructureKind.Class)
13+
if symbol_name in overrides:
14+
value = overrides[symbol_name]
15+
else:
16+
value = format_type(type_alias['type'], {'root_symbol_name': symbol_name}, StructureKind.Class)
1417
result = f"""
1518
{symbol_name} = {value}"""
1619
if documentation:

utils/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def format_type(typ: EveryType, context: FormatTypeContext, preferred_structure_
9090
return f'Union[{", ".join(union)}]'
9191
elif typ['kind'] == 'tuple':
9292
union = [format_type(item, context, preferred_structure_kind) for item in typ['items']]
93-
return f'tuple[{", ".join(union)}]'
93+
return f'list[{" | ".join(set(union))}]'
9494
elif typ['kind'] == 'literal':
9595
if not typ['value']['properties']:
9696
return 'Dict[str, LSPAny]'

0 commit comments

Comments
 (0)