File: test_weak_callable.py

package info (click to toggle)
psygnal 0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 636 kB
  • sloc: python: 7,604; makefile: 8
file content (239 lines) | stat: -rw-r--r-- 6,169 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import gc
import re
from functools import partial
from typing import Any
from unittest.mock import Mock
from weakref import ref

import pytest

from psygnal import SignalInstance
from psygnal._weak_callback import WeakCallback, weak_callback


@pytest.mark.parametrize(
    "type_",
    [
        "function",
        "toolz_function",
        "weak_func",
        "lambda",
        "method",
        "partial_method",
        "toolz_method",
        "setattr",
        "setitem",
        "mock",
        "weak_cb",
        "print",
    ],
)
def test_slot_types(type_: str, capsys: Any) -> None:
    mock = Mock()
    final_mock = Mock()

    class MyObj:
        def method(self, x: int) -> None:
            mock(x)
            return x

        def __setitem__(self, key, value):
            mock(value)
            return value

        def __setattr__(self, __name: str, __value) -> None:
            if __name == "x":
                mock(__value)
                return __value

    obj = MyObj()

    if type_ == "setattr":
        cb = weak_callback(setattr, obj, "x", finalize=final_mock)
    elif type_ == "setitem":
        cb = weak_callback(obj.__setitem__, "x", finalize=final_mock)
    elif type_ in {"function", "weak_func"}:

        def obj(x: int) -> None:
            mock(x)
            return x

        cb = weak_callback(obj, strong_func=(type_ == "function"), finalize=final_mock)
    elif type_ == "toolz_function":
        toolz = pytest.importorskip("toolz")

        @toolz.curry
        def obj(z: int, x: int) -> None:
            mock(x)
            return x

        cb = weak_callback(obj(5), finalize=final_mock)
    elif type_ == "lambda":
        cb = weak_callback(lambda x: mock(x) and x, finalize=final_mock)
    elif type_ == "method":
        cb = weak_callback(obj.method, finalize=final_mock)
    elif type_ == "partial_method":
        cb = weak_callback(partial(obj.method, 2), max_args=0, finalize=final_mock)
    elif type_ == "toolz_method":
        toolz = pytest.importorskip("toolz")
        cb = weak_callback(toolz.curry(obj.method, 2), max_args=0, finalize=final_mock)
    elif type_ == "mock":
        cb = weak_callback(mock, finalize=final_mock)
    elif type_ == "weak_cb":
        cb = weak_callback(obj.method, finalize=final_mock)
        cb = weak_callback(cb, finalize=final_mock)
    elif type_ == "print":
        cb = weak_callback(print, finalize=final_mock)

    assert isinstance(cb, WeakCallback)
    assert isinstance(cb.slot_repr(), str)
    cb.cb((2,))
    assert cb.dereference() is not None
    if type_ == "print":
        assert capsys.readouterr().out == "2\n"
        return

    mock.assert_called_once_with(2)
    mock.reset_mock()
    result = cb(2)
    if type_ not in ("setattr", "mock"):
        assert result == 2
    mock.assert_called_once_with(2)

    del obj

    if type_ not in ("function", "toolz_function", "lambda", "mock"):
        final_mock.assert_called_once_with(cb)
        assert cb.dereference() is None
        with pytest.raises(ReferenceError):
            cb.cb((2,))
        with pytest.raises(ReferenceError):
            cb(2)
    else:
        cb.cb((4,))
        mock.assert_called_with(4)


def test_weak_callable_equality() -> None:
    """Slot callers should be equal only if they represent the same bound-method."""

    class T:
        def x(self): ...

    t1 = T()
    t2 = T()
    t1_ref = ref(t1)
    t2_ref = ref(t2)

    bmt1_a = weak_callback(t1.x)
    bmt1_b = weak_callback(t1.x)
    bmt2_a = weak_callback(t2.x)
    bmt2_b = weak_callback(t2.x)

    assert bmt1_a != "not a weak callback"

    def _assert_equality() -> None:
        assert bmt1_a == bmt1_b
        assert bmt2_a == bmt2_b
        assert bmt1_a != bmt2_a
        assert bmt1_b != bmt2_b

    _assert_equality()
    del t1
    gc.collect()
    assert t1_ref() is None
    _assert_equality()
    del t2
    gc.collect()
    assert t2_ref() is None
    _assert_equality()


def test_nonreferencable() -> None:
    class T:
        __slots__ = ("x",)

        def method(self) -> None: ...

    t = T()
    with pytest.warns(UserWarning, match="failed to create weakref"):
        cb = weak_callback(t.method)
        assert cb.dereference() == t.method

    with pytest.raises(TypeError):
        weak_callback(t.method, on_ref_error="raise")

    cb = weak_callback(t.method, on_ref_error="ignore")
    assert cb.dereference() == t.method


@pytest.mark.parametrize("strong", [True, False])
def test_deref(strong: bool) -> None:
    def func(x): ...

    p = partial(func, 1)
    cb = weak_callback(p, strong_func=strong)
    dp = cb.dereference()

    assert dp.func is p.func
    assert dp.args == p.args
    assert dp.keywords == p.keywords


def test_queued_callbacks() -> None:
    from psygnal._queue import QueuedCallback

    def func(x):
        return x

    cb = weak_callback(func)
    qcb = QueuedCallback(cb, thread="current")

    assert qcb.dereference() is func
    assert qcb(1) == 1


def test_cb_raises() -> None:
    from psygnal import EmitLoopError

    sig = SignalInstance((int,), name="sig")

    class T:
        @property
        def x(self) -> int:
            return 1

        @x.setter
        def x(self, value: int) -> None:
            1 / value

        def __setitem__(self, key: str, value: int) -> Any:
            1 / value

        def method(self, x: int) -> None:
            1 / x

    t = T()

    sig.connect(t.method)
    error_re = re.compile(
        f"emitting signal.*'sig'.*{re.escape(__file__)}.*method", re.DOTALL
    )
    with pytest.raises(EmitLoopError, match=error_re):
        sig.emit("a")
    sig.disconnect(t.method)

    sig.connect_setattr(t, "x", maxargs=1)
    error_re = re.compile(
        f"emitting signal.*'sig'.*{re.escape(__file__)}.*x", re.DOTALL
    )
    with pytest.raises(EmitLoopError, match=error_re):
        sig.emit("a")
    sig.disconnect_setattr(t, "x")

    sig.connect_setitem(t, "x", maxargs=1)
    error_re = re.compile(
        f"emitting signal.*'sig'.*{re.escape(__file__)}.*__setitem__", re.DOTALL
    )
    with pytest.raises(EmitLoopError, match=error_re):
        sig.emit("a")