File: test_typing.py

package info (click to toggle)
python-sdbus 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 996 kB
  • sloc: python: 7,911; ansic: 2,507; makefile: 9; sh: 4
file content (167 lines) | stat: -rw-r--r-- 4,389 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# SPDX-License-Identifier: LGPL-2.1-or-later

# Copyright (C) 2024 igo95862

# This file is part of python-sdbus

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
from __future__ import annotations

from sdbus import (
    DbusInterfaceCommon,
    DbusInterfaceCommonAsync,
    dbus_method,
    dbus_method_async,
    dbus_property,
    dbus_property_async,
    dbus_signal_async,
)


class TestTypingBlocking(
    DbusInterfaceCommon,
    interface_name="example.com",
):

    @dbus_method(result_signature="as")
    def get_str_list_method(self) -> list[str]:
        raise NotImplementedError

    @dbus_property("as")
    def str_list_property(self) -> list[str]:
        raise NotImplementedError


# These functions are not meant to be executed
# but exist to be type checked.

def check_blocking_interface_method_typing(
    test_interface: TestTypingBlocking,
) -> None:

    should_be_list = test_interface.get_str_list_method()
    should_be_list.append("test")

    for x in should_be_list:
        x.capitalize()


def check_blocking_interface_property_typing(
    test_interface: TestTypingBlocking,
) -> None:

    should_be_list = test_interface.str_list_property
    should_be_list.append("test")

    for x in should_be_list:
        x.capitalize()

    test_interface.str_list_property = ["test", "foobar"]


class TestTypingAsync(
    DbusInterfaceCommonAsync,
    interface_name="example.com",
):

    @dbus_method_async(result_signature="as")
    async def get_str_list_method(self) -> list[str]:
        raise NotImplementedError

    @dbus_property_async("as")
    def str_list_property(self) -> list[str]:
        raise NotImplementedError

    @dbus_signal_async("as")
    def str_list_signal(self) -> list[str]:
        raise NotImplementedError


async def check_async_interface_method_typing(
    test_interface: TestTypingAsync,
) -> None:

    should_be_list = await test_interface.get_str_list_method()
    should_be_list.append("test")

    for x in should_be_list:
        x.capitalize()


async def check_async_interface_property_typing(
    test_interface: TestTypingAsync,
) -> None:

    should_be_list = await test_interface.str_list_property
    should_be_list.append("test")

    for x in should_be_list:
        x.capitalize()

    should_be_list2 = await test_interface.str_list_property.get_async()
    should_be_list2.append("test")

    for x in should_be_list2:
        x.capitalize()

    await test_interface.str_list_property.set_async(["test", "foobar"])


async def check_async_interface_signal_typing(
    test_interface: TestTypingAsync,
) -> None:

    async for ls in test_interface.str_list_signal:
        ls.append("test")
        for x in ls:
            x.capitalize()

    async for ls2 in test_interface.str_list_signal.catch():
        ls2.append("test")
        for x2 in ls2:
            x2.capitalize()

    async for p1, ls3 in test_interface.str_list_signal.catch_anywhere():
        p1.capitalize()
        ls3.append("test")
        for x3 in ls3:
            x3.capitalize()

    async for p2, ls4 in (
        TestTypingAsync.str_list_signal
        .catch_anywhere("org.example")
    ):
        p2.capitalize()
        ls4.append("test")
        for x4 in ls4:
            x4.capitalize()

    test_interface.str_list_signal.emit(["test", "foobar"])


async def check_async_element_class_access_typing() -> None:

    test_list: list[str] = []

    # TODO: Fix dbus async method typing
    # test_list.append(
    #     TestTypingAsync.get_str_list_method.method_name
    # )
    test_list.append(
        TestTypingAsync.str_list_property.property_name
    )
    test_list.append(
        TestTypingAsync.str_list_signal.signal_name
    )