1
+ from sys import version_info
2
+
1
3
from pydbus import xml_generator
2
4
from pydbus .generic import signal
5
+ from pydbus .strong_typing import typed_method , typed_property
3
6
4
7
5
8
@xml_generator .attach_introspection_xml
@@ -9,11 +12,20 @@ class Example(object):
9
12
def __init__ (self ):
10
13
self ._rw = 42
11
14
15
+ @typed_method (("s" , ), "i" )
12
16
def OneParamReturn (self , parameter ):
13
17
return 42
14
18
19
+ @typed_method (("s" , ), None )
20
+ def OneParamNoReturn (self , parameter ):
21
+ pass
22
+
23
+ @typed_property ("i" )
24
+ def ReadProperty (self ):
25
+ return 42
26
+
15
27
@xml_generator .emits_changed_signal
16
- @property
28
+ @typed_property ( "i" )
17
29
def RwProperty (self ):
18
30
return self ._rw
19
31
@@ -53,20 +65,43 @@ def arguments(self, arg1, arg2):
53
65
def ctx_argument (self , arg , dbus_context ):
54
66
pass
55
67
56
- assert xml_generator .get_arguments (nothing ) == []
57
- assert xml_generator .get_arguments (arguments ) == ["arg1" , "arg2" ]
58
- assert xml_generator .get_arguments (ctx_argument ) == ["arg" ]
68
+ @typed_method (tuple (), None )
69
+ def typed_nothing (self ):
70
+ pass
71
+
72
+ @typed_method (("s" , "i" ), None )
73
+ def typed_arguments (self , arg1 , arg2 ):
74
+ pass
75
+
76
+ assert xml_generator .get_arguments (nothing ) == (tuple (), None )
77
+ assert xml_generator .get_arguments (arguments ) == ((("arg1" , None ), ("arg2" , None )), None )
78
+ assert xml_generator .get_arguments (ctx_argument ) == ((("arg" , None ), ), None )
79
+
80
+ assert xml_generator .get_arguments (typed_nothing ) == (tuple (), None )
81
+ assert xml_generator .get_arguments (typed_arguments ) == ((("arg1" , "s" ), ("arg2" , "i" )), None )
59
82
60
83
61
84
def test_valid ():
62
85
assert not hasattr (Example .OneParamReturn , "dbus_interface" )
86
+ assert Example .OneParamReturn .arg_types == ("s" , )
87
+ assert Example .OneParamReturn .ret_type == "i"
88
+
89
+ assert not hasattr (Example .OneParamNoReturn , "dbus_interface" )
90
+ assert Example .OneParamNoReturn .arg_types == ("s" , )
91
+ assert Example .OneParamNoReturn .ret_type is None
92
+
93
+ assert not hasattr (Example .ReadProperty , "dbus_interface" )
94
+ assert isinstance (Example .ReadProperty , property )
95
+ assert Example .ReadProperty .fget .prop_type == "i"
96
+ assert Example .ReadProperty .fset is None
63
97
64
98
assert not hasattr (Example .RwProperty , "dbus_interface" )
65
99
assert isinstance (Example .RwProperty , property )
66
100
assert Example .RwProperty .fget .causes_signal is True
101
+ assert Example .RwProperty .fget .prop_type == "i"
67
102
assert Example .RwProperty .fset is not None
68
103
69
- assert Example .dbus == b'<node><interface name="net.lvht.Foo1"><method name="OneParamReturn"><arg direction="in" name="parameter" /></ method><property access="readwrite" name="RwProperty"><annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true" /></property></interface></node>'
104
+ assert Example .dbus == b'<node><interface name="net.lvht.Foo1"><method name="OneParamNoReturn"><arg direction="in" name="parameter" type="s" /></method><method name=" OneParamReturn"><arg direction="in" name="parameter" type="s" /><arg direction="out" name="return" type="i" /></ method><property access="read" name="ReadProperty" type="i" /><property access=" readwrite" name="RwProperty" type="i "><annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true" /></property></interface></node>'
70
105
71
106
72
107
def test_multiple_interfaces ():
@@ -89,8 +124,13 @@ def Dummy(self, param=None):
89
124
except ValueError as e :
90
125
assert str (e ) == "Default values are not allowed for method 'Dummy'"
91
126
92
- E_NO_VARGS = (
127
+ if version_info [0 ] == 2 :
128
+ E_NO_VARGS = (
93
129
"Variable arguments arguments are not allowed for method 'Dummy'" )
130
+ else :
131
+ E_NO_VARGS = E_NO_KWARGS = (
132
+ "Variable arguments or keyword only arguments are not allowed for "
133
+ "method 'Dummy'" )
94
134
95
135
def Dummy (self , * vargs ):
96
136
pass
@@ -111,7 +151,42 @@ def Dummy(self, **kwargs):
111
151
assert str (e ) == E_NO_VARGS
112
152
113
153
154
+ def test_require_strong_typing ():
155
+ try :
156
+ @xml_generator .attach_introspection_xml (True )
157
+ @xml_generator .interface ("net.lvht.Foo1" )
158
+ class Example (object ):
159
+
160
+ def Dummy (self , param ):
161
+ pass
162
+ except ValueError as e :
163
+ assert str (e ) == "No argument types defined for method 'Dummy'"
164
+
165
+ @xml_generator .attach_introspection_xml (True )
166
+ @xml_generator .interface ("net.lvht.Foo1" )
167
+ class RequiredExample (object ):
168
+
169
+ @typed_method (("s" , ), None )
170
+ def Dummy (self , param ):
171
+ pass
172
+
173
+ assert RequiredExample .Dummy .arg_types == ("s" , )
174
+ assert RequiredExample .Dummy .ret_type is None
175
+
176
+ @xml_generator .attach_introspection_xml (False )
177
+ @xml_generator .interface ("net.lvht.Foo1" )
178
+ class OptionalExample (object ):
179
+
180
+ @typed_method (("s" , ), None )
181
+ def Dummy (self , param ):
182
+ pass
183
+
184
+ assert OptionalExample .dbus == RequiredExample .dbus
185
+ assert OptionalExample is not RequiredExample
186
+
187
+
114
188
test_get_arguments ()
115
189
test_valid ()
116
190
test_multiple_interfaces ()
117
191
test_invalid_function ()
192
+ test_require_strong_typing ()
0 commit comments