Skip to content

Commit b349479

Browse files
committed
tests/class_new: Add another testcase for __new__/__init__ interaction.
Similar to the existing testcase, but test that returning both value of native type and instance of another user class from __new__ lead to __init__ not being called, for better coverage.
1 parent ca21aed commit b349479

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

tests/basics/class_new.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def meth(self):
3434
a = a.__new__(A)
3535
a.meth()
3636

37+
# __new__ returns not an instance of the class (None here), __init__
38+
# should not be called
39+
3740
class B:
3841
def __new__(self, v1, v2):
3942
print("B.__new__", v1, v2)
@@ -43,3 +46,21 @@ def __init__(self, v1, v2):
4346
print("B.__init__", v1, v2)
4447

4548
print("B inst:", B(1, 2))
49+
50+
51+
# Variation of the above, __new__ returns an instance of another class,
52+
# __init__ should not be called
53+
54+
class Dummy: pass
55+
56+
class C:
57+
def __new__(cls):
58+
print("C.__new__")
59+
return Dummy()
60+
61+
def __init__(self):
62+
# Should not be called in this test
63+
print("C.__init__")
64+
65+
c = C()
66+
print(isinstance(c, Dummy))

0 commit comments

Comments
 (0)