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

Skip to content

Commit d630e79

Browse files
committed
whatsnew: object.__format__ raises TypeError on non-empty string.
See issues #7994 and #9856. I also modified with wording of the format doc entry to better match what really happens, and added a versionchanged to the datamodel object.__format__ section.
1 parent 13b493e commit d630e79

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

Doc/library/functions.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,13 @@ are always available. They are listed here in alphabetical order.
540540
A call to ``format(value, format_spec)`` is translated to
541541
``type(value).__format__(format_spec)`` which bypasses the instance
542542
dictionary when searching for the value's :meth:`__format__` method. A
543-
:exc:`TypeError` exception is raised if the method is not found or if either
544-
the *format_spec* or the return value are not strings.
543+
:exc:`TypeError` exception is raised if the method search reaches
544+
:mod:`object` and the *format_spec* is non-empty, or if either the
545+
*format_spec* or the return value are not strings.
545546

546-
.. versionadded:: 3.4
547+
.. versionchanged:: 3.4
547548
``object().__format__(format_spec)`` raises :exc:`TypeError`
548-
if *format_spec* is not empty string.
549+
if *format_spec* is not an empty string.
549550

550551

551552
.. _func-frozenset:

Doc/reference/datamodel.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,10 @@ Basic customization
12261226

12271227
The return value must be a string object.
12281228

1229+
.. versionchanged:: 3.4
1230+
The __format__ method of ``object`` itself raises a :exc:`TypeError`
1231+
if passed any non-empty string.
1232+
12291233

12301234
.. _richcmpfuncs:
12311235
.. method:: object.__lt__(self, other)

Doc/whatsnew/3.4.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,15 @@ removed:
16001600
* :class:`inspect.Signature`: positional-only parameters are now required
16011601
to have a valid name.
16021602

1603+
* :meth:`object.__format__` no longer accepts non-empty format strings, it now
1604+
raises a :exc:`TypeError` instead. Using a non-empty string has been
1605+
deprecated since Python 3.2. This change has been made to prevent a
1606+
situation where previously working (but incorrect) code would start failing
1607+
if an object gained a __format__ method, which means that your code may now
1608+
raise a :exc:`TypeError` if you are using an ``'s'`` format code with objects
1609+
that do not have a __format__ method that handles it. See :issue:`7994` for
1610+
background.
1611+
16031612

16041613
Code Cleanups
16051614
-------------

0 commit comments

Comments
 (0)