From b349479a49aab440b2f38fc6e45b3df4bf7a1aa7 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 1 Sep 2017 00:39:06 +0300 Subject: [PATCH] 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. --- tests/basics/class_new.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/basics/class_new.py b/tests/basics/class_new.py index 4912f71f0b..1f6a86c648 100644 --- a/tests/basics/class_new.py +++ b/tests/basics/class_new.py @@ -34,6 +34,9 @@ a.meth() a = a.__new__(A) a.meth() +# __new__ returns not an instance of the class (None here), __init__ +# should not be called + class B: def __new__(self, v1, v2): print("B.__new__", v1, v2) @@ -43,3 +46,21 @@ class B: print("B.__init__", v1, v2) print("B inst:", B(1, 2)) + + +# Variation of the above, __new__ returns an instance of another class, +# __init__ should not be called + +class Dummy: pass + +class C: + def __new__(cls): + print("C.__new__") + return Dummy() + + def __init__(self): + # Should not be called in this test + print("C.__init__") + +c = C() +print(isinstance(c, Dummy))