Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01d1719

Browse files
committed
Issue #27366: Tweak PEP 487 documentation
* Added versionadded directives * Deleted duplicate sentence from __init_subclass__ docstring * Modernized tests
1 parent 6074f21 commit 01d1719

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

Doc/reference/datamodel.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,8 @@ class' :attr:`~object.__dict__`.
14971497
Called at the time the owning class *owner* is created. The
14981498
descriptor has been assigned to *name*.
14991499

1500+
.. versionadded:: 3.6
1501+
15001502

15011503
The attribute :attr:`__objclass__` is interpreted by the :mod:`inspect` module
15021504
as specifying the class where this object was defined (setting this
@@ -1648,6 +1650,7 @@ applied to, ``__init_subclass__`` solely applies to future subclasses of the
16481650
class defining the method.
16491651

16501652
.. classmethod:: object.__init_subclass__(cls)
1653+
16511654
This method is called whenever the containing class is subclassed.
16521655
*cls* is then the new subclass. If defined as a normal instance method,
16531656
this method is implicitly converted to a class method.
@@ -1669,6 +1672,8 @@ class defining the method.
16691672
The default implementation ``object.__init_subclass__`` does
16701673
nothing, but raises an error if it is called with any arguments.
16711674

1675+
.. versionadded:: 3.6
1676+
16721677

16731678
.. _metaclasses:
16741679

Lib/test/test_subclassinit.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from unittest import TestCase, main
21
import sys
32
import types
3+
import unittest
44

55

6-
class Test(TestCase):
6+
class Test(unittest.TestCase):
77
def test_init_subclass(self):
8-
class A(object):
8+
class A:
99
initialized = False
1010

1111
def __init_subclass__(cls):
@@ -19,7 +19,7 @@ class B(A):
1919
self.assertTrue(B.initialized)
2020

2121
def test_init_subclass_dict(self):
22-
class A(dict, object):
22+
class A(dict):
2323
initialized = False
2424

2525
def __init_subclass__(cls):
@@ -33,7 +33,7 @@ class B(A):
3333
self.assertTrue(B.initialized)
3434

3535
def test_init_subclass_kwargs(self):
36-
class A(object):
36+
class A:
3737
def __init_subclass__(cls, **kwargs):
3838
cls.kwargs = kwargs
3939

@@ -43,7 +43,7 @@ class B(A, x=3):
4343
self.assertEqual(B.kwargs, dict(x=3))
4444

4545
def test_init_subclass_error(self):
46-
class A(object):
46+
class A:
4747
def __init_subclass__(cls):
4848
raise RuntimeError
4949

@@ -52,7 +52,7 @@ class B(A):
5252
pass
5353

5454
def test_init_subclass_wrong(self):
55-
class A(object):
55+
class A:
5656
def __init_subclass__(cls, whatever):
5757
pass
5858

@@ -61,7 +61,7 @@ class B(A):
6161
pass
6262

6363
def test_init_subclass_skipped(self):
64-
class BaseWithInit(object):
64+
class BaseWithInit:
6565
def __init_subclass__(cls, **kwargs):
6666
super().__init_subclass__(**kwargs)
6767
cls.initialized = cls
@@ -76,15 +76,15 @@ class A(BaseWithoutInit):
7676
self.assertIs(BaseWithoutInit.initialized, BaseWithoutInit)
7777

7878
def test_init_subclass_diamond(self):
79-
class Base(object):
79+
class Base:
8080
def __init_subclass__(cls, **kwargs):
8181
super().__init_subclass__(**kwargs)
8282
cls.calls = []
8383

8484
class Left(Base):
8585
pass
8686

87-
class Middle(object):
87+
class Middle:
8888
def __init_subclass__(cls, middle, **kwargs):
8989
super().__init_subclass__(**kwargs)
9090
cls.calls += [middle]
@@ -107,7 +107,7 @@ def __set_name__(self, owner, name):
107107
self.owner = owner
108108
self.name = name
109109

110-
class A(object):
110+
class A:
111111
d = Descriptor()
112112

113113
self.assertEqual(A.d.name, "d")
@@ -121,12 +121,12 @@ def __new__(cls, name, bases, ns):
121121
self.assertIs(ret.d.owner, ret)
122122
return 0
123123

124-
class Descriptor(object):
124+
class Descriptor:
125125
def __set_name__(self, owner, name):
126126
self.owner = owner
127127
self.name = name
128128

129-
class A(object, metaclass=Meta):
129+
class A(metaclass=Meta):
130130
d = Descriptor()
131131
self.assertEqual(A, 0)
132132

@@ -136,7 +136,7 @@ def __set_name__(self, owner, name):
136136
raise RuntimeError
137137

138138
with self.assertRaises(RuntimeError):
139-
class A(object):
139+
class A:
140140
d = Descriptor()
141141

142142
def test_set_name_wrong(self):
@@ -145,7 +145,7 @@ def __set_name__(self):
145145
pass
146146

147147
with self.assertRaises(TypeError):
148-
class A(object):
148+
class A:
149149
d = Descriptor()
150150

151151
def test_set_name_init_subclass(self):
@@ -161,7 +161,7 @@ def __new__(cls, name, bases, ns):
161161
self.meta_name = self.name
162162
return self
163163

164-
class A(object):
164+
class A:
165165
def __init_subclass__(cls):
166166
cls.owner = cls.d.owner
167167
cls.name = cls.d.name
@@ -179,7 +179,7 @@ class MyMeta(type):
179179
pass
180180

181181
with self.assertRaises(TypeError):
182-
class MyClass(object, metaclass=MyMeta, otherarg=1):
182+
class MyClass(metaclass=MyMeta, otherarg=1):
183183
pass
184184

185185
with self.assertRaises(TypeError):
@@ -193,7 +193,7 @@ def __init__(self, name, bases, namespace, otherarg):
193193
super().__init__(name, bases, namespace)
194194

195195
with self.assertRaises(TypeError):
196-
class MyClass(object, metaclass=MyMeta, otherarg=1):
196+
class MyClass(metaclass=MyMeta, otherarg=1):
197197
pass
198198

199199
class MyMeta(type):
@@ -204,7 +204,7 @@ def __init__(self, name, bases, namespace, otherarg):
204204
super().__init__(name, bases, namespace)
205205
self.otherarg = otherarg
206206

207-
class MyClass(object, metaclass=MyMeta, otherarg=1):
207+
class MyClass(metaclass=MyMeta, otherarg=1):
208208
pass
209209

210210
self.assertEqual(MyClass.otherarg, 1)
@@ -217,7 +217,7 @@ def __new__(cls, name, bases, namespace):
217217
dict=namespace)
218218

