Skip to content

Commit 00ff23f

Browse files
miss-islingtonfurkanonderblurb-it[bot]CAM-Gerlachterryjreedy
authored
[3.13] gh-60712: Include the "object" type in the lists of documented types (GH-103036) (GH-126197)
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 e6e140d commit 00ff23f

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
@@ -1286,9 +1286,10 @@ are always available. They are listed here in alphabetical order.
12861286

12871287
.. class:: object()
12881288

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

12931294
.. note::
12941295

Doc/reference/datamodel.rst

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

19881988
This is typically used for debugging, so it is important that the representation
1989-
is information-rich and unambiguous.
1989+
is information-rich and unambiguous. A default implementation is provided by the
1990+
:class:`object` class itself.
19901991

19911992
.. index::
19921993
single: string; __str__() (object method)
@@ -1996,10 +1997,10 @@ Basic customization
19961997

19971998
.. method:: object.__str__(self)
19981999

1999-
Called by :func:`str(object) <str>` and the built-in functions
2000-
:func:`format` and :func:`print` to compute the "informal" or nicely
2000+
Called by :func:`str(object) <str>`, the default :meth:`__format__` implementation,
2001+
and the built-in function :func:`print`, to compute the "informal" or nicely
20012002
printable string representation of an object. The return value must be a
2002-
:ref:`string <textseq>` object.
2003+
:ref:`str <textseq>` object.
20032004

20042005
This method differs from :meth:`object.__repr__` in that there is no
20052006
expectation that :meth:`__str__` return a valid Python expression: a more
@@ -2016,7 +2017,8 @@ Basic customization
20162017
.. index:: pair: built-in function; bytes
20172018

20182019
Called by :ref:`bytes <func-bytes>` to compute a byte-string representation
2019-
of an object. This should return a :class:`bytes` object.
2020+
of an object. This should return a :class:`bytes` object. The :class:`object`
2021+
class itself does not provide this method.
20202022

20212023
.. index::
20222024
single: string; __format__() (object method)
@@ -2040,6 +2042,9 @@ Basic customization
20402042

20412043
The return value must be a string object.
20422044

2045+
The default implementation by the :class:`object` class should be given
2046+
an empty *format_spec* string. It delegates to :meth:`__str__`.
2047+
20432048
.. versionchanged:: 3.4
20442049
The __format__ method of ``object`` itself raises a :exc:`TypeError`
20452050
if passed any non-empty string.
@@ -2082,6 +2087,12 @@ Basic customization
20822087
``(x<y or x==y)`` does not imply ``x<=y``. To automatically generate ordering
20832088
operations from a single root operation, see :func:`functools.total_ordering`.
20842089

2090+
By default, the :class:`object` class provides implementations consistent
2091+
with :ref:`expressions-value-comparisons`: equality compares according to
2092+
object identity, and order comparisons raise :exc:`TypeError`. Each default
2093+
method may generate these results directly, but may also return
2094+
:data:`NotImplemented`.
2095+
20852096
See the paragraph on :meth:`__hash__` for
20862097
some important notes on creating :term:`hashable` objects which support
20872098
custom comparison operations and are usable as dictionary keys.
@@ -2137,9 +2148,9 @@ Basic customization
21372148
bucket).
21382149

21392150
User-defined classes have :meth:`__eq__` and :meth:`__hash__` methods
2140-
by default; with them, all objects compare unequal (except with themselves)
2141-
and ``x.__hash__()`` returns an appropriate value such that ``x == y``
2142-
implies both that ``x is y`` and ``hash(x) == hash(y)``.
2151+
by default (inherited from the :class:`object` class); with them, all objects compare
2152+
unequal (except with themselves) and ``x.__hash__()`` returns an appropriate
2153+
value such that ``x == y`` implies both that ``x is y`` and ``hash(x) == hash(y)``.
21432154

