Skip to content

Commit bda8190

Browse files
miss-islingtonfurkanonderblurb-it[bot]CAM-Gerlachterryjreedy
authored
[3.12] gh-60712: Include the "object" type in the lists of documented types (GH-103036) (GH-126198)
gh-60712: Include the "object" type in the lists of documented types (GH-103036) * add test for the predefined object's attributes * Include the "object" type in the lists of documented types * remove 'or' from augment tuple * 📜🤖 Added by blurb_it. * Add cross-reference to news * Fix format for the function parameter * Add space * add reference for the 'object' * add reference for NotImplemented * Change ref:`string <textseq>` as class:`str` * remove hyphen from `newly-created` * Update Doc/reference/datamodel.rst 'dictionaries' to 'dict' * Update predefined attribute types in testPredefinedAttrs * Change `universal type` as `top type` * Don't mention about the top type * Update the description of richcmpfuncs * Update Doc/library/stdtypes.rst * Revert: Hierarchy Section in Data Model Documentation * Revert to original explanations of __new__ and __init__ methods in datamodel.rst for improved clarity. * Update Doc/reference/datamodel.rst * Remove blank line * Use ref:`str <textseq>` instead of :class:`str * Revert changes the description of Other Built-in Types in stdtypes.rst * Update Doc/reference/datamodel.rst --------- (cherry picked from commit 4f82621) Co-authored-by: Furkan Onder <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: C.A.M. Gerlach <[email protected]> Co-authored-by: Terry Jan Reedy <[email protected]> Co-authored-by: Éric <[email protected]> Co-authored-by: Carol Willing <[email protected]>
1 parent f2315ee commit bda8190

File tree

4 files changed

+93
-18
lines changed

4 files changed

+93
-18
lines changed

Doc/library/functions.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,9 +1223,10 @@ are always available. They are listed here in alphabetical order.
12231223

12241224
.. class:: object()
12251225

1226-
Return a new featureless object. :class:`object` is a base for all classes.
1227-
It has methods that are common to all instances of Python classes. This
1228-
function does not accept any arguments.
1226+
This is the ultimate base class of all other classes. It has methods
1227+
that are common to all instances of Python classes. When the constructor
1228+
is called, it returns a new featureless object. The constructor does not
1229+
accept any arguments.
12291230

12301231
.. note::
12311232

Doc/reference/datamodel.rst

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,8 @@ Basic customization
19421942
"informal" string representation of instances of that class is required.
19431943

19441944
This is typically used for debugging, so it is important that the representation
1945-
is information-rich and unambiguous.
1945+
is information-rich and unambiguous. A default implementation is provided by the
1946+
:class:`object` class itself.
19461947

19471948
.. index::
19481949
single: string; __str__() (object method)
@@ -1952,10 +1953,10 @@ Basic customization
19521953

19531954
.. method:: object.__str__(self)
19541955

1955-
Called by :func:`str(object) <str>` and the built-in functions
1956-
:func:`format` and :func:`print` to compute the "informal" or nicely
1956+
Called by :func:`str(object) <str>`, the default :meth:`__format__` implementation,
1957+
and the built-in function :func:`print`, to compute the "informal" or nicely
19571958
printable string representation of an object. The return value must be a
1958-
:ref:`string <textseq>` object.
1959+
:ref:`str <textseq>` object.
19591960

19601961
This method differs from :meth:`object.__repr__` in that there is no
19611962
expectation that :meth:`__str__` return a valid Python expression: a more
@@ -1972,7 +1973,8 @@ Basic customization
19721973
.. index:: pair: built-in function; bytes
19731974

19741975
Called by :ref:`bytes <func-bytes>` to compute a byte-string representation
1975-
of an object. This should return a :class:`bytes` object.
1976+
of an object. This should return a :class:`bytes` object. The :class:`object`
1977+
class itself does not provide this method.
19761978

19771979
.. index::
19781980
single: string; __format__() (object method)
@@ -1996,6 +1998,9 @@ Basic customization
19961998

19971999
The return value must be a string object.
19982000

2001+
The default implementation by the :class:`object` class should be given
2002+
an empty *format_spec* string. It delegates to :meth:`__str__`.
2003+
19992004
.. versionchanged:: 3.4
20002005
The __format__ method of ``object`` itself raises a :exc:`TypeError`
20012006
if passed any non-empty string.
@@ -2038,6 +2043,12 @@ Basic customization
20382043
``(x<y or x==y)`` does not imply ``x<=y``. To automatically generate ordering
20392044
operations from a single root operation, see :func:`functools.total_ordering`.
20402045

2046+
By default, the :class:`object` class provides implementations consistent
2047+
with :ref:`expressions-value-comparisons`: equality compares according to
2048+
object identity, and order comparisons raise :exc:`TypeError`. Each default
2049+
method may generate these results directly, but may also return
2050+
:data:`NotImplemented`.
2051+
20412052
See the paragraph on :meth:`__hash__` for
20422053
some important notes on creating :term:`hashable` objects which support
20432054
custom comparison operations and are usable as dictionary keys.
@@ -2093,9 +2104,9 @@ Basic customization
20932104
bucket).
20942105

20952106
User-defined classes have :meth:`__eq__` and :meth:`__hash__` methods
2096-
by default; with them, all objects compare unequal (except with themselves)
2097-
and ``x.__hash__()`` returns an appropriate value such that ``x == y``
2098-
implies both that ``x is y`` and ``hash(x) == hash(y)``.
2107+
by default (inherited from the :class:`object` class); with them, all objects compare
2108+
unequal (except with themselves) and ``x.__hash__()`` returns an appropriate
2109+
value such that ``x == y`` implies both that ``x is y`` and ``hash(x) == hash(y)``.
20992110

21002111
A class that overrides :meth:`__eq__` and does not define :meth:`__hash__`
21012112
will have its :meth:`__hash__` implicitly set to ``None``. When the
@@ -2145,8 +2156,8 @@ Basic customization
21452156
``bool()``; should return ``False`` or ``True``. When this method is not
21462157
defined, :meth:`~object.__len__` is called, if it is defined, and the object is
21472158
considered true if its result is nonzero. If a class defines neither
2148-
:meth:`!__len__` nor :meth:`!__bool__`, all its instances are considered
2149-
true.
2159+
:meth:`!__len__` nor :meth:`!__bool__` (which is true of the :class:`object`
2160+
class itself), all its instances are considered true.
21502161

21512162

21522163
.. _attribute-access:
@@ -2168,6 +2179,7 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
21682179
for ``self``; or :meth:`__get__` of a *name* property raises
21692180
:exc:`AttributeError`). This method should either return the (computed)
21702181
attribute value or raise an :exc:`AttributeError` exception.
2182+
The :class:`object` class itself does not provide this method.
21712183

21722184
Note that if the attribute is found through the normal mechanism,
21732185
:meth:`__getattr__` is not called. (This is an intentional asymmetry between
@@ -2306,8 +2318,8 @@ method (a so-called *descriptor* class) appears in an *owner* class (the
23062318
descriptor must be in either the owner's class dictionary or in the class
23072319
dictionary for one of its parents). In the examples below, "the attribute"
23082320
refers to the attribute whose name is the key of the property in the owner
2309-
class' :attr:`~object.__dict__`.
2310-
2321+
class' :attr:`~object.__dict__`. The :class:`object` class itself does not
2322+
implement any of these protocols.
23112323

23122324
.. method:: object.__get__(self, instance, owner=None)
23132325

@@ -2999,17 +3011,19 @@ Emulating callable objects
29993011

30003012
Called when the instance is "called" as a function; if this method is defined,
30013013
``x(arg1, arg2, ...)`` roughly translates to ``type(x).__call__(x, arg1, ...)``.
3014+
The :class:`object` class itself does not provide this method.
30023015

30033016

30043017
.. _sequence-types:
30053018

30063019
Emulating container types
30073020
-------------------------
30083021

3009-
The following methods can be defined to implement container objects. Containers
3010-
usually are :term:`sequences <sequence>` (such as :class:`lists <list>` or
3022+
The following methods can be defined to implement container objects. None of them
3023+
are provided by the :class:`object` class itself. Containers usually are
3024+
:term:`sequences <sequence>` (such as :class:`lists <list>` or
30113025
:class:`tuples <tuple>`) or :term:`mappings <mapping>` (like
3012-
:class:`dictionaries <dict>`),
3026+
:term:`dictionaries <dictionary>`),
30133027
but can represent other containers as well. The first set of methods is used
30143028
either to emulate a sequence or to emulate a mapping; the difference is that for
30153029
a sequence, the allowable keys should be the integers *k* for which ``0 <= k <
@@ -3372,6 +3386,7 @@ Typical uses of context managers include saving and restoring various kinds of
33723386
global state, locking and unlocking resources, closing opened files, etc.
33733387

33743388
For more information on context managers, see :ref:`typecontextmanager`.
3389+
The :class:`object` class itself does not provide the context manager methods.
33753390

33763391

33773392
.. method:: object.__enter__(self)
@@ -3576,6 +3591,8 @@ are awaitable.
35763591
Must return an :term:`iterator`. Should be used to implement
35773592
:term:`awaitable` objects. For instance, :class:`asyncio.Future` implements
35783593
this method to be compatible with the :keyword:`await` expression.
3594+
The :class:`object` class itself is not awaitable and does not provide
3595+
this method.
35793596

35803597
.. note::
35813598

@@ -3661,6 +3678,9 @@ its ``__anext__`` method.
36613678

36623679
Asynchronous iterators can be used in an :keyword:`async for` statement.
36633680

3681+
The :class:`object` class itself does not provide these methods.
3682+
3683+
36643684
.. method:: object.__aiter__(self)
36653685

36663686
Must return an *asynchronous iterator* object.
@@ -3707,6 +3727,8 @@ suspend execution in its ``__aenter__`` and ``__aexit__`` methods.
37073727

37083728
Asynchronous context managers can be used in an :keyword:`async with` statement.
37093729

3730+
The :class:`object` class itself does not provide these methods.
3731+
37103732
.. method:: object.__aenter__(self)
37113733

37123734
Semantically similar to :meth:`~object.__enter__`, the only

Lib/test/test_class.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,56 @@ def __eq__(self, other): return 1
503503

504504
self.assertRaises(TypeError, hash, C2())
505505

506+
def testPredefinedAttrs(self):
507+
o = object()
508+
509+
class Custom:
510+
pass
511+
512+
c = Custom()
513+
514+
methods = (
515+
'__class__', '__delattr__', '__dir__', '__eq__', '__format__',
516+
'__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__',
517+
'__init__', '__init_subclass__', '__le__', '__lt__', '__ne__',
518+
'__new__', '__reduce__', '__reduce_ex__', '__repr__',
519+
'__setattr__', '__sizeof__', '__str__', '__subclasshook__'
520+
)
521+
for name in methods:
522+
with self.subTest(name):
523+
self.assertTrue(callable(getattr(object, name, None)))
524+
self.assertTrue(callable(getattr(o, name, None)))
525+
self.assertTrue(callable(getattr(Custom, name, None)))
526+
self.assertTrue(callable(getattr(c, name, None)))
527+
528+
not_defined = [
529+
'__abs__', '__aenter__', '__aexit__', '__aiter__', '__anext__',
530+
'__await__', '__bool__', '__bytes__', '__ceil__',
531+
'__complex__', '__contains__', '__del__', '__delete__',
532+
'__delitem__', '__divmod__', '__enter__', '__exit__',
533+
'__float__', '__floor__', '__get__', '__getattr__', '__getitem__',
534+
'__index__', '__int__', '__invert__', '__iter__', '__len__',
535+
'__length_hint__', '__missing__', '__neg__', '__next__',
536+
'__objclass__', '__pos__', '__rdivmod__', '__reversed__',
537+
'__round__', '__set__', '__setitem__', '__trunc__'
538+
]
539+
augment = (
540+
'add', 'and', 'floordiv', 'lshift', 'matmul', 'mod', 'mul', 'pow',
541+
'rshift', 'sub', 'truediv', 'xor'
542+
)
543+
not_defined.extend(map("__{}__".format, augment))
544+
not_defined.extend(map("__r{}__".format, augment))
545+
not_defined.extend(map("__i{}__".format, augment))
546+
for name in not_defined:
547+
with self.subTest(name):
548+
self.assertFalse(hasattr(object, name))
549+
self.assertFalse(hasattr(o, name))
550+
self.assertFalse(hasattr(Custom, name))
551+
self.assertFalse(hasattr(c, name))
552+
553+
# __call__() is defined on the metaclass but not the class
554+
self.assertFalse(hasattr(o, "__call__"))
555+
self.assertFalse(hasattr(c, "__call__"))
506556

507557
def testSFBug532646(self):
508558
# Test for SF bug 532646
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Include the :class:`object` type in the lists of documented types.
2+
Change by Furkan Onder and Martin Panter.

0 commit comments

Comments
 (0)