Skip to content

bpo-40995: reprlib.Repr attributes can be overriden in __init__() #20925

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

Closed
wants to merge 1 commit into from
Closed
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
91 changes: 48 additions & 43 deletions Doc/library/reprlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ debugger and may be useful in other contexts as well.
This module provides a class, an instance, and a function:


.. class:: Repr()
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \
Copy link
Contributor

Choose a reason for hiding this comment

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

self should not be included here.

maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \
maxlong=40, maxother=30)
Comment on lines +21 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

Please align these lines with the (.


Class which provides formatting services useful in implementing functions
similar to the built-in :func:`repr`; size limits for different object types
Expand Down Expand Up @@ -71,72 +73,75 @@ string instead.
Repr Objects
------------

:class:`Repr` instances provide several attributes which can be used to provide
size limits for the representations of different object types, and methods
which format specific object types.
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \
Copy link
Contributor

Choose a reason for hiding this comment

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

self should not be included here.

maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \
maxlong=40, maxother=30)
Comment on lines +77 to +78
Copy link
Contributor

Choose a reason for hiding this comment

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

Please align these lines with the (.


:class:`Repr` instances provide several attributes which can be used to provide
Copy link
Contributor

Choose a reason for hiding this comment

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

Please limit lines to 80 characters in reST files (https://devguide.python.org/documenting/#use-of-whitespace).

size limits for the representations of different object types, and methods
which format specific object types.

.. attribute:: Repr.maxlevel
.. attribute:: maxlevel

Depth limit on the creation of recursive representations. The default is ``6``.
Depth limit on the creation of recursive representations. The default is ``6``.


.. attribute:: Repr.maxdict
Repr.maxlist
Repr.maxtuple
Repr.maxset
Repr.maxfrozenset
Repr.maxdeque
Repr.maxarray
.. attribute:: maxdict
maxlist
maxtuple
maxset
maxfrozenset
maxdeque
maxarray

Limits on the number of entries represented for the named object type. The
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
the others.
Limits on the number of entries represented for the named object type. The
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
the others.


.. attribute:: Repr.maxlong
.. attribute:: maxlong

Maximum number of characters in the representation for an integer. Digits
are dropped from the middle. The default is ``40``.
Maximum number of characters in the representation for an integer. Digits
are dropped from the middle. The default is ``40``.


.. attribute:: Repr.maxstring
.. attribute:: maxstring

Limit on the number of characters in the representation of the string. Note
that the "normal" representation of the string is used as the character source:
if escape sequences are needed in the representation, these may be mangled when
the representation is shortened. The default is ``30``.
Limit on the number of characters in the representation of the string. Note
that the "normal" representation of the string is used as the character source:
if escape sequences are needed in the representation, these may be mangled when
the representation is shortened. The default is ``30``.


.. attribute:: Repr.maxother
.. attribute:: maxother

This limit is used to control the size of object types for which no specific
formatting method is available on the :class:`Repr` object. It is applied in a
similar manner as :attr:`maxstring`. The default is ``20``.
This limit is used to control the size of object types for which no specific
formatting method is available on the :class:`Repr` object. It is applied in a
similar manner as :attr:`maxstring`. The default is ``20``.


.. method:: Repr.repr(obj)
.. method:: repr(obj)

The equivalent to the built-in :func:`repr` that uses the formatting imposed by
the instance.
The equivalent to the built-in :func:`repr` that uses the formatting imposed by
the instance.


.. method:: Repr.repr1(obj, level)
.. method:: repr1(obj, level)

Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
determine which formatting method to call, passing it *obj* and *level*. The
type-specific methods should call :meth:`repr1` to perform recursive formatting,
with ``level - 1`` for the value of *level* in the recursive call.
Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
determine which formatting method to call, passing it *obj* and *level*. The
type-specific methods should call :meth:`repr1` to perform recursive formatting,
with ``level - 1`` for the value of *level* in the recursive call.


.. method:: Repr.repr_TYPE(obj, level)
:noindex:
.. method:: repr_TYPE(obj, level)
:noindex:

Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, **TYPE** is replaced by
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is
handled by :meth:`repr1`. Type-specific methods which need to recursively
format a value should call ``self.repr1(subobj, level - 1)``.
Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, **TYPE** is replaced by
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is
handled by :meth:`repr1`. Type-specific methods which need to recursively
format a value should call ``self.repr1(subobj, level - 1)``.


.. _subclassing-reprs:
Expand Down
27 changes: 15 additions & 12 deletions Lib/reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,23 @@ def wrapper(self):

return decorating_function


class Repr:

def __init__(self):
self.maxlevel = 6
self.maxtuple = 6
self.maxlist = 6
self.maxarray = 5
self.maxdict = 4
self.maxset = 6
self.maxfrozenset = 6
self.maxdeque = 6
self.maxstring = 30
self.maxlong = 40
self.maxother = 30
def __init__(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5,
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30,
maxlong=40, maxother=30):
self.maxlevel = maxlevel
self.maxtuple = maxtuple
self.maxlist = maxlist
self.maxarray = maxarray
self.maxdict = maxdict
self.maxset = maxset
self.maxfrozenset = maxfrozenset
self.maxdeque = maxdeque
self.maxstring = maxstring
self.maxlong = maxlong
self.maxother = maxother

def repr(self, x):
return self.repr1(x, self.maxlevel)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ def test_unsortable(self):
r(y)
r(z)

def test___init__(self):
a = "a"*40
r = Repr()
self.assertEqual(r.repr(a), "'aaaaaaaaaaaa...aaaaaaaaaaaaa'")
r = Repr(maxstring=10)
self.assertEqual(r.repr(a), "'aa...aaa'")
Copy link
Contributor

Choose a reason for hiding this comment

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

I think more tests need to be provided.


def write_file(path, text):
with open(path, 'w', encoding='ASCII') as fp:
fp.write(text)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`reprlib.Repr` attributes can now be set when creating an instance.
Patch contributed by Rémi Lapeyre.