21442155
A class that overrides :meth:`__eq__` and does not define :meth:`__hash__`
21452156
will have its :meth:`__hash__` implicitly set to ``None``. When the
@@ -2189,8 +2200,8 @@ Basic customization
21892200
``bool()``; should return ``False`` or ``True``. When this method is not
21902201
defined, :meth:`~object.__len__` is called, if it is defined, and the object is
21912202
considered true if its result is nonzero. If a class defines neither
2192-
:meth:`!__len__` nor :meth:`!__bool__`, all its instances are considered
2193-
true.
2203+
:meth:`!__len__` nor :meth:`!__bool__` (which is true of the :class:`object`
2204+
class itself), all its instances are considered true.
21942205

21952206

21962207
.. _attribute-access:
@@ -2212,6 +2223,7 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
22122223
for ``self``; or :meth:`__get__` of a *name* property raises
22132224
:exc:`AttributeError`). This method should either return the (computed)
22142225
attribute value or raise an :exc:`AttributeError` exception.
2226+
The :class:`object` class itself does not provide this method.
22152227

22162228
Note that if the attribute is found through the normal mechanism,
22172229
:meth:`__getattr__` is not called. (This is an intentional asymmetry between
@@ -2350,8 +2362,8 @@ method (a so-called *descriptor* class) appears in an *owner* class (the
23502362
descriptor must be in either the owner's class dictionary or in the class
23512363
dictionary for one of its parents). In the examples below, "the attribute"
23522364
refers to the attribute whose name is the key of the property in the owner
2353-
class' :attr:`~object.__dict__`.
2354-
2365+
class' :attr:`~object.__dict__`. The :class:`object` class itself does not
2366+
implement any of these protocols.
23552367

23562368
.. method:: object.__get__(self, instance, owner=None)
23572369

@@ -3043,17 +3055,19 @@ Emulating callable objects
30433055

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

30473060

30483061
.. _sequence-types:
30493062

30503063
Emulating container types
30513064
-------------------------
30523065

3053-
The following methods can be defined to implement container objects. Containers
3054-
usually are :term:`sequences <sequence>` (such as :class:`lists <list>` or
3066+
The following methods can be defined to implement container objects. None of them
3067+
are provided by the :class:`object` class itself. Containers usually are
3068+
:term:`sequences <sequence>` (such as :class:`lists <list>` or
30553069
:class:`tuples <tuple>`) or :term:`mappings <mapping>` (like
3056-
:class:`dictionaries <dict>`),
3070+
:term:`dictionaries <dictionary>`),
30573071
but can represent other containers as well. The first set of methods is used
30583072
either to emulate a sequence or to emulate a mapping; the difference is that for
30593073
a sequence, the allowable keys should be the integers *k* for which ``0 <= k <
@@ -3416,6 +3430,7 @@ Typical uses of context managers include saving and restoring various kinds of
34163430
global state, locking and unlocking resources, closing opened files, etc.
34173431

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

34203435

34213436
.. method:: object.__enter__(self)
@@ -3620,6 +3635,8 @@ are awaitable.
36203635
Must return an :term:`iterator`. Should be used to implement
36213636
:term:`awaitable` objects. For instance, :class:`asyncio.Future` implements
36223637
this method to be compatible with the :keyword:`await` expression.
3638+
The :class:`object` class itself is not awaitable and does not provide
3639+
this method.
36233640

36243641
.. note::
36253642

@@ -3705,6 +3722,9 @@ its ``__anext__`` method.
37053722

37063723
Asynchronous iterators can be used in an :keyword:`async for` statement.
37073724

3725+
The :class:`object` class itself does not provide these methods.
3726+
3727+
37083728
.. method:: object.__aiter__(self)
37093729

37103730
Must return an *asynchronous iterator* object.
@@ -3751,6 +3771,8 @@ suspend execution in its ``__aenter__`` and ``__aexit__`` methods.
37513771

37523772
Asynchronous context managers can be used in an :keyword:`async with` statement.
37533773

3774+
The :class:`object` class itself does not provide these methods.
3775+
37543776
.. method:: object.__aenter__(self)
37553777

37563778
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)