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

Skip to content

bpo-36793: Remove unneeded __str__ definitions. #13081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,13 @@ Changes in Python behavior
raised when getting the attribute from the type dictionary are no longer
ignored. (Contributed by Serhiy Storchaka in :issue:`35459`.)

* Removed ``__str__`` implementations from builtin types :class:`bool`,
:class:`int`, :class:`float`, :class:`complex` and few classes from
the standard library. They now inherit ``__str__()`` from :class:`object`.
As result, defining the ``__repr__()`` method in the subclass of these
classes will affect they string representation.
(Contributed by Serhiy Storchaka in :issue:`36793`.)

* On AIX, :attr:`sys.platform` doesn't contain the major version anymore.
It is always ``'aix'``, instead of ``'aix3'`` .. ``'aix7'``. Since
older Python versions include the version number, it is recommended to
Expand Down
2 changes: 0 additions & 2 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5631,8 +5631,6 @@ def __init__(self, value=None):
def __repr__(self):
return "(%r, %r, %r)" % (self.sign, self.int, self.exp)

__str__ = __repr__



def _normalize(op1, op2, prec = 0):
Expand Down
2 changes: 0 additions & 2 deletions Lib/asyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ def __repr__(self):
status.append(repr(self.addr))
return '<%s at %#x>' % (' '.join(status), id(self))

__str__ = __repr__

def add_channel(self, map=None):
#self.log_info('adding channel %s' % self)
if map is None:
Expand Down
3 changes: 1 addition & 2 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ def __repr__(self):
name = self._dt_test.name.split('.')
return "%s (%s)" % (name[-1], '.'.join(name[:-1]))

__str__ = __repr__
__str__ = object.__str__

def shortDescription(self):
return "Doctest: " + self._dt_test.name
Expand Down Expand Up @@ -2399,7 +2399,6 @@ def id(self):

def __repr__(self):
return self._dt_test.filename
__str__ = __repr__

def format_failure(self, err):
return ('Failed doctest test for %s\n File "%s", line 0\n\n%s'
Expand Down
4 changes: 1 addition & 3 deletions Lib/email/charset.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,9 @@ def __init__(self, input_charset=DEFAULT_CHARSET):
self.output_codec = CODEC_MAP.get(self.output_charset,
self.output_charset)

def __str__(self):
def __repr__(self):
return self.input_charset.lower()

__repr__ = __str__

def __eq__(self, other):
return str(self) == str(other).lower()

Expand Down
3 changes: 1 addition & 2 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,8 +1419,7 @@ def __repr__(self):
e = ''
return '%s(%i bytes read%s)' % (self.__class__.__name__,
len(self.partial), e)
def __str__(self):
return repr(self)
__str__ = object.__str__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(just because I'm curious) in some cases this line was removed, in some cases it was moved to object.__str__ -- is there a heuristic you used here or ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is removed if no parent class except object defines __str__. But if it was changed in the parent class (in BaseException in this case) we need to set it explicitly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the clarification 👍


class ImproperConnectionState(HTTPException):
pass
Expand Down
4 changes: 2 additions & 2 deletions Lib/json/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
list=list,
str=str,
tuple=tuple,
_intstr=int.__str__,
_intstr=int.__repr__,
):

if _indent is not None and not isinstance(_indent, str):
Expand Down Expand Up @@ -307,7 +307,7 @@ def _iterencode_list(lst, _current_indent_level):
elif value is False:
yield buf + 'false'
elif isinstance(value, int):
# Subclasses of int/float may override __str__, but we still
# Subclasses of int/float may override __repr__, but we still
# want to encode them as integers/floats in JSON. One example
# within the standard library is IntEnum.
yield buf + _intstr(value)
Expand Down
4 changes: 1 addition & 3 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,10 @@ def __init__(self, name, level, pathname, lineno,
else:
self.process = None

def __str__(self):
def __repr__(self):
return '<LogRecord: %s, %s, %s, %s, "%s">'%(self.name, self.levelno,
self.pathname, self.lineno, self.msg)

__repr__ = __str__

def getMessage(self):
"""
Return the message for this LogRecord.
Expand Down
4 changes: 1 addition & 3 deletions Lib/sre_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ def __new__(cls, value, name):
self.name = name
return self

def __str__(self):
def __repr__(self):
return self.name

__repr__ = __str__

MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')

def _makecodes(names):
Expand Down
1 change: 0 additions & 1 deletion Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ def __repr__(self):
return "%s(%d)" % (self.__class__.__name__, int(self))

__del__ = Close
__str__ = __repr__
else:
# When select or poll has indicated that the file is writable,
# we can write up to _PIPE_BUF bytes without risk of blocking.
Expand Down
7 changes: 1 addition & 6 deletions Lib/xmlrpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ def escape(s):

class Error(Exception):
"""Base class for client errors."""
def __str__(self):
return repr(self)
__str__ = object.__str__

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

__str__ = __repr__

def __getattr__(self, name):
return _MultiCallMethod(self.__call_list, name)

Expand Down Expand Up @@ -1468,8 +1465,6 @@ def __repr__(self):
(self.__class__.__name__, self.__host, self.__handler)
)

__str__ = __repr__

def __getattr__(self, name):
# magic method dispatcher
return _Method(self.__request, name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Removed ``__str__`` implementations from builtin types :class:`bool`,
:class:`int`, :class:`float`, :class:`complex` and few classes from the
standard library. They now inherit ``__str__()`` from :class:`object`.
2 changes: 1 addition & 1 deletion Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -5390,7 +5390,7 @@ static PyTypeObject PyDecContext_Type =
0, /* tp_as_mapping */
(hashfunc) 0, /* tp_hash */
0, /* tp_call */
(reprfunc) context_repr, /* tp_str */
0, /* tp_str */
(getattrofunc) context_getattr, /* tp_getattro */
(setattrofunc) context_setattr, /* tp_setattro */
(PyBufferProcs *) 0, /* tp_as_buffer */
Expand Down
4 changes: 2 additions & 2 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ encoder_listencode_obj(PyEncoderObject *s, _PyAccu *acc,
return _steal_accumulate(acc, encoded);
}
else if (PyLong_Check(obj)) {
PyObject *encoded = PyLong_Type.tp_str(obj);
PyObject *encoded = PyLong_Type.tp_repr(obj);
if (encoded == NULL)
return -1;
return _steal_accumulate(acc, encoded);
Expand Down Expand Up @@ -1646,7 +1646,7 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
goto bail;
}
else if (PyLong_Check(key)) {
kstr = PyLong_Type.tp_str(key);
kstr = PyLong_Type.tp_repr(key);
if (kstr == NULL) {
goto bail;
}
Expand Down
2 changes: 1 addition & 1 deletion Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ PyTypeObject PyBool_Type = {
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
bool_repr, /* tp_str */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down
2 changes: 1 addition & 1 deletion Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1129,7 +1129,7 @@ PyTypeObject PyComplex_Type = {
0, /* tp_as_mapping */
(hashfunc)complex_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)complex_repr, /* tp_str */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down
2 changes: 1 addition & 1 deletion Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1923,7 +1923,7 @@ PyTypeObject PyFloat_Type = {
0, /* tp_as_mapping */
(hashfunc)float_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)float_repr, /* tp_str */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down
2 changes: 1 addition & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5592,7 +5592,7 @@ PyTypeObject PyLong_Type = {
0, /* tp_as_mapping */
(hashfunc)long_hash, /* tp_hash */
0, /* tp_call */
long_to_decimal_string, /* tp_str */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Expand Down