Skip to content

Commit 96aeaec

Browse files
bpo-36793: Remove unneeded __str__ definitions. (GH-13081)
Classes that define __str__ the same as __repr__ can just inherit it from object.
1 parent 9646630 commit 96aeaec

File tree

18 files changed

+25
-33
lines changed

18 files changed

+25
-33
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,13 @@ Changes in Python behavior
811811
raised when getting the attribute from the type dictionary are no longer
812812
ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.)
813813

814+
* Removed ``__str__`` implementations from builtin types :class:`bool`,
815+
:class:`int`, :class:`float`, :class:`complex` and few classes from
816+
the standard library. They now inherit ``__str__()`` from :class:`object`.
817+
As result, defining the ``__repr__()`` method in the subclass of these
818+
classes will affect they string representation.
819+
(Contributed by Serhiy Storchaka in :issue:`36793`.)
820+
814821
* On AIX, :attr:`sys.platform` doesn't contain the major version anymore.
815822
It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``. Since
816823
older Python versions include the version number, it is recommended to

Lib/_pydecimal.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5631,8 +5631,6 @@ def __init__(self, value=None):
56315631
def __repr__(self):
56325632
return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
56335633

5634-
__str__ = __repr__
5635-
56365634

56375635

56385636
def _normalize(op1, op2, prec = 0):

Lib/asyncore.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ def __repr__(self):
262262
status.append(repr(self.addr))
263263
return '<%s at %#x>' % (' '.join(status), id(self))
264264

265-
__str__ = __repr__
266-
267265
def add_channel(self, map=None):
268266
#self.log_info('adding channel %s' % self)
269267
if map is None:

Lib/doctest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,7 @@ def __repr__(self):
23002300
name = self._dt_test.name.split('.')
23012301
return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
23022302

2303-
__str__ = __repr__
2303+
__str__ = object.__str__
23042304

23052305
def shortDescription(self):
23062306
return "Doctest: " + self._dt_test.name
@@ -2399,7 +2399,6 @@ def id(self):
23992399

24002400
def __repr__(self):
24012401
return self._dt_test.filename
2402-
__str__ = __repr__
24032402

24042403
def format_failure(self, err):
24052404
return ('Failed doctest test for %s\n File "%s", line 0\n\n%s'

Lib/email/charset.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,9 @@ def __init__(self, input_charset=DEFAULT_CHARSET):
241241
self.output_codec = CODEC_MAP.get(self.output_charset,
242242
self.output_charset)
243243

244-
def __str__(self):
244+
def __repr__(self):
245245
return self.input_charset.lower()
246246

247-
__repr__ = __str__
248-
249247
def __eq__(self, other):
250248
return str(self) == str(other).lower()
251249

Lib/http/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,7 @@ def __repr__(self):
14191419
e = ''
14201420
return '%s(%i bytes read%s)' % (self.__class__.__name__,
14211421
len(self.partial), e)
1422-
def __str__(self):
1423-
return repr(self)
1422+
__str__ = object.__str__
14241423

14251424
class ImproperConnectionState(HTTPException):
14261425
pass

Lib/json/encoder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
268268
list=list,
269269
str=str,
270270
tuple=tuple,
271-
_intstr=int.__str__,
271+
_intstr=int.__repr__,
272272
):
273273

274274
if _indent is not None and not isinstance(_indent, str):
@@ -307,7 +307,7 @@ def _iterencode_list(lst, _current_indent_level):
307307
elif value is False:
308308
yield buf + 'false'
309309
elif isinstance(value, int):
310-
# Subclasses of int/float may override __str__, but we still
310+
# Subclasses of int/float may override __repr__, but we still
311311
# want to encode them as integers/floats in JSON. One example
312312
# within the standard library is IntEnum.
313313
yield buf + _intstr(value)

Lib/logging/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,10 @@ def __init__(self, name, level, pathname, lineno,
364364
else:
365365
self.process = None
366366

367-
def __str__(self):
367+
def __repr__(self):
368368
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
369369
self.pathname, self.lineno, self.msg)
370370

