File: test_issue.py

package info (click to toggle)
nanobind 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,060 kB
  • sloc: cpp: 11,838; python: 5,862; ansic: 4,820; makefile: 22; sh: 15
file content (37 lines) | stat: -rw-r--r-- 1,019 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
import test_issue_ext as m
import pytest

# Issue #279: dynamic_attr broken
@pytest.mark.parametrize("variant", [1, 2])
def test01_issue_279(variant):
    def _get_parameter(self: m.Model, key: str):
        p = self._get_param(key)
        if p is not None:  # cache it for fast access later
            setattr(self, key, p)
            return p
        raise AttributeError(f"'key' not found in {self}")

    m.Model.__getattr__ = _get_parameter

    if variant == 2:
        def _print_model(self):
            return f"{self.__class__.__qualname__}()"

        m.Model.__str__ = _print_model

    class Top(m.Model):
        def __init__(self):
            super().__init__()
            self.model_a = m.ModelA()

    top = Top()
    str(top.model_a)
    str(top.model_a.a)


# Issue #307: move constructor unexpectedly called
def test02_issue_307():
    l = [m.Example("A"), m.Example("B")]
    assert str(l) == '[Example("A"), Example("B")]'
    m.process(l)
    assert str(l) == '[Example("A"), Example("B")]'