]> granicus.if.org Git - python/commitdiff
inspect.signature: Support classes without user-defined __init__/__new__ #20308
authorYury Selivanov <yselivanov@sprymix.com>
Tue, 28 Jan 2014 00:29:45 +0000 (19:29 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Tue, 28 Jan 2014 00:29:45 +0000 (19:29 -0500)
Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index 9436e352763bea2a581534da0da49485087d9980..2211b8d4e04cbb4526e9a78b7abd157dc5cdca0a 100644 (file)
@@ -1561,6 +1561,17 @@ def signature(obj):
                 init = _get_user_defined_method(obj, '__init__')
                 if init is not None:
                     sig = signature(init)
+
+        if sig is None:
+            if type in obj.__mro__:
+                # 'obj' is a metaclass without user-defined __init__
+                # or __new__. Return a signature of 'type' builtin.
+                return signature(type)
+            else:
+                # We have a class (not metaclass), but no user-defined
+                # __init__ or __new__ for it
+                return signature(object)
+
     elif not isinstance(obj, _NonUserDefinedCallables):
         # An object with __call__
         # We also check that the 'obj' is not an instance of
index f5f18f0f218d89ce5ae9ff582763beb87d09f405..a6b4c7ac60f043b74386b7b55e5da9bec8684655 100644 (file)
@@ -2045,6 +2045,20 @@ class TestSignatureObject(unittest.TestCase):
                            ('bar', 2, ..., "keyword_only")),
                           ...))
 
+        # Test classes without user-defined __init__ or __new__
+        class C: pass
+        self.assertEqual(str(inspect.signature(C)), '()')
+        class D(C): pass
+        self.assertEqual(str(inspect.signature(D)), '()')
+
+        # Test meta-classes without user-defined __init__ or __new__
+        class C(type): pass
+        self.assertEqual(str(inspect.signature(C)),
+                         '(object_or_name, bases, dict)')
+        class D(C): pass
+        self.assertEqual(str(inspect.signature(D)),
+                         '(object_or_name, bases, dict)')
+
     def test_signature_on_callable_objects(self):
         class Foo:
             def __call__(self, a):
index 6d528be9e7412cf67f97e774d808b62c508b9d5b..e1050c628eb7b414a87b8a43f6e858641db49ef6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20308: inspect.signature now works on classes without user-defined
+  __init__ or __new__ methods.
+
 
 What's New in Python 3.4.0 Beta 3?
 ==================================