aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/pysidetest/multiple_inheritance_test.py
blob: 49550ba55f8137c5b55ab1fe9089079a2ac8cc90 (plain)
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
# Copyright (C) 2023 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)

from helper.usesqapplication import UsesQApplication
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtWidgets import QMainWindow, QLabel, QWidget


def xprint(*args, **kw):
    if "-v" in sys.argv:
        print(*args, **kw)


# This is the original testcase of PYSIDE-1564
class Age(object):
    def __init__(self, age=0, **kwds):
        super().__init__(**kwds)

        self.age = age


class Person(QtCore.QObject, Age):
    def __init__(self, name, **kwds):
        super().__init__(**kwds)

        self.name = name


class OriginalMultipleInheritanceTest(unittest.TestCase):

    def testIt(self):
        xprint()
        p = Person("Joe", age=38)
        xprint(f"p.age = {p.age}")
        # This would crash if MI does not work.

# More tests follow:


# mro ('C', 'A', 'QObject', 'Object', 'B', 'object')
class A(QtCore.QObject):
    def __init__(self, anna=77, **kw):
        xprint(f'A: before init kw = {kw}')
        super().__init__(**kw)
        xprint('A: after init')


class B:
    def __init__(self, otto=6, age=7, **kw):
        xprint(f'B: before init kw = {kw}')
        if "killme" in kw:
            raise AssertionError("asdf")
        super().__init__(**kw)
        self.age = age
        xprint('B: after init')


class C(A, B):
    def __init__(self, **kw):
        xprint(f'C: before init kw = {kw}')
        super().__init__(**kw)
        xprint('C: after init')


# mro ('F', 'D', 'QCursor', 'E', 'QLabel', 'QFrame', 'QWidget', 'QObject',
#      'QPaintDevice', 'Object', 'object')
class D(QtGui.QCursor):
    def __init__(self, anna=77, **kw):
        xprint(f'D: before init kw = {kw}')
        super().__init__(**kw)
        xprint('D: after init')


class E:
    def __init__(self, age=7, **kw):
        xprint(f'E: before init kw = {kw}')
        super().__init__(**kw)
        self.age = age
        xprint('E: after init')


class F(D, E, QtWidgets.QLabel):
    def __init__(self, **kw):
        xprint(f'F: before init kw = {kw}')
        super().__init__(**kw)
        xprint('F: after init')


# mro ('I', 'G', 'QTextDocument', 'H', 'QLabel', 'QFrame', 'QWidget', 'QObject',
#      'QPaintDevice', 'Object', 'object')
# Similar, but this time we want to reach `H` without support from `super`.
class G(QtGui.QTextDocument):
    pass


class H:
    def __init__(self, age=7, **kw):
        xprint(f'H: before init kw = {kw}')
        super().__init__(**kw)
        self.age = age
        xprint('H: after init')


class II(G, H, QtWidgets.QLabel):
    pass


# PYSIDE-2294: Friedemann's test adapted.
#              We need to ignore positional args in mixin classes.
class Ui_X_MainWindow(object):  # Emulating uic
    def setupUi(self, MainWindow):
        MainWindow.resize(400, 300)
        self.lbl = QLabel(self)


class MainWindow(QMainWindow, Ui_X_MainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)


class AdditionalMultipleInheritanceTest(UsesQApplication):

    def testABC(self):
        xprint()
        res = C(otto=3, anna=5)
        self.assertEqual(res.age, 7)
        xprint()
        with self.assertRaises(AssertionError):
            res = C(killme=42)
        xprint()

    def testDEF(self):
        xprint()
        res = F(anna=5)
        self.assertEqual(res.age, 7)
        xprint()

    def testGHI(self):
        xprint()
        res = II(age=7)
        self.assertEqual(res.age, 7)
        xprint()

    def testParentDoesNotCrash(self):
        # This crashed with
        # TypeError: object.__init__() takes exactly one argument (the instance to initialize)
        MainWindow()


# PYSIDE-2654: Additional missing init test.
#              This must work if no __init__ is defined (Ui_Form)
class Ui_Form(object):
    pass


class Mixin:
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)


class Card(Mixin, QWidget):
    def __init__(self, parent=None) -> None:
        super().__init__(parent=parent)


class Demo(Card, Ui_Form):
    def __init__(self) -> None:
        super().__init__()


class MissingInitFunctionTest(UsesQApplication):
    def testMissing(self):
        Demo()
        # Tests if this works. Would crash without the extra
        # check for object.__init__


if __name__ == "__main__":
    unittest.main()