File: test_pybind.py

package info (click to toggle)
taskflow 3.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 45,948 kB
  • sloc: cpp: 39,058; xml: 35,572; python: 12,935; javascript: 1,732; makefile: 59; sh: 16
file content (426 lines) | stat: -rw-r--r-- 19,828 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#
#   This file is part of m.css.
#
#   Copyright © 2017, 2018, 2019, 2020, 2021, 2022, 2023
#             Vladimír Vondruš <mosra@centrum.cz>
#   Copyright © 2020 Sergei Izmailov <sergei.a.izmailov@gmail.com>
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
#   DEALINGS IN THE SOFTWARE.
#

import copy
import sys
import unittest

from python import State, parse_pybind_signature, default_config

from . import BaseInspectTestCase

class Signature(unittest.TestCase):
    # make_type_link() needs state.config['INPUT_MODULES'], simply supply
    # everything there
    state = State(copy.deepcopy(default_config))

    def test(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, a2: module.Thing) -> module.Thing3'),
            ('foo', '', [
                ('a', 'int', 'int', None),
                ('a2', 'module.Thing', 'module.Thing', None),
            ], 'module.Thing3', 'module.Thing3'))

    def test_newline(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, a2: module.Thing) -> module.Thing3\n'),
            ('foo', '', [
                ('a', 'int', 'int', None),
                ('a2', 'module.Thing', 'module.Thing', None),
            ], 'module.Thing3', 'module.Thing3'))

    def test_docs(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, a2: module.Thing) -> module.Thing3\n\nDocs here!!'),
            ('foo', 'Docs here!!', [
                ('a', 'int', 'int', None),
                ('a2', 'module.Thing', 'module.Thing', None),
            ], 'module.Thing3', 'module.Thing3'))

    def test_no_args(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'thingy() -> str'),
            ('thingy', '', [], 'str', 'str'))

    def test_no_return(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            '__init__(self: module.Thing)'),
            ('__init__', '', [
                ('self', 'module.Thing', 'module.Thing', None),
            ], None, None))

    def test_none_return(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            '__init__(self: module.Thing) -> None'),
            ('__init__', '', [
                ('self', 'module.Thing', 'module.Thing', None),
            ], 'None', 'None'))

    def test_no_arg_types(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'thingy(self, the_other_thing)'),
            ('thingy', '', [
                ('self', None, None, None),
                ('the_other_thing', None, None, None),
            ], None, None))

    def test_none_arg_types(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'thingy(self, the_other_thing: Callable[[], None])'),
            ('thingy', '', [
                ('self', None, None, None),
                ('the_other_thing', 'typing.Callable[[], None]', 'typing.Callable[[], None]', None),
            ], None, None))

    def test_square_brackets(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Tuple[int, str], no_really: str) -> List[str]'),
            ('foo', '', [
                ('a', 'typing.Tuple[int, str]', 'typing.Tuple[int, str]', None),
                ('no_really', 'str', 'str', None),
            ], 'typing.List[str]', 'typing.List[str]'))

    def test_nested_square_brackets(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Tuple[int, List[Tuple[int, int]]], another: float) -> Union[str, None]'),
            ('foo', '', [
                ('a', 'typing.Tuple[int, typing.List[typing.Tuple[int, int]]]', 'typing.Tuple[int, typing.List[typing.Tuple[int, int]]]', None),
                ('another', 'float', 'float', None),
            ], 'typing.Union[str, None]', 'typing.Union[str, None]'))

    def test_callable(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Callable[[int, Tuple[int, int]], float], another: float)'),
            ('foo', '', [
                ('a', 'typing.Callable[[int, typing.Tuple[int, int]], float]', 'typing.Callable[[int, typing.Tuple[int, int]], float]', None),
                ('another', 'float', 'float', None),
            ], None, None))

    def test_kwargs(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(*args, **kwargs)'),
            ('foo', '', [
                ('*args', None, None, None),
                ('**kwargs', None, None, None),
            ], None, None))

    def test_keyword_positional_only(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, /, b: float, *, keyword: str)'),
            ('foo', '', [
                ('a', 'int', 'int', None),
                ('/', None, None, None),
                ('b', 'float', 'float', None),
                ('*', None, None, None),
                ('keyword', 'str', 'str', None),
            ], None, None))

    def test_default_values(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: float = 1.0, b: str = \'hello\')'),
            ('foo', '', [
                ('a', 'float', 'float', '1.0'),
                ('b', 'str', 'str', '\'hello\''),
            ], None, None))

        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: float = libA.foo(libB.goo(123), libB.bar + 13) + 2, b = 3)'),
            ('foo', '', [
                ('a', 'float', 'float', 'libA.foo(libB.goo(123), libB.bar + 13) + 2'),
                ('b', None, None, '3'),
            ], None, None))

        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Tuple[int, ...] = (1,("hello", \'world\'),3,4))'),
            ('foo', '', [
                ('a', 'typing.Tuple[int, ...]',
                      'typing.Tuple[int, ...]',
                 '(1,("hello", \'world\'),3,4)')
            ], None, None))

        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: str = [dict(key="A", value=\'B\')["key"][0], None][0])'),
             ('foo', '', [
                 ('a', 'str', 'str', '[dict(key="A", value=\'B\')["key"][0], None][0]')
             ], None, None))

        bad_signature = ('foo', '', [('…', None, None, None)], None, None)

        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: float = [0][)'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: float = ()'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: float = (()'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: float = ))'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: float = ])'), bad_signature)

    # https://github.com/pybind/pybind11/pull/2126, extremely stupid and
    # annoying but what can I do. Want to support both this and the original
    # behavior in case they revert the insanity again, so test that both
    # variants give the same output.
    def test_default_enum_values_pybind26(self):
        # Before the insane change
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: bar.Enum = Enum.FOO_BAR)'),
            ('foo', '', [
                ('a', 'bar.Enum', 'bar.Enum', 'Enum.FOO_BAR')
            ], None, None))

        # After the insane change
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: bar.Enum = <Enum.FOO_BAR: -13376>)'),
            ('foo', '', [
                ('a', 'bar.Enum', 'bar.Enum', 'Enum.FOO_BAR')
            ], None, None))

        # Nested
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: bar.Enum = (4, [<Enum.FOO_BAR: -13376>], <Enum.FIZZ_PISS: 1>))'),
            ('foo', '', [
                ('a', 'bar.Enum', 'bar.Enum', '(4, [Enum.FOO_BAR], Enum.FIZZ_PISS)')
            ], None, None))

        # This isn't really expected to happen but yeah it still treats it as
        # an enum
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Enum = <Enum_MISSING_DOT:>)'),
            ('foo', '', [
                ('a', 'Enum', 'Enum', 'Enum_MISSING_DOT')
            ], None, None))

        # This is how pybind prints various objects, should be passed as-is.
        # It should not corrupt any parameters after.
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: FooBar = <FooBar object at 0xabcd>, b: int = 3)'),
            ('foo', '', [
                ('a', 'FooBar', 'FooBar', '<FooBar object at 0xabcd>'),
                ('b', 'int', 'int', '3')
            ], None, None))

        # This is weird and so will be passed as-is.
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: Enum = <Enum_MISSING_GT: -1)'),
            ('foo', '', [
                ('a', 'Enum', 'Enum', '<Enum_MISSING_GT: -1')
            ], None, None))

        # This will fail
        bad_signature = ('foo', '', [('…', None, None, None)], None, None)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: Enum = <Enum.CHARACTERS_AFTER: 17>a)'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo(a: Enum = <Enum.CHARACTERS_AFTER: 89><)'), bad_signature)

    def test_bad_return_type(self):
        bad_signature = ('foo', '', [('…', None, None, None)], None, None)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo() -> List[[]'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo() -> List]'), bad_signature)
        self.assertEqual(parse_pybind_signature(self.state, [], 'foo() -> ::std::vector<int>'), bad_signature)

    def test_crazy_stuff(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, b: Math::Vector<4, UnsignedInt>)'),
            ('foo', '', [('…', None, None, None)], None, None))

    def test_crazy_stuff_nested(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, b: List[Math::Vector<4, UnsignedInt>])'),
            ('foo', '', [('…', None, None, None)], None, None))

    def test_crazy_stuff_docs(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int, b: Math::Vector<4, UnsignedInt>)\n\nThis is text!!'),
            ('foo', 'This is text!!', [('…', None, None, None)], None, None))

    def test_crazy_return(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int) -> Math::Vector<4, UnsignedInt>'),
            ('foo', '', [('…', None, None, None)], None, None))

    def test_crazy_return_nested(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int) -> List[Math::Vector<4, UnsignedInt>]'),
            ('foo', '', [('…', None, None, None)], None, None))

    def test_crazy_return_docs(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            'foo(a: int) -> Math::Vector<4, UnsignedInt>\n\nThis returns!'),
            ('foo', 'This returns!', [('…', None, None, None)], None, None))

    def test_no_name(self):
        self.assertEqual(parse_pybind_signature(self.state, [],
            '(arg0: MyClass) -> float'),
            ('', '', [('arg0', 'MyClass', 'MyClass', None)], 'float', 'float'))

    def test_name_mapping(self):
        state = copy.deepcopy(self.state)
        state.name_mapping['module._module'] = 'module'

        self.assertEqual(parse_pybind_signature(state, [],
            'foo(a: module._module.Foo, b: typing.Tuple[int, module._module.Bar]) -> module._module.Baz'),
            ('foo', '', [('a', 'module.Foo', 'module.Foo', None),
                         ('b', 'typing.Tuple[int, module.Bar]', 'typing.Tuple[int, module.Bar]', None)], 'module.Baz', 'module.Baz'))