219219
with self.assertRaises(TypeError):
220-
class MyClass(object, metaclass=MyMeta):
220+
class MyClass(metaclass=MyMeta):
221221
pass
222222

223223
class MyMeta(type):
@@ -226,7 +226,7 @@ def __new__(cls, name, bases, namespace, otherarg):
226226
self.otherarg = otherarg
227227
return self
228228

229-
class MyClass(object, metaclass=MyMeta, otherarg=1):
229+
class MyClass(metaclass=MyMeta, otherarg=1):
230230
pass
231231

232232
self.assertEqual(MyClass.otherarg, 1)
@@ -241,4 +241,4 @@ def test_type(self):
241241

242242

243243
if __name__ == "__main__":
244-
main()
244+
unittest.main()

Objects/typeobject.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,11 +4341,10 @@ object_init_subclass(PyObject *cls, PyObject *arg)
43414341
}
43424342

43434343
PyDoc_STRVAR(object_init_subclass_doc,
4344-
"This method is called when a class is subclassed\n"
4344+
"This method is called when a class is subclassed.\n"
43454345
"\n"
4346-
"Whenever a class is subclassed, this method is called. The default\n"
4347-
"implementation does nothing. It may be overridden to extend\n"
4348-
"subclasses.\n");
4346+
"The default implementation does nothing. It may be\n"
4347+
"overridden to extend subclasses.\n");
43494348

43504349
/*
43514350
from PEP 3101, this code implements:

0 commit comments

Comments
 (0)