File: test_partial_and_macros.py

package info (click to toggle)
python-makefun 1.15.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 440 kB
  • sloc: python: 2,384; makefile: 2
file content (213 lines) | stat: -rw-r--r-- 5,430 bytes parent folder | download
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
import functools
import pytest
import re
import sys

import makefun
try:
    from inspect import signature
except ImportError:
    from funcsigs import signature


PY2 = sys.version_info < (3, )

# Python 3.13 dedents docstrings, earlier versions just strip initial
# whitespace.  Use a regexp to get a consistently dedented docstring
# for comparison across Python versions.
DOCSTRING_NORMALIZE_RE = re.compile(r"^ +", re.MULTILINE)


def test_doc():
    def foo(x, y):
        """
        a `foo` function

        :param x:
        :param y:
        :return:
        """
        return x + y

    ref_bar = functools.partial(foo, x=12)

    ref_sig_str = "(x=12, y)" if PY2 else "(*, x=12, y)"
    assert str(signature(ref_bar)) == ref_sig_str

    bar = makefun.partial(foo, x=12)

    # same behaviour - except in python 2 where our "KW_ONLY_ARG!" appear
    assert str(signature(bar)).replace("=KW_ONLY_ARG!", "") == str(signature(ref_bar))

    bar.__name__ = 'bar'
    help(bar)
    with pytest.raises(TypeError):
        bar(1)
    assert bar(y=1) == 13

    sig_actual_call = ref_sig_str.replace("*, ", "")

    assert DOCSTRING_NORMALIZE_RE.sub("", bar.__doc__) \
           == """<This function is equivalent to 'foo%s', see original 'foo' doc below.>

a `foo` function

:param x:
:param y:
:return:
""" % sig_actual_call


def test_partial():
    """Tests that `with_partial` works"""

    @makefun.with_partial(y='hello')
    def foo(x, y, a):
        """
        a `foo` function

        :param x:
        :param y:
        :param a:
        :return:
        """
        print(a)
        print(x, y)

    if not PY2:
        # true keyword-only
        with pytest.raises(TypeError):
            foo(1, 2)

    foo(1, a=2)
    help(foo)

    sig_actual_call = "(x, y='hello', a)"  # if PY2 else "(x, *, y='hello', a)"

    assert DOCSTRING_NORMALIZE_RE.sub("", foo.__doc__.replace("=KW_ONLY_ARG!", "")) \
           == """<This function is equivalent to 'foo%s', see original 'foo' doc below.>

a `foo` function

:param x:
:param y:
:param a:
:return:
""" % sig_actual_call


def test_issue_57():
    def f(b=0):
        """hey"""
        return b

    f.i = 1

    # creating the decorator
    dec = makefun.wraps(functools.partial(f, b=2), func_name='foo')

    # applying the decorator
    n = dec(functools.partial(f, b=1))

    # check metadata
    assert n.i == 1
    # check signature
    sig_actual_call = "(b=2)"
    # sig = sig_actual_call if PY2 else "(*, b=2)"

    assert n.__doc__ == """<This function is equivalent to 'f%s', see original 'f' doc below.>
hey""" % sig_actual_call
    # check implementation: the default value from the signature (from @wraps) is the one that applies here
    assert n() == 2


def test_create_with_partial():
    def f(b=0):
        """hey"""
        return b

    f.i = 1

    m = makefun.create_function("(b=-1)", functools.partial(f, b=2), **f.__dict__)
    assert str(signature(m)) == "(b=-1)"
    assert m() == -1
    assert m.i == 1
    # the doc remains untouched in create_function as opposed to wraps, this is normal
    assert m.__doc__ == functools.partial.__doc__


def test_args_order_and_kind():
    """Make sure that the order remains ok"""

    def f(a, b, c, **d):
        return a + b + c + sum(d)

    # reference: functools.partial
    fp_ref = functools.partial(f, b=0)

    # except in python 2, all kwargs following the predefined arg become kw-only
    if sys.version_info < (3,):
        assert str(signature(fp_ref)) == "(a, b=0, c, **d)"
    else:
        assert str(signature(fp_ref)) == "(a, *, b=0, c, **d)"

    # our makefun.partial
    fp = makefun.partial(f, b=0)

    # same behaviour - except in python 2 where our "KW_ONLY_ARG!" appear
    assert str(signature(fp_ref)) == str(signature(fp)).replace("=KW_ONLY_ARG!", "")

    # positional-only behaviour
    if sys.version_info >= (3, 8):
        from ._test_py38 import make_pos_only_f
        f = make_pos_only_f()

        # it is possible to keyword-partialize a positional-only argument...
        fp_ref = functools.partial(f, b=0)

        # but 'signature' does not support it before Python 3.12.4 !
        if sys.version_info < (3, 12, 4):
            with pytest.raises(ValueError):
                signature(fp_ref)
        else:
            assert str(signature(fp_ref)) == "(a, c, /, *, d, **e)"

        # TODO https://github.com/smarie/python-makefun/issues/107
        # so we do not support it
        with pytest.raises(NotImplementedError):
            makefun.partial(f, b=0)

        # assert str(signature(fp_ref)) == str(signature(fp))


@pytest.mark.parametrize("is_generator", [False, True])
def test_simple_partial_copy(is_generator):
    """Test that when not providing any argument to partial, it is equivalent to wraps with new sig = None

    This test was extended to cover issue 79.
    """

    if is_generator:
        def f1(a):
            yield a + 1
    else:
        def f1(a):
            return a + 1

    f2 = makefun.partial(f1)

    # make sure that this is the same as wraps
    # and same for the func attribute
    assert f2.func == f2.__wrapped__ == f1

    f3 = makefun.wraps(f1)(f1)
    assert f3.__wrapped__ == f1

    if is_generator:
        assert next(f2(1)) == next(f3(1)) == 2
    else:
        assert f2(1) == f3(1) == 2

    # the func attribute is there too
    f4 = functools.partial(f1)
    assert f2.func == f4.func