class Signatures(BaseInspectTestCase):
    def test_positional_args(self):
        sys.path.append(self.path)
        import pybind_signatures

        # Verify that the assumptions are correct -- not using py::arg() makes
        # the parameters positional-only, while py::arg() makes them
        # positional-or-keyword
        self.assertEqual(pybind_signatures.scale(14, 0.3), 4)
        with self.assertRaises(TypeError):
            pybind_signatures.scale(arg0=1, arg1=3.0)
        self.assertEqual(pybind_signatures.scale_kwargs(14, 0.3), 4)
        self.assertEqual(pybind_signatures.scale_kwargs(a=14, argument=0.3), 4)

        # Verify the same for classes
        a = pybind_signatures.MyClass()
        self.assertEqual(pybind_signatures.MyClass.instance_function(a, 3, 'bla'), (0.5, 42))
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass.instance_function(self=a, arg0=3, arg1='bla')
        self.assertEqual(pybind_signatures.MyClass.instance_function_kwargs(a, 3, 'bla'), (0.5, 42))
        self.assertEqual(pybind_signatures.MyClass.instance_function_kwargs(self=a, hey=3, what='bla'), (0.5, 42))

        # In particular, the 'self' parameter is positional-only if there are
        # no arguments to use py::arg() for
        self.assertEqual(pybind_signatures.MyClass.another(a), 42)
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass.another(self=a)

    def test_explicit_positional_args(self):
        sys.path.append(self.path)
        import pybind_signatures

        # Similar to above, but these functions have explicit py::pos_only and
        # py::kw_only placeholders

        if not pybind_signatures.MyClass26.is_pybind26:
            self.skipTest("only on pybind 2.6+")

        # The a: int argument is always before the / and thus shouldn't be
        # callable with a keyword
        self.assertEqual(pybind_signatures.MyClass26.positional_only(1, 3.0), 1)
        self.assertEqual(pybind_signatures.MyClass26.positional_keyword_only(1, 3.0), 3)
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass26.positional_only(a=1, b=3.0)
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass26.positional_keyword_only(a=1, b=3.0)

        # The b argument is always between / and * and thus should be callable
        # both without (done above/below) and with
        self.assertEqual(pybind_signatures.MyClass26.positional_only(1, b=3.0), 1)
        self.assertEqual(pybind_signatures.MyClass26.keyword_only(b=3.0), 2)
        self.assertEqual(pybind_signatures.MyClass26.positional_keyword_only(1, b=3.0), 3)

        # The keyword: str argument is always after the / and thus shouldn't be
        # callable without a keyword
        self.assertEqual(pybind_signatures.MyClass26.keyword_only(3.0, keyword='yes'), 2)
        self.assertEqual(pybind_signatures.MyClass26.positional_keyword_only(1, 3.0, keyword='yes'), 3)
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass26.keyword_only(3.0, 'yes')
        with self.assertRaises(TypeError):
            pybind_signatures.MyClass26.positional_keyword_only(1, 3.0, 'yes')

    def test(self):
        sys.path.append(self.path)
        import pybind_signatures
        self.run_python({
            'INPUT_MODULES': [pybind_signatures, 'false_positives'],
            'PYBIND11_COMPATIBILITY': True
        })
        self.assertEqual(*self.actual_expected_contents('pybind_signatures.html'))
        self.assertEqual(*self.actual_expected_contents('pybind_signatures.MyClass.html'))
        self.assertEqual(*self.actual_expected_contents('false_positives.html'))

        if pybind_signatures.MyClass23.is_pybind23:
            self.assertEqual(*self.actual_expected_contents('pybind_signatures.MyClass23.html'))
        if pybind_signatures.MyClass26.is_pybind26:
            self.assertEqual(*self.actual_expected_contents('pybind_signatures.MyClass26.html'))

