From 6346a2abc5b79b18d2d5eb96136fd8a43f884a12 Mon Sep 17 00:00:00 2001 From: Linus Heckemann Date: Tue, 25 Mar 2025 09:58:24 +0100 Subject: [PATCH] Python generator: use Mapping instead of Dict for type annotations Fixes #414 --- generator/plugins/python/utils.py | 4 ++-- packages/python/lsprotocol/types.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/generator/plugins/python/utils.py b/generator/plugins/python/utils.py index 93c7db1..1700ede 100644 --- a/generator/plugins/python/utils.py +++ b/generator/plugins/python/utils.py @@ -208,7 +208,7 @@ def _reset(self): self._imports: List[str] = [ "import enum", "import functools", - "from typing import Any, Dict, Literal, Optional, Sequence, Tuple, Union", + "from typing import Any, Dict, Mapping, Literal, Optional, Sequence, Tuple, Union", "import attrs", "from . import validators", ] @@ -356,7 +356,7 @@ def _generate_type_name( if type_def.kind == "map": # This kind defines a dictionary like object. - return f"Dict[{self._generate_type_name(type_def.key, class_name, prefix)}, {self._generate_type_name(type_def.value, class_name, prefix)}]" + return f"Mapping[{self._generate_type_name(type_def.key, class_name, prefix)}, {self._generate_type_name(type_def.value, class_name, prefix)}]" if type_def.kind == "tuple": # This kind defined a tuple like object. diff --git a/packages/python/lsprotocol/types.py b/packages/python/lsprotocol/types.py index f02fe2f..d20c539 100644 --- a/packages/python/lsprotocol/types.py +++ b/packages/python/lsprotocol/types.py @@ -9,7 +9,7 @@ import enum import functools -from typing import Any, Dict, Literal, Optional, Sequence, Tuple, Union +from typing import Any, Dict, Mapping, Literal, Optional, Sequence, Tuple, Union import attrs from . import validators @@ -1957,7 +1957,7 @@ class WorkspaceEdit: cause failure of the operation. How the client recovers from the failure is described by the client capability: `workspace.workspaceEdit.failureHandling`""" - changes: Optional[Dict[str, Sequence["TextEdit"]]] = attrs.field(default=None) + changes: Optional[Mapping[str, Sequence["TextEdit"]]] = attrs.field(default=None) """Holds changes to existing resources.""" document_changes: Optional[ @@ -1975,7 +1975,7 @@ class WorkspaceEdit: only plain `TextEdit`s using the `changes` property are supported.""" change_annotations: Optional[ - Dict[ChangeAnnotationIdentifier, "ChangeAnnotation"] + Mapping[ChangeAnnotationIdentifier, "ChangeAnnotation"] ] = attrs.field(default=None) """A map of change annotations that can be referenced in `AnnotatedTextEdit`s or create, rename and delete file / folder operations. @@ -2454,7 +2454,7 @@ class DocumentDiagnosticReportPartialResult: # Since: 3.17.0 - related_documents: Dict[ + related_documents: Mapping[ str, Union["FullDocumentDiagnosticReport", "UnchangedDocumentDiagnosticReport"] ] = attrs.field() @@ -5644,7 +5644,7 @@ class RelatedFullDocumentDiagnosticReport: """The actual items.""" related_documents: Optional[ - Dict[ + Mapping[ str, Union[FullDocumentDiagnosticReport, "UnchangedDocumentDiagnosticReport"], ] @@ -5705,7 +5705,7 @@ class RelatedUnchangedDocumentDiagnosticReport: diagnostic request for the same document.""" related_documents: Optional[ - Dict[ + Mapping[ str, Union[FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport] ] ] = attrs.field(default=None)