371-
__repr__ = __str__
372-
373371
def getMessage(self):
374372
"""
375373
Return the message for this LogRecord.

Lib/sre_constants.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,9 @@ def __new__(cls, value, name):
5959
self.name = name
6060
return self
6161

62-
def __str__(self):
62+
def __repr__(self):
6363
return self.name
6464

65-
__repr__ = __str__
66-
6765
MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
6866

6967
def _makecodes(names):

Lib/subprocess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def __repr__(self):
203203
return "%s(%d)" % (self.__class__.__name__, int(self))
204204

205205
__del__ = Close
206-
__str__ = __repr__
207206
else:
208207
# When select or poll has indicated that the file is writable,
209208
# we can write up to _PIPE_BUF bytes without risk of blocking.

Lib/xmlrpc/client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ def escape(s):
186186

187187
class Error(Exception):
188188
"""Base class for client errors."""
189-
def __str__(self):
190-
return repr(self)
189+
__str__ = object.__str__
191190

192191
##
193192
# Indicates an HTTP-level protocol error. This is raised by the HTTP
@@ -869,8 +868,6 @@ def __init__(self, server):
869868
def __repr__(self):
870869
return "<%s at %#x>" % (self.__class__.__name__, id(self))
871870

872-
__str__ = __repr__
873-
874871
def __getattr__(self, name):
875872
return _MultiCallMethod(self.__call_list, name)
876873

@@ -1468,8 +1465,6 @@ def __repr__(self):
14681465
(self.__class__.__name__, self.__host, self.__handler)
14691466
)
14701467

1471-
__str__ = __repr__
1472-
14731468
def __getattr__(self, name):
14741469
# magic method dispatcher
14751470
return _Method(self.__request, name)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed ``__str__`` implementations from builtin types :class:`bool`,
2+
:class:`int`, :class:`float`, :class:`complex` and few classes from the
3+
standard library. They now inherit ``__str__()`` from :class:`object`.

Modules/_decimal/_decimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5390,7 +5390,7 @@ static PyTypeObject PyDecContext_Type =
53905390
0, /* tp_as_mapping */
53915391
(hashfunc) 0, /* tp_hash */
53925392
0, /* tp_call */
5393-
(reprfunc) context_repr, /* tp_str */
5393+
0, /* tp_str */
53945394
(getattrofunc) context_getattr, /* tp_getattro */
53955395
(setattrofunc) context_setattr, /* tp_setattro */
53965396
(PyBufferProcs *) 0, /* tp_as_buffer */

Modules/_json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
14821482
return _steal_accumulate(acc, encoded);
14831483
}
14841484
else if (PyLong_Check(obj)) {
1485-
PyObject *encoded = PyLong_Type.tp_str(obj);
1485+
PyObject *encoded = PyLong_Type.tp_repr(obj);
14861486
if (encoded == NULL)
14871487
return -1;
14881488
return _steal_accumulate(acc, encoded);
@@ -1646,7 +1646,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
16461646
goto bail;
16471647
}
16481648
else if (PyLong_Check(key)) {
1649-
kstr = PyLong_Type.tp_str(key);
1649+
kstr = PyLong_Type.tp_repr(key);
16501650
if (kstr == NULL) {
16511651
goto bail;
16521652
}

Objects/boolobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ PyTypeObject PyBool_Type = {
147147
0, /* tp_as_mapping */
148148
0, /* tp_hash */
149149
0, /* tp_call */
150-
bool_repr, /* tp_str */
150+
0, /* tp_str */
151151
0, /* tp_getattro */
152152
0, /* tp_setattro */
153153
0, /* tp_as_buffer */

Objects/complexobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ PyTypeObject PyComplex_Type = {
11291129
0, /* tp_as_mapping */
11301130
(hashfunc)complex_hash, /* tp_hash */
11311131
0, /* tp_call */
1132-
(reprfunc)complex_repr, /* tp_str */
1132+
0, /* tp_str */
11331133
PyObject_GenericGetAttr, /* tp_getattro */
11341134
0, /* tp_setattro */
11351135
0, /* tp_as_buffer */

Objects/floatobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ PyTypeObject PyFloat_Type = {
19231923
0, /* tp_as_mapping */
19241924
(hashfunc)float_hash, /* tp_hash */
19251925
0, /* tp_call */
1926-
(reprfunc)float_repr, /* tp_str */
1926+
0, /* tp_str */
19271927
PyObject_GenericGetAttr, /* tp_getattro */
19281928
0, /* tp_setattro */
19291929
0, /* tp_as_buffer */

Objects/longobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5592,7 +5592,7 @@ PyTypeObject PyLong_Type = {
55925592
0, /* tp_as_mapping */
55935593
(hashfunc)long_hash, /* tp_hash */
55945594
0, /* tp_call */
5595-
long_to_decimal_string, /* tp_str */
5595+
0, /* tp_str */
55965596
PyObject_GenericGetAttr, /* tp_getattro */
55975597
0, /* tp_setattro */
55985598
0, /* tp_as_buffer */

0 commit comments

Comments
 (0)