class Enums(BaseInspectTestCase):
    def test(self):
        self.run_python({
            'PLUGINS': ['m.sphinx'],
            'INPUT_DOCS': ['docs.rst'],
            'PYBIND11_COMPATIBILITY': True,
        })
        self.assertEqual(*self.actual_expected_contents('pybind_enums.html'))

class Submodules(BaseInspectTestCase):
    def test(self):
        self.run_python({
            'PYBIND11_COMPATIBILITY': True
        })
        self.assertEqual(*self.actual_expected_contents('pybind_submodules.html'))

class SubmodulesPackage(BaseInspectTestCase):
    def test(self):
        self.run_python({
            'PYBIND11_COMPATIBILITY': True
        })
        self.assertEqual(*self.actual_expected_contents('pybind_submodules_package.sub.html'))

class NameMapping(BaseInspectTestCase):
    def test(self):
        self.run_python({
            'PYBIND11_COMPATIBILITY': True
        })
        self.assertEqual(*self.actual_expected_contents('pybind_name_mapping.html'))
        self.assertEqual(*self.actual_expected_contents('pybind_name_mapping.Class.html'))
        self.assertEqual(*self.actual_expected_contents('pybind_name_mapping.submodule.html'))

class TypeLinks(BaseInspectTestCase):
    def test(self):
        sys.path.append(self.path)
        import pybind_type_links
        # Annotate the type of TYPE_DATA (TODO: can this be done from pybind?)
        pybind_type_links.__annotations__ = {}
        pybind_type_links.__annotations__['TYPE_DATA'] = pybind_type_links.Foo
        pybind_type_links.Foo.__annotations__ = {}
        pybind_type_links.Foo.__annotations__['TYPE_DATA'] = pybind_type_links.Enum
        self.run_python({
            'PLUGINS': ['m.sphinx'],
            'INPUT_MODULES': [pybind_type_links],
            'PYBIND11_COMPATIBILITY': True,
            'M_SPHINX_INVENTORIES': [
                ('../../../doc/documentation/python.inv', 'https://docs.python.org/3/', [], ['m-doc-external'])]
        })
        self.assertEqual(*self.actual_expected_contents('pybind_type_links.html'))
        self.assertEqual(*self.actual_expected_contents('pybind_type_links.Foo.html'))

class ExternalOverloadDocs(BaseInspectTestCase):
    def test(self):
        self.run_python({
            'PLUGINS': ['m.sphinx'],
            'INPUT_DOCS': ['docs.rst'],
            'PYBIND11_COMPATIBILITY': True
        })
        self.assertEqual(*self.actual_expected_contents('pybind_external_overload_docs.html'))
        self.assertEqual(*self.actual_expected_contents('pybind_external_overload_docs.Class.html'))

        sys.path.append(self.path)
        import pybind_external_overload_docs
        if pybind_external_overload_docs.Class26.is_pybind26:
            self.assertEqual(*self.actual_expected_contents('pybind_external_overload_docs.Class26.html'))