[Python-checkins] cpython: Issue #27366: Tweak PEP 487 documentation

berker.peksag python-checkins at python.org
Sat Jul 30 07:05:37 EDT 2016


https://hg.python.org/cpython/rev/8747e3455ecb
changeset:   102494:8747e3455ecb
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Sat Jul 30 14:06:15 2016 +0300
summary:
  Issue #27366: Tweak PEP 487 documentation

* Added versionadded directives
* Deleted duplicate sentence from __init_subclass__ docstring
* Modernized tests

files:
  Doc/reference/datamodel.rst   |   5 ++
  Lib/test/test_subclassinit.py |  44 +++++++++++-----------
  Objects/typeobject.c          |   7 +--
  3 files changed, 30 insertions(+), 26 deletions(-)


diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1497,6 +1497,8 @@
    Called at the time the owning class *owner* is created. The
    descriptor has been assigned to *name*.
 
+   .. versionadded:: 3.6
+
 
 The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module
 as specifying the class where this object was defined (setting this
@@ -1648,6 +1650,7 @@
 class defining the method.
 
 .. classmethod:: object.__init_subclass__(cls)
+
    This method is called whenever the containing class is subclassed.
    *cls* is then the new subclass. If defined as a normal instance method,
    this method is implicitly converted to a class method.
@@ -1669,6 +1672,8 @@
    The default implementation ``object.__init_subclass__`` does
    nothing, but raises an error if it is called with any arguments.
 
+   .. versionadded:: 3.6
+
 
 .. _metaclasses:
 
diff --git a/Lib/test/test_subclassinit.py b/Lib/test/test_subclassinit.py
--- a/Lib/test/test_subclassinit.py
+++ b/Lib/test/test_subclassinit.py
@@ -1,11 +1,11 @@
-from unittest import TestCase, main
 import sys
 import types
+import unittest
 
 
-class Test(TestCase):
+class Test(unittest.TestCase):
     def test_init_subclass(self):
-        class A(object):
+        class A:
             initialized = False
 
             def __init_subclass__(cls):
@@ -19,7 +19,7 @@
         self.assertTrue(B.initialized)
 
     def test_init_subclass_dict(self):
-        class A(dict, object):
+        class A(dict):
             initialized = False
 
             def __init_subclass__(cls):
@@ -33,7 +33,7 @@
         self.assertTrue(B.initialized)
 
     def test_init_subclass_kwargs(self):
-        class A(object):
+        class A:
             def __init_subclass__(cls, **kwargs):
                 cls.kwargs = kwargs
 
@@ -43,7 +43,7 @@
         self.assertEqual(B.kwargs, dict(x=3))
 
     def test_init_subclass_error(self):
-        class A(object):
+        class A:
             def __init_subclass__(cls):
                 raise RuntimeError
 
@@ -52,7 +52,7 @@
                 pass
 
     def test_init_subclass_wrong(self):
-        class A(object):
+        class A:
             def __init_subclass__(cls, whatever):
                 pass
 
@@ -61,7 +61,7 @@
                 pass
 
     def test_init_subclass_skipped(self):
-        class BaseWithInit(object):
+        class BaseWithInit:
             def __init_subclass__(cls, **kwargs):
                 super().__init_subclass__(**kwargs)
                 cls.initialized = cls
@@ -76,7 +76,7 @@
         self.assertIs(BaseWithoutInit.initialized, BaseWithoutInit)
 
     def test_init_subclass_diamond(self):
-        class Base(object):
+        class Base:
             def __init_subclass__(cls, **kwargs):
                 super().__init_subclass__(**kwargs)
                 cls.calls = []
@@ -84,7 +84,7 @@
         class Left(Base):
             pass
 
-        class Middle(object):
+        class Middle:
             def __init_subclass__(cls, middle, **kwargs):
                 super().__init_subclass__(**kwargs)
                 cls.calls += [middle]
@@ -107,7 +107,7 @@
                 self.owner = owner
                 self.name = name
 
-        class A(object):
+        class A:
             d = Descriptor()
 
         self.assertEqual(A.d.name, "d")
@@ -121,12 +121,12 @@
                 self.assertIs(ret.d.owner, ret)
                 return 0
 
-        class Descriptor(object):
+        class Descriptor:
             def __set_name__(self, owner, name):
                 self.owner = owner
                 self.name = name
 
-        class A(object, metaclass=Meta):
+        class A(metaclass=Meta):
             d = Descriptor()
         self.assertEqual(A, 0)
 
@@ -136,7 +136,7 @@
                 raise RuntimeError
 
         with self.assertRaises(RuntimeError):
-            class A(object):
+            class A:
                 d = Descriptor()
 
     def test_set_name_wrong(self):
@@ -145,7 +145,7 @@
                 pass
 
         with self.assertRaises(TypeError):
-            class A(object):
+            class A:
                 d = Descriptor()
 
     def test_set_name_init_subclass(self):
@@ -161,7 +161,7 @@
                 self.meta_name = self.name
                 return self
 
-        class A(object):
+        class A:
             def __init_subclass__(cls):
                 cls.owner = cls.d.owner
                 cls.name = cls.d.name
@@ -179,7 +179,7 @@
             pass
 
         with self.assertRaises(TypeError):
-            class MyClass(object, metaclass=MyMeta, otherarg=1):
+            class MyClass(metaclass=MyMeta, otherarg=1):
                 pass
 
         with self.assertRaises(TypeError):
@@ -193,7 +193,7 @@
                 super().__init__(name, bases, namespace)
 
         with self.assertRaises(TypeError):
-            class MyClass(object, metaclass=MyMeta, otherarg=1):
+            class MyClass(metaclass=MyMeta, otherarg=1):
                 pass
 
         class MyMeta(type):
@@ -204,7 +204,7 @@
                 super().__init__(name, bases, namespace)
                 self.otherarg = otherarg
 
-        class MyClass(object, metaclass=MyMeta, otherarg=1):
+        class MyClass(metaclass=MyMeta, otherarg=1):
             pass
 
         self.assertEqual(MyClass.otherarg, 1)
@@ -217,7 +217,7 @@
                                        dict=namespace)
 
         with self.assertRaises(TypeError):
-            class MyClass(object, metaclass=MyMeta):
+            class MyClass(metaclass=MyMeta):
                 pass
 
         class MyMeta(type):
@@ -226,7 +226,7 @@
                 self.otherarg = otherarg
                 return self
 
-        class MyClass(object, metaclass=MyMeta, otherarg=1):
+        class MyClass(metaclass=MyMeta, otherarg=1):
             pass
 
         self.assertEqual(MyClass.otherarg, 1)
@@ -241,4 +241,4 @@
 
 
 if __name__ == "__main__":
-    main()
+    unittest.main()
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4341,11 +4341,10 @@
 }
 
 PyDoc_STRVAR(object_init_subclass_doc,
-"This method is called when a class is subclassed\n"
+"This method is called when a class is subclassed.\n"
 "\n"
-"Whenever a class is subclassed, this method is called. The default\n"
-"implementation does nothing. It may be overridden to extend\n"
-"subclasses.\n");
+"The default implementation does nothing. It may be\n"
+"overridden to extend subclasses.\n");
 
 /*
    from PEP 3101, this code implements:

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list