Skip to content

Commit 3babb53

Browse files
committed
Python generator: use Mapping instead of Dict for type annotations
Fixes #414
1 parent 2841d4a commit 3babb53

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

generator/plugins/python/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def _reset(self):
208208
self._imports: List[str] = [
209209
"import enum",
210210
"import functools",
211-
"from typing import Any, Dict, Literal, Optional, Sequence, Tuple, Union",
211+
"from typing import Any, Mapping, Literal, Optional, Sequence, Tuple, Union",
212212
"import attrs",
213213
"from . import validators",
214214
]
@@ -356,7 +356,7 @@ def _generate_type_name(
356356

357357
if type_def.kind == "map":
358358
# This kind defines a dictionary like object.
359-
return f"Dict[{self._generate_type_name(type_def.key, class_name, prefix)}, {self._generate_type_name(type_def.value, class_name, prefix)}]"
359+
return f"Mapping[{self._generate_type_name(type_def.key, class_name, prefix)}, {self._generate_type_name(type_def.value, class_name, prefix)}]"
360360

361361
if type_def.kind == "tuple":
362362
# This kind defined a tuple like object.
@@ -1154,11 +1154,11 @@ def _get_utility_code(self, lsp_model: model.LSPModel) -> List[str]:
11541154
"",
11551155
]
11561156

1157-
code_lines += ["", "ALL_TYPES_MAP: Dict[str, Union[type, object]] = {"]
1157+
code_lines += ["", "ALL_TYPES_MAP: Mapping[str, Union[type, object]] = {"]
11581158
code_lines += sorted([f"'{name}': {name}," for name in set(self._types.keys())])
11591159
code_lines += ["}", ""]
11601160

1161-
code_lines += ["_MESSAGE_DIRECTION: Dict[str, str] = {"]
1161+
code_lines += ["_MESSAGE_DIRECTION: Mapping[str, str] = {"]
11621162

11631163
code_lines += ["# Request methods"]
11641164
code_lines += sorted(

packages/python/lsprotocol/types.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import enum
1111
import functools
12-
from typing import Any, Dict, Literal, Optional, Sequence, Tuple, Union
12+
from typing import Any, Mapping, Literal, Optional, Sequence, Tuple, Union
1313
import attrs
1414
from . import validators
1515

@@ -1957,7 +1957,7 @@ class WorkspaceEdit:
19571957
cause failure of the operation. How the client recovers from the failure is described by
19581958
the client capability: `workspace.workspaceEdit.failureHandling`"""
19591959

1960-
changes: Optional[Dict[str, Sequence["TextEdit"]]] = attrs.field(default=None)
1960+
changes: Optional[Mapping[str, Sequence["TextEdit"]]] = attrs.field(default=None)
19611961
"""Holds changes to existing resources."""
19621962

19631963
document_changes: Optional[
@@ -1975,7 +1975,7 @@ class WorkspaceEdit:
19751975
only plain `TextEdit`s using the `changes` property are supported."""
19761976

19771977
change_annotations: Optional[
1978-
Dict[ChangeAnnotationIdentifier, "ChangeAnnotation"]
1978+
Mapping[ChangeAnnotationIdentifier, "ChangeAnnotation"]
19791979
] = attrs.field(default=None)
19801980
"""A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and
19811981
delete file / folder operations.
@@ -2454,7 +2454,7 @@ class DocumentDiagnosticReportPartialResult:
24542454

24552455
# Since: 3.17.0
24562456

2457-
related_documents: Dict[
2457+
related_documents: Mapping[
24582458
str, Union["FullDocumentDiagnosticReport", "UnchangedDocumentDiagnosticReport"]
24592459
] = attrs.field()
24602460

@@ -5644,7 +5644,7 @@ class RelatedFullDocumentDiagnosticReport:
56445644
"""The actual items."""
56455645

56465646
related_documents: Optional[
5647-
Dict[
5647+
Mapping[
56485648
str,
56495649
Union[FullDocumentDiagnosticReport, "UnchangedDocumentDiagnosticReport"],
56505650
]
@@ -5705,7 +5705,7 @@ class RelatedUnchangedDocumentDiagnosticReport:
57055705
diagnostic request for the same document."""
57065706

57075707
related_documents: Optional[
5708-
Dict[
5708+
Mapping[
57095709
str, Union[FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport]
57105710
]
57115711
] = attrs.field(default=None)
@@ -13236,7 +13236,7 @@ def is_special_property(cls: type, property_name: str) -> bool:
1323613236
return qualified_name in _SPECIAL_PROPERTIES
1323713237

1323813238

13239-
ALL_TYPES_MAP: Dict[str, Union[type, object]] = {
13239+
ALL_TYPES_MAP: Mapping[str, Union[type, object]] = {
1324013240
"AnnotatedTextEdit": AnnotatedTextEdit,
1324113241
"ApplyKind": ApplyKind,
1324213242
"ApplyWorkspaceEditParams": ApplyWorkspaceEditParams,
@@ -13901,7 +13901,7 @@ def is_special_property(cls: type, property_name: str) -> bool:
1390113901
"_InitializeParams": _InitializeParams,
1390213902
}
1390313903

13904-
_MESSAGE_DIRECTION: Dict[str, str] = {
13904+
_MESSAGE_DIRECTION: Mapping[str, str] = {
1390513905
# Request methods
1390613906
CALL_HIERARCHY_INCOMING_CALLS: "clientToServer",
1390713907
CALL_HIERARCHY_OUTGOING_CALLS: "clientToServer",

0 commit comments

Comments
 (0)