Skip to content

Commit fd98d9b

Browse files
committed
Use Python 3.9 generic collections type hints in generated code
Since Python 3.9 is now a minimal version the `list`, `dict` and `tuple` type hints can now be used instead of `typing.List`...
1 parent 227ac90 commit fd98d9b

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/sdbus/interface_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def typing_basic(cls, char: str) -> str:
136136

137137
@staticmethod
138138
def typing_into_tuple(typing_iter: Iterable[str]) -> str:
139-
return f"Tuple[{', '.join(typing_iter)}]"
139+
return f"tuple[{', '.join(typing_iter)}]"
140140

141141
@staticmethod
142142
def slice_container(dbus_sig_iter: Iterator[str], peek_str: str) -> str:
@@ -219,7 +219,7 @@ def typing_complete(cls, complete_sig: str) -> str:
219219
dict_value_sig = complete_sig[3:-1]
220220
dict_value_typing = cls.typing_complete(dict_value_sig)
221221

222-
return f"Dict[{dict_key_typing}, {dict_value_typing}]"
222+
return f"dict[{dict_key_typing}, {dict_value_typing}]"
223223

224224
elif complete_sig.startswith('a'):
225225
array_completes = cls.split_sig(complete_sig[1:])
@@ -230,7 +230,7 @@ def typing_complete(cls, complete_sig: str) -> str:
230230

231231
array_single_complete = array_completes[0]
232232

233-
return f"List[{cls.typing_complete(array_single_complete)}]"
233+
return f"list[{cls.typing_complete(array_single_complete)}]"
234234
elif complete_sig.startswith('('):
235235
if complete_sig[-1] != ')':
236236
raise ValueError(f"Malformed struct {complete_sig}")
@@ -618,7 +618,7 @@ def has_members(self) -> bool:
618618
"generic_header": """\
619619
from __future__ import annotations
620620
621-
from typing import Any, Dict, List, Tuple
621+
from typing import Any
622622
623623
""",
624624
"async_imports_header": """from sdbus import (

test/test_interface_generator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def test_signature_to_typing(self) -> None:
112112

113113
with self.subTest('Parse variant'):
114114
self.assertEqual(
115-
'Tuple[str, Any]', DbusSigToTyping.typing_complete('v')
115+
'tuple[str, Any]', DbusSigToTyping.typing_complete('v')
116116
)
117117

118118
with self.subTest('Splitter test'):
@@ -124,40 +124,40 @@ def test_signature_to_typing(self) -> None:
124124
with self.subTest('Parse struct'):
125125
self.assertEqual(
126126
DbusSigToTyping.typing_complete('(sx)'),
127-
'Tuple[str, int]',
127+
'tuple[str, int]',
128128
)
129129

130130
with self.subTest('Parse list'):
131131
self.assertEqual(
132132
DbusSigToTyping.typing_complete('a(sx)'),
133-
'List[Tuple[str, int]]',
133+
'list[tuple[str, int]]',
134134
)
135135

136136
with self.subTest('Parse dict'):
137137
self.assertEqual(
138138
DbusSigToTyping.typing_complete('a{s(xh)}'),
139-
'Dict[str, Tuple[int, int]]',
139+
'dict[str, tuple[int, int]]',
140140
)
141141

142142
with self.subTest('Parse signature'):
143143
self.assertEqual(
144144
DbusSigToTyping.sig_to_typing('a{s(xh)}'),
145-
'Dict[str, Tuple[int, int]]',
145+
'dict[str, tuple[int, int]]',
146146
)
147147

148148
self.assertEqual(
149149
DbusSigToTyping.sig_to_typing('a{s(xh)}xs'),
150-
'Tuple[Dict[str, Tuple[int, int]], int, str]',
150+
'tuple[dict[str, tuple[int, int]], int, str]',
151151
)
152152

153153
self.assertEqual(
154154
DbusSigToTyping.sig_to_typing('a{s(xh)}xs'),
155-
'Tuple[Dict[str, Tuple[int, int]], int, str]',
155+
'tuple[dict[str, tuple[int, int]], int, str]',
156156
)
157157

158158
self.assertEqual(
159159
DbusSigToTyping.sig_to_typing('as'),
160-
'List[str]',
160+
'list[str]',
161161
)
162162

163163
self.assertEqual(

0 commit comments

Comments
 (0)