The formatted representation keeps objects on a single line if it can, and
breaks them onto multiple lines if they don't fit within the allowed width,
-adjustable by the *width* parameter defaulting to 80 characters.
+adjustable by the *width* parameter defaulting to 88 characters.
.. versionchanged:: 3.9
Added support for pretty-printing :class:`types.SimpleNamespace`.
Functions
---------
-.. function:: pp(object, stream=None, indent=1, width=80, depth=None, *, \
- compact=False, expand=False, sort_dicts=False, \
- underscore_numbers=False)
+.. function:: pp(object, stream=None, indent=4, width=88, depth=None, *, \
+ compact=False, sort_dicts=False, underscore_numbers=False)
Prints the formatted representation of *object*, followed by a newline.
This function may be used in the interactive interpreter
:param bool compact:
Control the way long :term:`sequences <sequence>` are formatted.
If ``False`` (the default),
- each item of a sequence will be formatted on a separate line,
- otherwise as many items as will fit within the *width*
- will be formatted on each output line.
- Incompatible with *expand*.
-
- :param bool expand:
- If ``True``,
opening parentheses and brackets will be followed by a newline and the
following content will be indented by one level, similar to
- pretty-printed JSON. Incompatible with *compact*.
+ pretty-printed JSON.
+ If ``True``, as many items as will fit within the *width*
+ will be formatted on each output line.
:param bool sort_dicts:
If ``True``, dictionaries will be formatted with
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
- >>> pprint.pp(stuff)
- [<Recursion on list with id=...>,
- 'spam',
- 'eggs',
- 'lumberjack',
- 'knights',
- 'ni']
+ >>> pprint.pp(stuff, width=100)
+ [<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']
.. versionadded:: 3.8
-.. function:: pprint(object, stream=None, indent=1, width=80, depth=None, *, \
- compact=False, expand=False, sort_dicts=True, \
- underscore_numbers=False)
+.. function:: pprint(object, stream=None, indent=4, width=88, depth=None, *, \
+ compact=False, sort_dicts=True, underscore_numbers=False)
Alias for :func:`~pprint.pp` with *sort_dicts* set to ``True`` by default,
which would automatically sort the dictionaries' keys,
you might want to use :func:`~pprint.pp` instead where it is ``False`` by default.
-.. function:: pformat(object, indent=1, width=80, depth=None, *, \
- compact=False, expand=False, sort_dicts=True, \
- underscore_numbers=False)
+.. function:: pformat(object, indent=4, width=88, depth=None, *, \
+ compact=False, sort_dicts=True, underscore_numbers=False)
Return the formatted representation of *object* as a string. *indent*,
- *width*, *depth*, *compact*, *expand*, *sort_dicts* and *underscore_numbers* are
+ *width*, *depth*, *compact*, *sort_dicts* and *underscore_numbers* are
passed to the :class:`PrettyPrinter` constructor as formatting parameters
and their meanings are as described in the documentation above.
.. _prettyprinter-objects:
-PrettyPrinter Objects
+PrettyPrinter objects
---------------------
.. index:: single: ...; placeholder
-.. class:: PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, \
- compact=False, expand=False, sort_dicts=True, \
+.. class:: PrettyPrinter(indent=4, width=88, depth=None, stream=None, *, \
+ compact=False, sort_dicts=True, \
underscore_numbers=False)
Construct a :class:`PrettyPrinter` instance.
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
- >>> pp = pprint.PrettyPrinter(indent=4)
+ >>> pp = pprint.PrettyPrinter()
>>> pp.pprint(stuff)
- [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
+ [
+ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
'spam',
'eggs',
'lumberjack',
'knights',
- 'ni']
- >>> pp = pprint.PrettyPrinter(width=41, compact=True)
+ 'ni',
+ ]
+ >>> pp = pprint.PrettyPrinter(indent=1, width=41, compact=True)
>>> pp.pprint(stuff)
[['spam', 'eggs', 'lumberjack',
'knights', 'ni'],
'spam', 'eggs', 'lumberjack', 'knights',
'ni']
- >>> pp = pprint.PrettyPrinter(width=41, expand=True, indent=3)
+ >>> pp = pprint.PrettyPrinter(width=41, indent=3)
>>> pp.pprint(stuff)
[
[
.. versionchanged:: 3.11
No longer attempts to write to :data:`!sys.stdout` if it is ``None``.
- .. versionchanged:: 3.15
- Added the *expand* parameter.
+ .. versionchanged:: next
+ Changed default *indent* from 1 to 4
+ and default *width* from 80 to 88.
+ The default ``compact=False`` layout is now similar to
+ pretty-printed JSON, with opening parentheses and brackets
+ followed by a newline and the contents indented by one level.
:class:`PrettyPrinter` instances have the following methods:
In its basic form, :func:`~pprint.pp` shows the whole object::
>>> pprint.pp(project_info)
- {'author': 'The Python Packaging Authority',
- 'author_email': 'pypa-dev@googlegroups.com',
- 'bugtrack_url': None,
- 'classifiers': ['Development Status :: 3 - Alpha',
- 'Intended Audience :: Developers',
- 'License :: OSI Approved :: MIT License',
- 'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.6',
- 'Programming Language :: Python :: 2.7',
- 'Programming Language :: Python :: 3',
- 'Programming Language :: Python :: 3.2',
- 'Programming Language :: Python :: 3.3',
- 'Programming Language :: Python :: 3.4',
- 'Topic :: Software Development :: Build Tools'],
- 'description': 'A sample Python project\n'
- '=======================\n'
- '\n'
- 'This is the description file for the project.\n'
- '\n'
- 'The file should use UTF-8 encoding and be written using '
- 'ReStructured Text. It\n'
- 'will be used to generate the project webpage on PyPI, and '
- 'should be written for\n'
- 'that purpose.\n'
- '\n'
- 'Typical contents for this file would include an overview of '
- 'the project, basic\n'
- 'usage examples, etc. Generally, including the project '
- 'changelog in here is not\n'
- 'a good idea, although a simple "What\'s New" section for the '
- 'most recent version\n'
- 'may be appropriate.',
- 'description_content_type': None,
- 'docs_url': None,
- 'download_url': 'UNKNOWN',
- 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
- 'home_page': 'https://github.com/pypa/sampleproject',
- 'keywords': 'sample setuptools development',
- 'license': 'MIT',
- 'maintainer': None,
- 'maintainer_email': None,
- 'name': 'sampleproject',
- 'package_url': 'https://pypi.org/project/sampleproject/',
- 'platform': 'UNKNOWN',
- 'project_url': 'https://pypi.org/project/sampleproject/',
- 'project_urls': {'Download': 'UNKNOWN',
- 'Homepage': 'https://github.com/pypa/sampleproject'},
- 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
- 'requires_dist': None,
- 'requires_python': None,
- 'summary': 'A sample Python project',
- 'version': '1.2.0'}
+ {
+ 'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ 'bugtrack_url': None,
+ 'classifiers': [
+ 'Development Status :: 3 - Alpha',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: MIT License',
+ 'Programming Language :: Python :: 2',
+ 'Programming Language :: Python :: 2.6',
+ 'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3.2',
+ 'Programming Language :: Python :: 3.3',
+ 'Programming Language :: Python :: 3.4',
+ 'Topic :: Software Development :: Build Tools',
+ ],
+ 'description': 'A sample Python project\n'
+ '=======================\n'
+ '\n'
+ 'This is the description file for the project.\n'
+ '\n'
+ 'The file should use UTF-8 encoding and be written using ReStructured Text. It\n'
+ 'will be used to generate the project webpage on PyPI, and should be written for\n'
+ 'that purpose.\n'
+ '\n'
+ 'Typical contents for this file would include an overview of the project, basic\n'
+ 'usage examples, etc. Generally, including the project changelog in here is not\n'
+ 'a good idea, although a simple "What\'s New" section for the most recent version\n'
+ 'may be appropriate.',
+ 'description_content_type': None,
+ 'docs_url': None,
+ 'download_url': 'UNKNOWN',
+ 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
+ 'home_page': 'https://github.com/pypa/sampleproject',
+ 'keywords': 'sample setuptools development',
+ 'license': 'MIT',
+ 'maintainer': None,
+ 'maintainer_email': None,
+ 'name': 'sampleproject',
+ 'package_url': 'https://pypi.org/project/sampleproject/',
+ 'platform': 'UNKNOWN',
+ 'project_url': 'https://pypi.org/project/sampleproject/',
+ 'project_urls': {'Download': 'UNKNOWN', 'Homepage': 'https://github.com/pypa/sampleproject'},
+ 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
+ 'requires_dist': None,
+ 'requires_python': None,
+ 'summary': 'A sample Python project',
+ 'version': '1.2.0',
+ }
The result can be limited to a certain *depth* (ellipsis is used for deeper
contents)::
>>> pprint.pp(project_info, depth=1)
- {'author': 'The Python Packaging Authority',
- 'author_email': 'pypa-dev@googlegroups.com',
- 'bugtrack_url': None,
- 'classifiers': [...],
- 'description': 'A sample Python project\n'
- '=======================\n'
- '\n'
- 'This is the description file for the project.\n'
- '\n'
- 'The file should use UTF-8 encoding and be written using '
- 'ReStructured Text. It\n'
- 'will be used to generate the project webpage on PyPI, and '
- 'should be written for\n'
- 'that purpose.\n'
- '\n'
- 'Typical contents for this file would include an overview of '
- 'the project, basic\n'
- 'usage examples, etc. Generally, including the project '
- 'changelog in here is not\n'
- 'a good idea, although a simple "What\'s New" section for the '
- 'most recent version\n'
- 'may be appropriate.',
- 'description_content_type': None,
- 'docs_url': None,
- 'download_url': 'UNKNOWN',
- 'downloads': {...},
- 'home_page': 'https://github.com/pypa/sampleproject',
- 'keywords': 'sample setuptools development',
- 'license': 'MIT',
- 'maintainer': None,
- 'maintainer_email': None,
- 'name': 'sampleproject',
- 'package_url': 'https://pypi.org/project/sampleproject/',
- 'platform': 'UNKNOWN',
- 'project_url': 'https://pypi.org/project/sampleproject/',
- 'project_urls': {...},
- 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
- 'requires_dist': None,
- 'requires_python': None,
- 'summary': 'A sample Python project',
- 'version': '1.2.0'}
+ {
+ 'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ 'bugtrack_url': None,
+ 'classifiers': [...],
+ 'description': 'A sample Python project\n'
+ '=======================\n'
+ '\n'
+ 'This is the description file for the project.\n'
+ '\n'
+ 'The file should use UTF-8 encoding and be written using ReStructured Text. It\n'
+ 'will be used to generate the project webpage on PyPI, and should be written for\n'
+ 'that purpose.\n'
+ '\n'
+ 'Typical contents for this file would include an overview of the project, basic\n'
+ 'usage examples, etc. Generally, including the project changelog in here is not\n'
+ 'a good idea, although a simple "What\'s New" section for the most recent version\n'
+ 'may be appropriate.',
+ 'description_content_type': None,
+ 'docs_url': None,
+ 'download_url': 'UNKNOWN',
+ 'downloads': {...},
+ 'home_page': 'https://github.com/pypa/sampleproject',
+ 'keywords': 'sample setuptools development',
+ 'license': 'MIT',
+ 'maintainer': None,
+ 'maintainer_email': None,
+ 'name': 'sampleproject',
+ 'package_url': 'https://pypi.org/project/sampleproject/',
+ 'platform': 'UNKNOWN',
+ 'project_url': 'https://pypi.org/project/sampleproject/',
+ 'project_urls': {...},
+ 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
+ 'requires_dist': None,
+ 'requires_python': None,
+ 'summary': 'A sample Python project',
+ 'version': '1.2.0',
+ }
Additionally, maximum character *width* can be suggested. If a long object
cannot be split, the specified width will be exceeded::
>>> pprint.pp(project_info, depth=1, width=60)
- {'author': 'The Python Packaging Authority',
- 'author_email': 'pypa-dev@googlegroups.com',
- 'bugtrack_url': None,
- 'classifiers': [...],
- 'description': 'A sample Python project\n'
- '=======================\n'
- '\n'
- 'This is the description file for the '
- 'project.\n'
- '\n'
- 'The file should use UTF-8 encoding and be '
- 'written using ReStructured Text. It\n'
- 'will be used to generate the project '
- 'webpage on PyPI, and should be written '
- 'for\n'
- 'that purpose.\n'
- '\n'
- 'Typical contents for this file would '
- 'include an overview of the project, '
- 'basic\n'
- 'usage examples, etc. Generally, including '
- 'the project changelog in here is not\n'
- 'a good idea, although a simple "What\'s '
- 'New" section for the most recent version\n'
- 'may be appropriate.',
- 'description_content_type': None,
- 'docs_url': None,
- 'download_url': 'UNKNOWN',
- 'downloads': {...},
- 'home_page': 'https://github.com/pypa/sampleproject',
- 'keywords': 'sample setuptools development',
- 'license': 'MIT',
- 'maintainer': None,
- 'maintainer_email': None,
- 'name': 'sampleproject',
- 'package_url': 'https://pypi.org/project/sampleproject/',
- 'platform': 'UNKNOWN',
- 'project_url': 'https://pypi.org/project/sampleproject/',
- 'project_urls': {...},
- 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
- 'requires_dist': None,
- 'requires_python': None,
- 'summary': 'A sample Python project',
- 'version': '1.2.0'}
-
-Lastly, we can format like pretty-printed JSON with the *expand* parameter.
-Best results are achieved with a higher *indent* value::
-
- >>> pprint.pp(project_info, indent=4, expand=True)
{
- 'author': 'The Python Packaging Authority',
- 'author_email': 'pypa-dev@googlegroups.com',
- 'bugtrack_url': None,
- 'classifiers': [
- 'Development Status :: 3 - Alpha',
- 'Intended Audience :: Developers',
- 'License :: OSI Approved :: MIT License',
- 'Programming Language :: Python :: 2',
- 'Programming Language :: Python :: 2.6',
- 'Programming Language :: Python :: 2.7',
- 'Programming Language :: Python :: 3',
- 'Programming Language :: Python :: 3.2',
- 'Programming Language :: Python :: 3.3',
- 'Programming Language :: Python :: 3.4',
- 'Topic :: Software Development :: Build Tools',
- ],
- 'description': 'A sample Python project\n'
- '=======================\n'
- '\n'
- 'This is the description file for the project.\n'
- '\n'
- 'The file should use UTF-8 encoding and be written using ReStructured '
- 'Text. It\n'
- 'will be used to generate the project webpage on PyPI, and should be '
- 'written for\n'
- 'that purpose.\n'
- '\n'
- 'Typical contents for this file would include an overview of the project, '
- 'basic\n'
- 'usage examples, etc. Generally, including the project changelog in here '
- 'is not\n'
- 'a good idea, although a simple "What\'s New" section for the most recent '
- 'version\n'
- 'may be appropriate.',
- 'description_content_type': None,
- 'docs_url': None,
- 'download_url': 'UNKNOWN',
- 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
- 'dynamic': None,
- 'home_page': 'https://github.com/pypa/sampleproject',
- 'keywords': 'sample setuptools development',
- 'license': 'MIT',
- 'license_expression': None,
- 'license_files': None,
- 'maintainer': None,
- 'maintainer_email': None,
- 'name': 'sampleproject',
- 'package_url': 'https://pypi.org/project/sampleproject/',
- 'platform': 'UNKNOWN',
- 'project_url': 'https://pypi.org/project/sampleproject/',
- 'project_urls': {
- 'Download': 'UNKNOWN',
- 'Homepage': 'https://github.com/pypa/sampleproject',
- },
- 'provides_extra': None,
- 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
- 'requires_dist': None,
- 'requires_python': None,
- 'summary': 'A sample Python project',
- 'version': '1.2.0',
- 'yanked': False,
- 'yanked_reason': None,
+ 'author': 'The Python Packaging Authority',
+ 'author_email': 'pypa-dev@googlegroups.com',
+ 'bugtrack_url': None,
+ 'classifiers': [...],
+ 'description': 'A sample Python project\n'
+ '=======================\n'
+ '\n'
+ 'This is the description file for the project.\n'
+ '\n'
+ 'The file should use UTF-8 encoding and be written '
+ 'using ReStructured Text. It\n'
+ 'will be used to generate the project webpage on PyPI, '
+ 'and should be written for\n'
+ 'that purpose.\n'
+ '\n'
+ 'Typical contents for this file would include an '
+ 'overview of the project, basic\n'
+ 'usage examples, etc. Generally, including the project '
+ 'changelog in here is not\n'
+ 'a good idea, although a simple "What\'s New" section '
+ 'for the most recent version\n'
+ 'may be appropriate.',
+ 'description_content_type': None,
+ 'docs_url': None,
+ 'download_url': 'UNKNOWN',
+ 'downloads': {...},
+ 'home_page': 'https://github.com/pypa/sampleproject',
+ 'keywords': 'sample setuptools development',
+ 'license': 'MIT',
+ 'maintainer': None,
+ 'maintainer_email': None,
+ 'name': 'sampleproject',
+ 'package_url': 'https://pypi.org/project/sampleproject/',
+ 'platform': 'UNKNOWN',
+ 'project_url': 'https://pypi.org/project/sampleproject/',
+ 'project_urls': {...},
+ 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
+ 'requires_dist': None,
+ 'requires_python': None,
+ 'summary': 'A sample Python project',
+ 'version': '1.2.0',
}
import collections
import contextlib
import dataclasses
+import functools
import io
import itertools
import pprint
from test.support import cpython_only
from test.support.import_helper import ensure_lazy_imports
+# Pin pre-3.15 width/indent for existing formatting tests.
+# compact=True keeps the legacy non-JSON-style container wrapping.
+_pformat = functools.partial(pprint.pformat, indent=1, width=80, compact=True)
+
# list, tuple and dict subclasses that do or don't overwrite __repr__
class list2(list):
pass
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=0)
self.assertRaises(ValueError, pprint.PrettyPrinter, depth=-1)
self.assertRaises(ValueError, pprint.PrettyPrinter, width=0)
- self.assertRaises(ValueError, pprint.PrettyPrinter, compact=True, expand=True)
def test_basic(self):
# Verify .isrecursive() and .isreadable() w/o recursion
True, False, None, ...,
):
native = repr(simple)
- self.assertEqual(pprint.pformat(simple), native)
- self.assertEqual(pprint.pformat(simple, width=1, indent=0)
+ self.assertEqual(_pformat(simple), native)
+ self.assertEqual(_pformat(simple, width=1, indent=0)
.replace('\n', ' '), native)
- self.assertEqual(pprint.pformat(simple, underscore_numbers=True), native)
+ self.assertEqual(_pformat(simple, underscore_numbers=True), native)
self.assertEqual(pprint.saferepr(simple), native)
def test_container_repr_override_called(self):
):
native = repr(cont)
expected = '*' * len(native)
- self.assertEqual(pprint.pformat(cont), expected)
- self.assertEqual(pprint.pformat(cont, width=1, indent=0), expected)
+ self.assertEqual(_pformat(cont), expected)
+ self.assertEqual(_pformat(cont, width=1, indent=0), expected)
self.assertEqual(pprint.saferepr(cont), expected)
def test_basic_line_wrap(self):
'read_io_runtime_us': 0,
'write_io_runtime_us': 43690}"""
for type in [dict, dict2]:
- self.assertEqual(pprint.pformat(type(o)), exp)
+ self.assertEqual(_pformat(type(o)), exp)
exp = """\
frozendict({'RPM_cal': 0,
'main_code_runtime_us': 0,
'read_io_runtime_us': 0,
'write_io_runtime_us': 43690})"""
- self.assertEqual(pprint.pformat(frozendict(o)), exp)
+ self.assertEqual(_pformat(frozendict(o)), exp)
exp = """\
frozendict2({'RPM_cal': 0,
'RPM_cal2': 48059,
'main_code_runtime_us': 0,
'read_io_runtime_us': 0,
'write_io_runtime_us': 43690})"""
- self.assertEqual(pprint.pformat(frozendict2(o)), exp)
+ self.assertEqual(_pformat(frozendict2(o)), exp)
o = range(100)
exp = 'dict_keys([%s])' % ',\n '.join(map(str, o))
keys = dict.fromkeys(o).keys()
- self.assertEqual(pprint.pformat(keys), exp)
+ self.assertEqual(_pformat(keys, width=1), exp)
keys = frozendict.fromkeys(o).keys()
- self.assertEqual(pprint.pformat(keys), exp)
+ self.assertEqual(_pformat(keys, width=1), exp)
o = range(100)
exp = 'dict_values([%s])' % ',\n '.join(map(str, o))
values = {v: v for v in o}.values()
- self.assertEqual(pprint.pformat(values), exp)
+ self.assertEqual(_pformat(values, width=1), exp)
values = frozendict({v: v for v in o}).values()
- self.assertEqual(pprint.pformat(values), exp)
+ self.assertEqual(_pformat(values, width=1), exp)
o = range(100)
exp = 'dict_items([%s])' % ',\n '.join("(%s, %s)" % (i, i) for i in o)
items = {v: v for v in o}.items()
- self.assertEqual(pprint.pformat(items), exp)
+ self.assertEqual(_pformat(items, width=11), exp)
items = frozendict({v: v for v in o}).items()
- self.assertEqual(pprint.pformat(items), exp)
+ self.assertEqual(_pformat(items, width=11), exp)
o = range(100)
exp = 'odict_keys([%s])' % ',\n '.join(map(str, o))
keys = collections.OrderedDict.fromkeys(o).keys()
- self.assertEqual(pprint.pformat(keys), exp)
+ self.assertEqual(_pformat(keys, width=1), exp)
o = range(100)
exp = 'odict_values([%s])' % ',\n '.join(map(str, o))
values = collections.OrderedDict({v: v for v in o}).values()
- self.assertEqual(pprint.pformat(values), exp)
+ self.assertEqual(_pformat(values, width=1), exp)
o = range(100)
exp = 'odict_items([%s])' % ',\n '.join("(%s, %s)" % (i, i) for i in o)
items = collections.OrderedDict({v: v for v in o}).items()
- self.assertEqual(pprint.pformat(items), exp)
+ self.assertEqual(_pformat(items, width=11), exp)
o = range(100)
exp = 'KeysView({%s})' % (': None,\n '.join(map(str, o)) + ': None')
keys_view = KeysView(dict.fromkeys(o))
- self.assertEqual(pprint.pformat(keys_view), exp)
+ self.assertEqual(_pformat(keys_view), exp)
o = range(100)
exp = 'ItemsView({%s})' % (': None,\n '.join(map(str, o)) + ': None')
items_view = ItemsView(dict.fromkeys(o))
- self.assertEqual(pprint.pformat(items_view), exp)
+ self.assertEqual(_pformat(items_view), exp)
o = range(100)
exp = 'MappingView({%s})' % (': None,\n '.join(map(str, o)) + ': None')
mapping_view = MappingView(dict.fromkeys(o))
- self.assertEqual(pprint.pformat(mapping_view), exp)
+ self.assertEqual(_pformat(mapping_view), exp)
o = range(100)
exp = 'ValuesView({%s})' % (': None,\n '.join(map(str, o)) + ': None')
values_view = ValuesView(dict.fromkeys(o))
- self.assertEqual(pprint.pformat(values_view), exp)
+ self.assertEqual(_pformat(values_view), exp)
o = range(100)
exp = '[%s]' % ',\n '.join(map(str, o))
for type in [list, list2]:
- self.assertEqual(pprint.pformat(type(o)), exp)
+ self.assertEqual(_pformat(type(o), width=1), exp)
o = tuple(range(100))
exp = '(%s)' % ',\n '.join(map(str, o))
for type in [tuple, tuple2]:
- self.assertEqual(pprint.pformat(type(o)), exp)
+ self.assertEqual(_pformat(type(o), width=1), exp)
# indent parameter
o = range(100)
exp = '[ %s]' % ',\n '.join(map(str, o))
for type in [list, list2]:
- self.assertEqual(pprint.pformat(type(o), indent=4), exp)
+ self.assertEqual(_pformat(type(o), indent=4, width=1), exp)
def test_nested_indentations(self):
o1 = list(range(10))
expected = """\
[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
{'first': 1, 'second': 2, 'third': 3}]"""
- self.assertEqual(pprint.pformat(o, indent=4, width=42), expected)
+ self.assertEqual(pprint.pformat(o, indent=4, width=42, compact=True), expected)
expected = """\
[ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
{ 'first': 1,
'second': 2,
'third': 3}]"""
- self.assertEqual(pprint.pformat(o, indent=4, width=41), expected)
+ self.assertEqual(pprint.pformat(o, indent=4, width=41, compact=True), expected)
def test_width(self):
expected = """\
[[[[[1, 2, 3],
'1 2']]]]]"""
o = eval(expected)
- self.assertEqual(pprint.pformat(o, width=15), expected)
- self.assertEqual(pprint.pformat(o, width=16), expected)
- self.assertEqual(pprint.pformat(o, width=25), expected)
- self.assertEqual(pprint.pformat(o, width=14), """\
-[[[[[[1,
- 2,
+ self.assertEqual(_pformat(o, width=15), expected)
+ self.assertEqual(_pformat(o, width=16), expected)
+ self.assertEqual(_pformat(o, width=25), expected)
+ self.assertEqual(_pformat(o, width=14), """\
+[[[[[[1, 2,
3],
'1 '
'2']]]],
- {1: [1,
- 2,
+ {1: [1, 2,
3],
2: [12,
34]},
'ef',),
set2({1,
23}),
- [[[[[1,
- 2,
+ [[[[[1, 2,
3],
'1 '
'2']]]]]""")
def test_integer(self):
- self.assertEqual(pprint.pformat(1234567), '1234567')
- self.assertEqual(pprint.pformat(1234567, underscore_numbers=True), '1_234_567')
+ self.assertEqual(_pformat(1234567), '1234567')
+ self.assertEqual(_pformat(1234567, underscore_numbers=True), '1_234_567')
class Temperature(int):
def __new__(cls, celsius_degrees):
def __repr__(self):
kelvin_degrees = self + 273.15
return f"{kelvin_degrees:.2f}°K"
- self.assertEqual(pprint.pformat(Temperature(1000)), '1273.15°K')
+ self.assertEqual(_pformat(Temperature(1000)), '1273.15°K')
def test_sorted_dict(self):
# Starting in Python 2.5, pprint sorts dict displays by key regardless
# Before the change, on 32-bit Windows pformat() gave order
# 'a', 'c', 'b' here, so this test failed.
d = {'a': 1, 'b': 1, 'c': 1}
- self.assertEqual(pprint.pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
- self.assertEqual(pprint.pformat([d, d]),
+ self.assertEqual(_pformat(d), "{'a': 1, 'b': 1, 'c': 1}")
+ self.assertEqual(_pformat([d, d]),
"[{'a': 1, 'b': 1, 'c': 1}, {'a': 1, 'b': 1, 'c': 1}]")
# The next one is kind of goofy. The sorted order depends on the
# Python 2.5, this was in the test_same_as_repr() test. It's worth
# keeping around for now because it's one of few tests of pprint
# against a crazy mix of types.
- self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
+ self.assertEqual(_pformat({"xy\tab\n": (3,), 5: [[]], (): {}}),
r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
def test_sort_dict(self):
d = dict.fromkeys('cba')
- self.assertEqual(pprint.pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}")
- self.assertEqual(pprint.pformat([d, d], sort_dicts=False),
+ self.assertEqual(_pformat(d, sort_dicts=False), "{'c': None, 'b': None, 'a': None}")
+ self.assertEqual(_pformat([d, d], sort_dicts=False),
"[{'c': None, 'b': None, 'a': None}, {'c': None, 'b': None, 'a': None}]")
def test_ordered_dict(self):
d = collections.OrderedDict()
- self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
+ self.assertEqual(_pformat(d, width=1), 'OrderedDict()')
d = collections.OrderedDict([])
- self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
+ self.assertEqual(_pformat(d, width=1), 'OrderedDict()')
words = 'the quick brown fox jumped over a lazy dog'.split()
d = collections.OrderedDict(zip(words, itertools.count()))
- self.assertEqual(pprint.pformat(d),
-"""\
-OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
- ('dog', 8)])""")
- self.assertEqual(pprint.pformat(d.keys(), sort_dicts=False),
+ self.assertEqual(_pformat(d),
"""\
-odict_keys(['the',
- 'quick',
- 'brown',
- 'fox',
- 'jumped',
- 'over',
- 'a',
- 'lazy',
- 'dog'])""")
- self.assertEqual(pprint.pformat(d.items(), sort_dicts=False),
+OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3), ('jumped', 4),
+ ('over', 5), ('a', 6), ('lazy', 7), ('dog', 8)])""")
+ self.assertEqual(
+ _pformat(d.keys(), sort_dicts=False),
+ "odict_keys(['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'a', 'lazy', 'dog'])",
+ )
+ self.assertEqual(_pformat(d.items(), sort_dicts=False),
"""\
-odict_items([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
- ('dog', 8)])""")
- self.assertEqual(pprint.pformat(d.values(), sort_dicts=False),
+odict_items([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3), ('jumped', 4), ('over', 5),
+ ('a', 6), ('lazy', 7), ('dog', 8)])""")
+ self.assertEqual(_pformat(d.values(), sort_dicts=False),
"odict_values([0, 1, 2, 3, 4, 5, 6, 7, 8])")
def test_mapping_proxy(self):
words = 'the quick brown fox jumped over a lazy dog'.split()
d = dict(zip(words, itertools.count()))
m = types.MappingProxyType(d)
- self.assertEqual(pprint.pformat(m), """\
+ self.assertEqual(_pformat(m), """\
mappingproxy({'a': 6,
'brown': 2,
'dog': 8,
'the': 0})""")
d = collections.OrderedDict(zip(words, itertools.count()))
m = types.MappingProxyType(d)
- self.assertEqual(pprint.pformat(m), """\
-mappingproxy(OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
+ self.assertEqual(_pformat(m), """\
+mappingproxy(OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3),
+ ('jumped', 4), ('over', 5), ('a', 6), ('lazy', 7),
('dog', 8)]))""")
def test_dict_views(self):
for dict_class in (dict, collections.OrderedDict, collections.Counter):
empty = dict_class({})
short = dict_class(dict(zip('edcba', 'edcba')))
- long = dict_class(dict((chr(x), chr(x)) for x in range(90, 64, -1)))
- lengths = {"empty": empty, "short": short, "long": long}
+ lengths = {"empty": empty, "short": short}
prefix = "odict" if dict_class is collections.OrderedDict else "dict"
for name, d in lengths.items():
with self.subTest(length=name, prefix=prefix):
- is_short = len(d) < 6
- joiner = ", " if is_short else ",\n "
k = d.keys()
v = d.values()
i = d.items()
- self.assertEqual(pprint.pformat(k, sort_dicts=True),
+ self.assertEqual(_pformat(k, sort_dicts=True),
prefix + "_keys([%s])" %
- joiner.join(repr(key) for key in sorted(k)))
- self.assertEqual(pprint.pformat(v, sort_dicts=True),
+ ", ".join(repr(key) for key in sorted(k)))
+ self.assertEqual(_pformat(v, sort_dicts=True),
prefix + "_values([%s])" %
- joiner.join(repr(val) for val in sorted(v)))
- self.assertEqual(pprint.pformat(i, sort_dicts=True),
+ ", ".join(repr(val) for val in sorted(v)))
+ self.assertEqual(_pformat(i, sort_dicts=True),
prefix + "_items([%s])" %
- joiner.join(repr(item) for item in sorted(i)))
- self.assertEqual(pprint.pformat(k, sort_dicts=False),
+ ", ".join(repr(item) for item in sorted(i)))
+ self.assertEqual(_pformat(k, sort_dicts=False),
prefix + "_keys([%s])" %
- joiner.join(repr(key) for key in k))
- self.assertEqual(pprint.pformat(v, sort_dicts=False),
+ ", ".join(repr(key) for key in k))
+ self.assertEqual(_pformat(v, sort_dicts=False),
prefix + "_values([%s])" %
- joiner.join(repr(val) for val in v))
- self.assertEqual(pprint.pformat(i, sort_dicts=False),
+ ", ".join(repr(val) for val in v))
+ self.assertEqual(_pformat(i, sort_dicts=False),
prefix + "_items([%s])" %
- joiner.join(repr(item) for item in i))
+ ", ".join(repr(item) for item in i))
+
+ # Long case: views wrap with compact-mode packing.
+ long = dict((chr(x), chr(x)) for x in range(90, 64, -1))
+ sorted_keys = (
+ "['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',\n"
+ " 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']"
+ )
+ unsorted_keys = (
+ "['Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K',\n"
+ " 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A']"
+ )
+ sorted_items = (
+ "[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('E', 'E'), ('F', 'F'),\n"
+ " ('G', 'G'), ('H', 'H'), ('I', 'I'), ('J', 'J'), ('K', 'K'), ('L', 'L'),\n"
+ " ('M', 'M'), ('N', 'N'), ('O', 'O'), ('P', 'P'), ('Q', 'Q'), ('R', 'R'),\n"
+ " ('S', 'S'), ('T', 'T'), ('U', 'U'), ('V', 'V'), ('W', 'W'), ('X', 'X'),\n"
+ " ('Y', 'Y'), ('Z', 'Z')]"
+ )
+ unsorted_items = (
+ "[('Z', 'Z'), ('Y', 'Y'), ('X', 'X'), ('W', 'W'), ('V', 'V'), ('U', 'U'),\n"
+ " ('T', 'T'), ('S', 'S'), ('R', 'R'), ('Q', 'Q'), ('P', 'P'), ('O', 'O'),\n"
+ " ('N', 'N'), ('M', 'M'), ('L', 'L'), ('K', 'K'), ('J', 'J'), ('I', 'I'),\n"
+ " ('H', 'H'), ('G', 'G'), ('F', 'F'), ('E', 'E'), ('D', 'D'), ('C', 'C'),\n"
+ " ('B', 'B'), ('A', 'A')]"
+ )
+ for dict_class in (dict, collections.OrderedDict, collections.Counter):
+ d = dict_class(long)
+ prefix = "odict" if dict_class is collections.OrderedDict else "dict"
+ with self.subTest(length="long", prefix=prefix):
+ self.assertEqual(_pformat(d.keys(), sort_dicts=True),
+ f"{prefix}_keys({sorted_keys})")
+ self.assertEqual(_pformat(d.values(), sort_dicts=True),
+ f"{prefix}_values({sorted_keys})")
+ self.assertEqual(_pformat(d.items(), sort_dicts=True),
+ f"{prefix}_items({sorted_items})")
+ self.assertEqual(_pformat(d.keys(), sort_dicts=False),
+ f"{prefix}_keys({unsorted_keys})")
+ self.assertEqual(_pformat(d.values(), sort_dicts=False),
+ f"{prefix}_values({unsorted_keys})")
+ self.assertEqual(_pformat(d.items(), sort_dicts=False),
+ f"{prefix}_items({unsorted_items})")
def test_abc_views(self):
empty = {}
s = sorted(i)
joined_items = "({%s})" % joiner.join(["%r: %r" % (k, v) for (k, v) in i])
sorted_items = "({%s})" % joiner.join(["%r: %r" % (k, v) for (k, v) in s])
- self.assertEqual(pprint.pformat(KeysView(d), sort_dicts=True),
+ self.assertEqual(_pformat(KeysView(d), sort_dicts=True),
KeysView.__name__ + sorted_items)
- self.assertEqual(pprint.pformat(ItemsView(d), sort_dicts=True),
+ self.assertEqual(_pformat(ItemsView(d), sort_dicts=True),
ItemsView.__name__ + sorted_items)
- self.assertEqual(pprint.pformat(MappingView(d), sort_dicts=True),
+ self.assertEqual(_pformat(MappingView(d), sort_dicts=True),
MappingView.__name__ + sorted_items)
- self.assertEqual(pprint.pformat(MV(d), sort_dicts=True),
+ self.assertEqual(_pformat(MV(d), sort_dicts=True),
MV.__name__ + sorted_items)
- self.assertEqual(pprint.pformat(ValuesView(d), sort_dicts=True),
+ self.assertEqual(_pformat(ValuesView(d), sort_dicts=True),
ValuesView.__name__ + sorted_items)
- self.assertEqual(pprint.pformat(KeysView(d), sort_dicts=False),
+ self.assertEqual(_pformat(KeysView(d), sort_dicts=False),
KeysView.__name__ + joined_items)
- self.assertEqual(pprint.pformat(ItemsView(d), sort_dicts=False),
+ self.assertEqual(_pformat(ItemsView(d), sort_dicts=False),
ItemsView.__name__ + joined_items)
- self.assertEqual(pprint.pformat(MappingView(d), sort_dicts=False),
+ self.assertEqual(_pformat(MappingView(d), sort_dicts=False),
MappingView.__name__ + joined_items)
- self.assertEqual(pprint.pformat(MV(d), sort_dicts=False),
+ self.assertEqual(_pformat(MV(d), sort_dicts=False),
MV.__name__ + joined_items)
- self.assertEqual(pprint.pformat(ValuesView(d), sort_dicts=False),
+ self.assertEqual(_pformat(ValuesView(d), sort_dicts=False),
ValuesView.__name__ + joined_items)
def test_nested_views(self):
d = {1: MappingView({1: MappingView({1: MappingView({1: 2})})})}
self.assertEqual(repr(d),
"{1: MappingView({1: MappingView({1: MappingView({1: 2})})})}")
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"{1: MappingView({1: MappingView({1: MappingView({1: 2})})})}")
- self.assertEqual(pprint.pformat(d, depth=2),
+ self.assertEqual(_pformat(d, depth=2),
"{1: MappingView({1: {...}})}")
d = {}
d1 = {1: d.values()}
d2 = {1: d1.values()}
d3 = {1: d2.values()}
- self.assertEqual(pprint.pformat(d3),
+ self.assertEqual(_pformat(d3),
"{1: dict_values([dict_values([dict_values([])])])}")
- self.assertEqual(pprint.pformat(d3, depth=2),
+ self.assertEqual(_pformat(d3, depth=2),
"{1: dict_values([{...}])}")
def test_unorderable_items_views(self):
"""Check that views with unorderable items have stable sorting."""
d = dict((((3+1j), 3), ((1+1j), (1+0j)), (1j, 0j), (500, None), (499, None)))
iv = ItemsView(d)
- self.assertEqual(pprint.pformat(iv),
- pprint.pformat(iv))
- self.assertTrue(pprint.pformat(iv).endswith(", 499: None, 500: None})"),
- pprint.pformat(iv))
- self.assertEqual(pprint.pformat(d.items()), # Won't be equal unless _safe_tuple
- pprint.pformat(d.items())) # is used in _safe_repr
- self.assertTrue(pprint.pformat(d.items()).endswith(", (499, None), (500, None)])"))
+ self.assertEqual(_pformat(iv),
+ _pformat(iv))
+ self.assertTrue(_pformat(iv).endswith(", 499: None, 500: None})"),
+ _pformat(iv))
+ self.assertEqual(_pformat(d.items()), # Won't be equal unless _safe_tuple
+ _pformat(d.items())) # is used in _safe_repr
+ self.assertTrue(_pformat(d.items()).endswith(", (499, None), (500, None)])"))
def test_mapping_view_subclass_no_mapping(self):
class BMV(MappingView):
self.mapping = self._mapping
del self._mapping
- self.assertRaises(AttributeError, pprint.pformat, BMV({}))
+ self.assertRaises(AttributeError, _pformat, BMV({}))
def test_mapping_subclass_repr(self):
"""Test that mapping ABC views use their ._mapping's __repr__."""
self.assertEqual(repr(m), "MyMapping(['test', 1])")
short_view_repr = "%s(MyMapping(['test', 1]))"
self.assertEqual(repr(m.keys()), short_view_repr % "KeysView")
- self.assertEqual(pprint.pformat(m.items()), short_view_repr % "ItemsView")
- self.assertEqual(pprint.pformat(m.keys()), short_view_repr % "KeysView")
- self.assertEqual(pprint.pformat(MappingView(m)), short_view_repr % "MappingView")
- self.assertEqual(pprint.pformat(m.values()), short_view_repr % "ValuesView")
+ self.assertEqual(_pformat(m.items()), short_view_repr % "ItemsView")
+ self.assertEqual(_pformat(m.keys()), short_view_repr % "KeysView")
+ self.assertEqual(_pformat(MappingView(m)), short_view_repr % "MappingView")
+ self.assertEqual(_pformat(m.values()), short_view_repr % "ValuesView")
alpha = "abcdefghijklmnopqrstuvwxyz"
m = MyMapping(alpha)
long_view_repr = "%%s(MyMapping([%s]))" % alpha_repr
self.assertEqual(repr(m), "MyMapping([%s])" % alpha_repr)
self.assertEqual(repr(m.keys()), long_view_repr % "KeysView")
- self.assertEqual(pprint.pformat(m.items()), long_view_repr % "ItemsView")
- self.assertEqual(pprint.pformat(m.keys()), long_view_repr % "KeysView")
- self.assertEqual(pprint.pformat(MappingView(m)), long_view_repr % "MappingView")
- self.assertEqual(pprint.pformat(m.values()), long_view_repr % "ValuesView")
+ self.assertEqual(_pformat(m.items()), long_view_repr % "ItemsView")
+ self.assertEqual(_pformat(m.keys()), long_view_repr % "KeysView")
+ self.assertEqual(_pformat(MappingView(m)), long_view_repr % "MappingView")
+ self.assertEqual(_pformat(m.values()), long_view_repr % "ValuesView")
def test_empty_simple_namespace(self):
ns = types.SimpleNamespace()
- formatted = pprint.pformat(ns)
+ formatted = _pformat(ns)
self.assertEqual(formatted, "namespace()")
def test_small_simple_namespace(self):
ns = types.SimpleNamespace(a=1, b=2)
- formatted = pprint.pformat(ns)
+ formatted = _pformat(ns)
self.assertEqual(formatted, "namespace(a=1, b=2)")
def test_simple_namespace(self):
lazy=7,
dog=8,
)
- formatted = pprint.pformat(ns, width=60, indent=4)
+ formatted = pprint.pformat(ns, width=60, indent=4, compact=True)
self.assertEqual(formatted, """\
namespace(the=0,
quick=1,
lazy=7,
dog=8,
)
- formatted = pprint.pformat(ns, width=60)
+ formatted = _pformat(ns, width=60)
self.assertEqual(formatted, """\
AdvancedNamespace(the=0,
quick=1,
def test_empty_dataclass(self):
dc = dataclasses.make_dataclass("MyDataclass", ())()
- formatted = pprint.pformat(dc)
+ formatted = _pformat(dc)
self.assertEqual(formatted, "MyDataclass()")
def test_small_dataclass(self):
dc = dataclass1("text", 123)
- formatted = pprint.pformat(dc)
+ formatted = _pformat(dc)
self.assertEqual(formatted, "dataclass1(field1='text', field2=123, field3=False)")
def test_larger_dataclass(self):
dc = dataclass1("some fairly long text", int(1e10), True)
- formatted = pprint.pformat([dc, dc], width=60, indent=4)
+ formatted = pprint.pformat([dc, dc], width=60, indent=4, compact=True)
self.assertEqual(formatted, """\
[ dataclass1(field1='some fairly long text',
field2=10000000000,
def test_dataclass_with_repr(self):
dc = dataclass2()
- formatted = pprint.pformat(dc, width=20)
+ formatted = _pformat(dc, width=20)
self.assertEqual(formatted, "custom repr that doesn't fit within pprint width")
def test_dataclass_no_repr(self):
dc = dataclass3()
- formatted = pprint.pformat(dc, width=10)
+ formatted = _pformat(dc, width=10)
self.assertRegex(
formatted,
fr"<{re.escape(__name__)}.dataclass3 object at \w+>",
def test_recursive_dataclass(self):
dc = dataclass4(None)
dc.a = dc
- formatted = pprint.pformat(dc, width=10)
+ formatted = _pformat(dc, width=10)
self.assertEqual(formatted, """\
dataclass4(a=...,
b=1)""")
dc6 = dataclass6(None)
dc5.a = dc6
dc6.c = dc5
- formatted = pprint.pformat(dc5, width=10)
+ formatted = _pformat(dc5, width=10)
self.assertEqual(formatted, """\
dataclass5(a=dataclass6(c=...,
d=1),
{'names with spaces': 'should be presented using repr()',
others.should.not.be: like.this}"""
- dotted_printer = DottedPrettyPrinter()
+ dotted_printer = DottedPrettyPrinter(indent=1, compact=True)
self.assertEqual(dotted_printer.pformat(o), exp)
# length(repr(obj)) < width
self.assertEqual(dotted_printer.pformat(o2), exp2)
def test_set_reprs(self):
- self.assertEqual(pprint.pformat(set()), 'set()')
- self.assertEqual(pprint.pformat(set(range(3))), '{0, 1, 2}')
- self.assertEqual(pprint.pformat(set(range(7)), width=20), '''\
-{0,
- 1,
- 2,
- 3,
- 4,
- 5,
+ self.assertEqual(_pformat(set()), 'set()')
+ self.assertEqual(_pformat(set(range(3))), '{0, 1, 2}')
+ self.assertEqual(_pformat(set(range(7)), width=20), '''\
+{0, 1, 2, 3, 4, 5,
6}''')
- self.assertEqual(pprint.pformat(set2(range(7)), width=20), '''\
-set2({0,
- 1,
- 2,
- 3,
- 4,
- 5,
- 6})''')
- self.assertEqual(pprint.pformat(set3(range(7)), width=20),
+ self.assertEqual(_pformat(set2(range(7)), width=20), '''\
+set2({0, 1, 2, 3, 4,
+ 5, 6})''')
+ self.assertEqual(_pformat(set3(range(7)), width=20),
'set3({0, 1, 2, 3, 4, 5, 6})')
- self.assertEqual(pprint.pformat(frozenset()), 'frozenset()')
- self.assertEqual(pprint.pformat(frozenset(range(3))),
+ self.assertEqual(_pformat(frozenset()), 'frozenset()')
+ self.assertEqual(_pformat(frozenset(range(3))),
'frozenset({0, 1, 2})')
- self.assertEqual(pprint.pformat(frozenset(range(7)), width=20), '''\
-frozenset({0,
- 1,
- 2,
- 3,
- 4,
- 5,
+ self.assertEqual(_pformat(frozenset(range(7)), width=20), '''\
+frozenset({0, 1, 2,
+ 3, 4, 5,
6})''')
- self.assertEqual(pprint.pformat(frozenset2(range(7)), width=20), '''\
-frozenset2({0,
- 1,
- 2,
- 3,
- 4,
- 5,
+ self.assertEqual(_pformat(frozenset2(range(7)), width=20), '''\
+frozenset2({0, 1, 2,
+ 3, 4, 5,
6})''')
- self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
+ self.assertEqual(_pformat(frozenset3(range(7)), width=20),
'frozenset3({0, 1, 2, 3, 4, 5, 6})')
def test_set_of_sets_reprs(self):
fs0 = frozenset()
fs1 = frozenset(('abc', 'xyz'))
data = frozenset((fs0, fs1))
- self.assertEqual(pprint.pformat(data),
+ self.assertEqual(_pformat(data),
'frozenset({%r, %r})' % (fs0, fs1))
- self.assertEqual(pprint.pformat(data), repr(data))
+ self.assertEqual(_pformat(data), repr(data))
fs2 = frozenset(('one', 'two'))
data = {fs2: frozenset((fs0, fs1))}
- self.assertEqual(pprint.pformat(data),
+ self.assertEqual(_pformat(data),
"{%r: frozenset({%r, %r})}" % (fs2, fs0, fs1))
- self.assertEqual(pprint.pformat(data), repr(data))
+ self.assertEqual(_pformat(data), repr(data))
# Single-line, unordered:
fs1 = frozenset(("xyz", "qwerty"))
fs2 = frozenset(("abcd", "spam"))
fs = frozenset((fs1, fs2))
- self.assertEqual(pprint.pformat(fs), repr(fs))
+ self.assertEqual(_pformat(fs), repr(fs))
# Multiline, unordered:
def check(res, invariants):
fs1 = frozenset(('regular string', 'other string'))
fs2 = frozenset(('third string', 'one more string'))
check(
- pprint.pformat(frozenset((fs1, fs2))),
+ _pformat(frozenset((fs1, fs2))),
[
"""
frozenset({%r,
# Everything is multiline, unordered:
check(
- pprint.pformat(
+ _pformat(
frozenset((
frozenset((
"xyz very-very long string",
nested_tuple = (1, (2, (3, (4, (5, 6)))))
nested_dict = {1: {2: {3: {4: {5: {6: 6}}}}}}
nested_list = [1, [2, [3, [4, [5, [6, []]]]]]]
- self.assertEqual(pprint.pformat(nested_tuple), repr(nested_tuple))
- self.assertEqual(pprint.pformat(nested_dict), repr(nested_dict))
- self.assertEqual(pprint.pformat(nested_list), repr(nested_list))
+ self.assertEqual(_pformat(nested_tuple), repr(nested_tuple))
+ self.assertEqual(_pformat(nested_dict), repr(nested_dict))
+ self.assertEqual(_pformat(nested_list), repr(nested_list))
lv1_tuple = '(1, (...))'
lv1_dict = '{1: {...}}'
lv1_list = '[1, [...]]'
- self.assertEqual(pprint.pformat(nested_tuple, depth=1), lv1_tuple)
- self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict)
- self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list)
+ self.assertEqual(_pformat(nested_tuple, depth=1), lv1_tuple)
+ self.assertEqual(_pformat(nested_dict, depth=1), lv1_dict)
+ self.assertEqual(_pformat(nested_list, depth=1), lv1_list)
def test_sort_unorderable_values(self):
# Issue 3976: sorted pprints fail for unorderable values.
skeys = sorted(keys, key=id)
clean = lambda s: s.replace(' ', '').replace('\n','')
- self.assertEqual(clean(pprint.pformat(set(keys))),
+ self.assertEqual(clean(_pformat(set(keys))),
'{' + ','.join(map(repr, skeys)) + '}')
- self.assertEqual(clean(pprint.pformat(frozenset(keys))),
+ self.assertEqual(clean(_pformat(frozenset(keys))),
'frozenset({' + ','.join(map(repr, skeys)) + '})')
- self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
+ self.assertEqual(clean(_pformat(dict.fromkeys(keys))),
'{' + ','.join('%r:None' % k for k in skeys) + '}')
- self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys).keys())),
+ self.assertEqual(clean(_pformat(dict.fromkeys(keys).keys())),
'dict_keys([' + ','.join('%r' % k for k in skeys) + '])')
- self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys).items())),
+ self.assertEqual(clean(_pformat(dict.fromkeys(keys).items())),
'dict_items([' + ','.join('(%r,None)' % k for k in skeys) + '])')
# Issue 10017: TypeError on user-defined types as dict keys.
- self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
+ self.assertEqual(_pformat({Unorderable: 0, 1: 0}),
'{1: 0, ' + repr(Unorderable) +': 0}')
# Issue 14998: TypeError on tuples with NoneTypes as dict keys.
keys = [(1,), (None,)]
- self.assertEqual(pprint.pformat(dict.fromkeys(keys, 0)),
+ self.assertEqual(_pformat(dict.fromkeys(keys, 0)),
'{%r: 0, %r: 0}' % tuple(sorted(keys, key=id)))
def test_sort_orderable_and_unorderable_values(self):
self.assertEqual(sorted([b, a]), [a, b])
self.assertEqual(sorted([a, b]), [a, b])
# set
- self.assertEqual(pprint.pformat(set([b, a]), width=1),
+ self.assertEqual(_pformat(set([b, a]), width=1),
'{%r,\n %r}' % (a, b))
- self.assertEqual(pprint.pformat(set([a, b]), width=1),
+ self.assertEqual(_pformat(set([a, b]), width=1),
'{%r,\n %r}' % (a, b))
# dict
- self.assertEqual(pprint.pformat(dict.fromkeys([b, a]), width=1),
+ self.assertEqual(_pformat(dict.fromkeys([b, a]), width=1),
'{%r: None,\n %r: None}' % (a, b))
- self.assertEqual(pprint.pformat(dict.fromkeys([a, b]), width=1),
+ self.assertEqual(_pformat(dict.fromkeys([a, b]), width=1),
'{%r: None,\n %r: None}' % (a, b))
def test_str_wrap(self):
# pprint tries to wrap strings intelligently
fox = 'the quick brown fox jumped over a lazy dog'
- self.assertEqual(pprint.pformat(fox, width=19), """\
+ self.assertEqual(_pformat(fox, width=19), """\
('the quick brown '
'fox jumped over '
'a lazy dog')""")
- self.assertEqual(pprint.pformat({'a': 1, 'b': fox, 'c': 2},
+ self.assertEqual(_pformat({'a': 1, 'b': fox, 'c': 2},
width=25), """\
{'a': 1,
'b': 'the quick brown '
# - non-ASCII is allowed
# - an apostrophe doesn't disrupt the pprint
special = "Portons dix bons \"whiskys\"\nà l'avocat goujat\t qui fumait au zoo"
- self.assertEqual(pprint.pformat(special, width=68), repr(special))
- self.assertEqual(pprint.pformat(special, width=31), """\
+ self.assertEqual(_pformat(special, width=68), repr(special))
+ self.assertEqual(_pformat(special, width=31), """\
('Portons dix bons "whiskys"\\n'
"Ã l'avocat goujat\\t qui "
'fumait au zoo')""")
- self.assertEqual(pprint.pformat(special, width=20), """\
+ self.assertEqual(_pformat(special, width=20), """\
('Portons dix bons '
'"whiskys"\\n'
"Ã l'avocat "
'goujat\\t qui '
'fumait au zoo')""")
- self.assertEqual(pprint.pformat([[[[[special]]]]], width=35), """\
+ self.assertEqual(_pformat([[[[[special]]]]], width=35), """\
[[[[['Portons dix bons "whiskys"\\n'
"Ã l'avocat goujat\\t qui "
'fumait au zoo']]]]]""")
- self.assertEqual(pprint.pformat([[[[[special]]]]], width=25), """\
+ self.assertEqual(_pformat([[[[[special]]]]], width=25), """\
[[[[['Portons dix bons '
'"whiskys"\\n'
"Ã l'avocat "
'goujat\\t qui '
'fumait au zoo']]]]]""")
- self.assertEqual(pprint.pformat([[[[[special]]]]], width=23), """\
+ self.assertEqual(_pformat([[[[[special]]]]], width=23), """\
[[[[['Portons dix '
'bons "whiskys"\\n'
"Ã l'avocat "
'zoo']]]]]""")
# An unwrappable string is formatted as its repr
unwrappable = "x" * 100
- self.assertEqual(pprint.pformat(unwrappable, width=80), repr(unwrappable))
- self.assertEqual(pprint.pformat(''), "''")
+ self.assertEqual(_pformat(unwrappable, width=80), repr(unwrappable))
+ self.assertEqual(_pformat(''), "''")
# Check that the pprint is a usable repr
special *= 10
for width in range(3, 40):
- formatted = pprint.pformat(special, width=width)
+ formatted = _pformat(special, width=width)
self.assertEqual(eval(formatted), special)
- formatted = pprint.pformat([special] * 2, width=width)
+ formatted = _pformat([special] * 2, width=width)
self.assertEqual(eval(formatted), [special] * 2)
def test_compact(self):
14, 15],
[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
[0, 1, 2, 3, 4]]"""
- self.assertEqual(pprint.pformat(o, width=47, compact=True), expected)
+ self.assertEqual(_pformat(o, width=47, compact=True), expected)
def test_compact_width(self):
levels = 20
for i in range(levels - 1):
o = [o]
for w in range(levels * 2 + 1, levels + 3 * number - 1):
- lines = pprint.pformat(o, width=w, compact=True).splitlines()
+ lines = _pformat(o, width=w, compact=True).splitlines()
maxwidth = max(map(len, lines))
self.assertLessEqual(maxwidth, w)
self.assertGreater(maxwidth, w - 3)
def test_bytes_wrap(self):
- self.assertEqual(pprint.pformat(b'', width=1), "b''")
- self.assertEqual(pprint.pformat(b'abcd', width=1), "b'abcd'")
+ self.assertEqual(_pformat(b'', width=1), "b''")
+ self.assertEqual(_pformat(b'abcd', width=1), "b'abcd'")
letters = b'abcdefghijklmnopqrstuvwxyz'
- self.assertEqual(pprint.pformat(letters, width=29), repr(letters))
- self.assertEqual(pprint.pformat(letters, width=19), """\
+ self.assertEqual(_pformat(letters, width=29), repr(letters))
+ self.assertEqual(_pformat(letters, width=19), """\
(b'abcdefghijkl'
b'mnopqrstuvwxyz')""")
- self.assertEqual(pprint.pformat(letters, width=18), """\
+ self.assertEqual(_pformat(letters, width=18), """\
(b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz')""")
- self.assertEqual(pprint.pformat(letters, width=16), """\
+ self.assertEqual(_pformat(letters, width=16), """\
(b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz')""")
special = bytes(range(16))
- self.assertEqual(pprint.pformat(special, width=61), repr(special))
- self.assertEqual(pprint.pformat(special, width=48), """\
+ self.assertEqual(_pformat(special, width=61), repr(special))
+ self.assertEqual(_pformat(special, width=48), """\
(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat(special, width=32), """\
+ self.assertEqual(_pformat(special, width=32), """\
(b'\\x00\\x01\\x02\\x03'
b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat(special, width=1), """\
+ self.assertEqual(_pformat(special, width=1), """\
(b'\\x00\\x01\\x02\\x03'
b'\\x04\\x05\\x06\\x07'
b'\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
+ self.assertEqual(_pformat({'a': 1, 'b': letters, 'c': 2},
width=21), """\
{'a': 1,
'b': b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz',
'c': 2}""")
- self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
+ self.assertEqual(_pformat({'a': 1, 'b': letters, 'c': 2},
width=20), """\
{'a': 1,
'b': b'abcdefgh'
b'ijklmnop'
b'qrstuvwxyz',
'c': 2}""")
- self.assertEqual(pprint.pformat([[[[[[letters]]]]]], width=25), """\
+ self.assertEqual(_pformat([[[[[[letters]]]]]], width=25), """\
[[[[[[b'abcdefghijklmnop'
b'qrstuvwxyz']]]]]]""")
- self.assertEqual(pprint.pformat([[[[[[special]]]]]], width=41), """\
+ self.assertEqual(_pformat([[[[[[special]]]]]], width=41), """\
[[[[[[b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f']]]]]]""")
# Check that the pprint is a usable repr
for width in range(1, 64):
- formatted = pprint.pformat(special, width=width)
+ formatted = _pformat(special, width=width)
self.assertEqual(eval(formatted), special)
- formatted = pprint.pformat([special] * 2, width=width)
+ formatted = _pformat([special] * 2, width=width)
self.assertEqual(eval(formatted), [special] * 2)
def test_bytearray_wrap(self):
- self.assertEqual(pprint.pformat(bytearray(), width=1), "bytearray(b'')")
+ self.assertEqual(_pformat(bytearray(), width=1), "bytearray(b'')")
letters = bytearray(b'abcdefghijklmnopqrstuvwxyz')
- self.assertEqual(pprint.pformat(letters, width=40), repr(letters))
- self.assertEqual(pprint.pformat(letters, width=28), """\
+ self.assertEqual(_pformat(letters, width=40), repr(letters))
+ self.assertEqual(_pformat(letters, width=28), """\
bytearray(b'abcdefghijkl'
b'mnopqrstuvwxyz')""")
- self.assertEqual(pprint.pformat(letters, width=27), """\
+ self.assertEqual(_pformat(letters, width=27), """\
bytearray(b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz')""")
- self.assertEqual(pprint.pformat(letters, width=25), """\
+ self.assertEqual(_pformat(letters, width=25), """\
bytearray(b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz')""")
special = bytearray(range(16))
- self.assertEqual(pprint.pformat(special, width=72), repr(special))
- self.assertEqual(pprint.pformat(special, width=57), """\
+ self.assertEqual(_pformat(special, width=72), repr(special))
+ self.assertEqual(_pformat(special, width=57), """\
bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat(special, width=41), """\
+ self.assertEqual(_pformat(special, width=41), """\
bytearray(b'\\x00\\x01\\x02\\x03'
b'\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat(special, width=1), """\
+ self.assertEqual(_pformat(special, width=1), """\
bytearray(b'\\x00\\x01\\x02\\x03'
b'\\x04\\x05\\x06\\x07'
b'\\x08\\t\\n\\x0b'
b'\\x0c\\r\\x0e\\x0f')""")
- self.assertEqual(pprint.pformat({'a': 1, 'b': letters, 'c': 2},
+ self.assertEqual(_pformat({'a': 1, 'b': letters, 'c': 2},
width=31), """\
{'a': 1,
'b': bytearray(b'abcdefghijkl'
b'mnopqrstuvwx'
b'yz'),
'c': 2}""")
- self.assertEqual(pprint.pformat([[[[[letters]]]]], width=37), """\
+ self.assertEqual(_pformat([[[[[letters]]]]], width=37), """\
[[[[[bytearray(b'abcdefghijklmnop'
b'qrstuvwxyz')]]]]]""")
- self.assertEqual(pprint.pformat([[[[[special]]]]], width=50), """\
+ self.assertEqual(_pformat([[[[[special]]]]], width=50), """\
[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
def test_default_dict(self):
d = collections.defaultdict(int)
- self.assertEqual(pprint.pformat(d, width=1), "defaultdict(<class 'int'>, {})")
+ self.assertEqual(_pformat(d, width=1), "defaultdict(<class 'int'>, {})")
words = 'the quick brown fox jumped over a lazy dog'.split()
d = collections.defaultdict(int, zip(words, itertools.count()))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
defaultdict(<class 'int'>,
{'a': 6,
def test_counter(self):
d = collections.Counter()
- self.assertEqual(pprint.pformat(d, width=1), "Counter()")
+ self.assertEqual(_pformat(d, width=1), "Counter()")
d = collections.Counter('senselessness')
- self.assertEqual(pprint.pformat(d, width=40),
+ self.assertEqual(_pformat(d, width=40),
"""\
Counter({'s': 6,
'e': 4,
'n': 2,
'l': 1})""")
- self.assertEqual(pprint.pformat(d, indent=2, width=1),
+ self.assertEqual(_pformat(d, indent=2, width=1),
"""\
Counter({ 's': 6,
'e': 4,
def test_chainmap(self):
d = collections.ChainMap()
- self.assertEqual(pprint.pformat(d, width=1), "ChainMap({})")
+ self.assertEqual(_pformat(d, width=1), "ChainMap({})")
words = 'the quick brown fox jumped over a lazy dog'.split()
items = list(zip(words, itertools.count()))
d = collections.ChainMap(dict(items))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
ChainMap({'a': 6,
'brown': 2,
'quick': 1,
'the': 0})""")
d = collections.ChainMap(dict(items), collections.OrderedDict(items))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
ChainMap({'a': 6,
'brown': 2,
'over': 5,
'quick': 1,
'the': 0},
- OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
+ OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3),
+ ('jumped', 4), ('over', 5), ('a', 6), ('lazy', 7),
('dog', 8)]))""")
- self.assertEqual(pprint.pformat(d.keys()),
+ self.assertEqual(_pformat(d.keys()),
"""\
KeysView(ChainMap({'a': 6,
'brown': 2,
'over': 5,
'quick': 1,
'the': 0},
- OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
+ OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3),
+ ('jumped', 4), ('over', 5), ('a', 6), ('lazy', 7),
('dog', 8)])))""")
- self.assertEqual(pprint.pformat(d.items()),
+ self.assertEqual(_pformat(d.items()),
"""\
ItemsView(ChainMap({'a': 6,
'brown': 2,
'over': 5,
'quick': 1,
'the': 0},
- OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
+ OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3),
+ ('jumped', 4), ('over', 5), ('a', 6), ('lazy', 7),
('dog', 8)])))""")
- self.assertEqual(pprint.pformat(d.values()),
+ self.assertEqual(_pformat(d.values()),
"""\
ValuesView(ChainMap({'a': 6,
'brown': 2,
'over': 5,
'quick': 1,
'the': 0},
- OrderedDict([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
+ OrderedDict([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3),
+ ('jumped', 4), ('over', 5), ('a', 6), ('lazy', 7),
('dog', 8)])))""")
def test_deque(self):
d = collections.deque()
- self.assertEqual(pprint.pformat(d, width=1), "deque([])")
+ self.assertEqual(_pformat(d, width=1), "deque([])")
d = collections.deque(maxlen=7)
- self.assertEqual(pprint.pformat(d, width=1), "deque([], maxlen=7)")
+ self.assertEqual(_pformat(d, width=1), "deque([], maxlen=7)")
words = 'the quick brown fox jumped over a lazy dog'.split()
d = collections.deque(zip(words, itertools.count()))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
-deque([('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
- ('dog', 8)])""")
+deque([('the', 0), ('quick', 1), ('brown', 2), ('fox', 3), ('jumped', 4),
+ ('over', 5), ('a', 6), ('lazy', 7), ('dog', 8)])""")
d = collections.deque(zip(words, itertools.count()), maxlen=7)
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
-deque([('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
- ('dog', 8)],
+deque([('brown', 2), ('fox', 3), ('jumped', 4), ('over', 5), ('a', 6),
+ ('lazy', 7), ('dog', 8)],
maxlen=7)""")
def test_user_dict(self):
d = collections.UserDict()
- self.assertEqual(pprint.pformat(d, width=1), "{}")
+ self.assertEqual(_pformat(d, width=1), "{}")
words = 'the quick brown fox jumped over a lazy dog'.split()
d = collections.UserDict(zip(words, itertools.count()))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
{'a': 6,
'brown': 2,
'over': 5,
'quick': 1,
'the': 0}""")
- self.assertEqual(pprint.pformat(d.keys()), """\
+ self.assertEqual(_pformat(d.keys()), """\
KeysView({'a': 6,
'brown': 2,
'dog': 8,
'over': 5,
'quick': 1,
'the': 0})""")
- self.assertEqual(pprint.pformat(d.items()), """\
+ self.assertEqual(_pformat(d.items()), """\
ItemsView({'a': 6,
'brown': 2,
'dog': 8,
'over': 5,
'quick': 1,
'the': 0})""")
- self.assertEqual(pprint.pformat(d.values()), """\
+ self.assertEqual(_pformat(d.values()), """\
ValuesView({'a': 6,
'brown': 2,
'dog': 8,
def test_user_list(self):
d = collections.UserList()
- self.assertEqual(pprint.pformat(d, width=1), "[]")
+ self.assertEqual(_pformat(d, width=1), "[]")
words = 'the quick brown fox jumped over a lazy dog'.split()
d = collections.UserList(zip(words, itertools.count()))
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
-[('the', 0),
- ('quick', 1),
- ('brown', 2),
- ('fox', 3),
- ('jumped', 4),
- ('over', 5),
- ('a', 6),
- ('lazy', 7),
- ('dog', 8)]""")
+[('the', 0), ('quick', 1), ('brown', 2), ('fox', 3), ('jumped', 4), ('over', 5),
+ ('a', 6), ('lazy', 7), ('dog', 8)]""")
def test_user_string(self):
d = collections.UserString('')
- self.assertEqual(pprint.pformat(d, width=1), "''")
+ self.assertEqual(_pformat(d, width=1), "''")
d = collections.UserString('the quick brown fox jumped over a lazy dog')
- self.assertEqual(pprint.pformat(d, width=20),
+ self.assertEqual(_pformat(d, width=20),
"""\
('the quick brown '
'fox jumped over '
'a lazy dog')""")
- self.assertEqual(pprint.pformat({1: d}, width=20),
+ self.assertEqual(_pformat({1: d}, width=20),
"""\
{1: 'the quick '
'brown fox '
def test_template(self):
d = t""
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"Template(strings=('',), interpolations=())")
- self.assertEqual(pprint.pformat(d), repr(d))
- self.assertEqual(pprint.pformat(d, width=1),
+ self.assertEqual(_pformat(d), repr(d))
+ self.assertEqual(_pformat(d, width=1),
"""\
Template(strings=('',),
interpolations=())""")
name = "World"
d = t"Hello {name}"
- self.assertEqual(pprint.pformat(d),
+ self.assertEqual(_pformat(d),
"""\
Template(strings=('Hello ', ''),
interpolations=(Interpolation('World', 'name', None, ''),))""")
ver = {3.13: False, 3.14: True}
d = t"Hello { {"name": "Python", "version": ver}!s:z}!"
- self.assertEqual(pprint.pformat(d, width=1),
+ self.assertEqual(_pformat(d, width=1),
"""\
Template(strings=('Hello ',
'!'),
def test_expand_template(self):
d = t""
self.assertEqual(
- pprint.pformat(d, expand=True),
+ pprint.pformat(d),
"Template(strings=('',), interpolations=())",
)
name = "World"
d = t"Hello {name}"
self.assertEqual(
- pprint.pformat(d, width=40, indent=4, expand=True),
+ pprint.pformat(d, width=40, indent=4),
"""\
Template(
strings=('Hello ', ''),
ver = {3.13: False, 3.14: True}
d = t"Hello { {"name": "Python", "version": ver}!s:z}!"
self.assertEqual(
- pprint.pformat(d, width=40, indent=4, expand=True),
+ pprint.pformat(d, width=40, indent=4),
"""\
Template(
strings=('Hello ', '!'),
corge=7,
garply=(1, 2, 3, 4),
)
- self.assertEqual(pprint.pformat(dummy_dataclass, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_dataclass, width=40, indent=4),
"""\
DummyDataclass(
foo='foo',
"quux": ["foo", "bar", "baz"],
"corge": 7,
}
- self.assertEqual(pprint.pformat(dummy_dict, width=40, indent=4,
- expand=True, sort_dicts=False),
+ self.assertEqual(pprint.pformat(dummy_dict, width=40, indent=4, sort_dicts=False),
"""\
{
'foo': 'bar',
("baz", 123),
]
)
- self.assertEqual(pprint.pformat(dummy_ordered_dict, width=20, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_ordered_dict, width=20, indent=4),
"""\
OrderedDict([
('foo', 1),
"baz",
"qux",
]
- self.assertEqual(pprint.pformat(dummy_list, width=20, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_list, width=20, indent=4),
"""\
[
'foo',
5,
6,
)
- self.assertEqual(pprint.pformat(dummy_tuple, width=20, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_tuple, width=20, indent=4),
"""\
(
'foo',
def test_expand_single_element_tuple(self):
self.assertEqual(
- pprint.pformat((1,), width=1, indent=4, expand=True),
+ pprint.pformat((1,), width=1, indent=4),
"""\
(
1,
"qux",
(1, 2, 3),
}
- self.assertEqual(pprint.pformat(dummy_set, width=20, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_set, width=20, indent=4),
"""\
{
'bar',
frozenset(dummy_set),
}
)
- self.assertEqual(pprint.pformat(dummy_frozenset, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_frozenset, width=40, indent=4),
"""\
frozenset({
frozenset({(1, 2, 3)}),
{"foo": "bar", "baz": 123, "qux": [1, 2]}
)
self.assertEqual(
- pprint.pformat(dummy_frozendict, width=20, indent=4, expand=True),
+ pprint.pformat(dummy_frozendict, width=20, indent=4),
"""\
frozendict({
'baz': 123,
def test_expand_bytes(self):
dummy_bytes = b"Hello world! foo bar baz 123 456 789"
- self.assertEqual(pprint.pformat(dummy_bytes, width=20, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_bytes, width=20, indent=4),
"""\
(
b'Hello world!'
def test_expand_bytearray(self):
dummy_bytes = b"Hello world! foo bar baz 123 456 789"
dummy_byte_array = bytearray(dummy_bytes)
- self.assertEqual(pprint.pformat(dummy_byte_array, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_byte_array, width=40, indent=4),
"""\
bytearray(
b'Hello world! foo bar baz 123 456'
"corge": 7,
}
dummy_mappingproxy = types.MappingProxyType(dummy_dict)
- self.assertEqual(pprint.pformat(dummy_mappingproxy, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_mappingproxy, width=40, indent=4),
"""\
mappingproxy({
'baz': 123,
),
)
- self.assertEqual(pprint.pformat(dummy_namespace, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_namespace, width=40, indent=4),
"""\
namespace(
foo='bar',
dummy_defaultdict["foo"].append("baz")
dummy_defaultdict["foo"].append("qux")
dummy_defaultdict["bar"] = {"foo": "bar", "baz": None}
- self.assertEqual(pprint.pformat(dummy_defaultdict, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_defaultdict, width=40, indent=4),
"""\
defaultdict(<class 'list'>, {
'bar': {'baz': None, 'foo': 'bar'},
'd': 2,
'e': 1,
})"""
- self.assertEqual(pprint.pformat(dummy_counter, width=40, indent=4,
- expand=True), expected)
+ self.assertEqual(pprint.pformat(dummy_counter, width=40, indent=4), expected)
expected2 = """\
Counter({
'd': 2,
'e': 1,
})"""
- self.assertEqual(pprint.pformat(dummy_counter, width=20, indent=2,
- expand=True), expected2)
+ self.assertEqual(pprint.pformat(dummy_counter, width=20, indent=2), expected2)
def test_expand_chainmap(self):
dummy_dict = {
{"corge": dummy_dict},
)
dummy_chainmap.maps.append({"garply": "waldo"})
- self.assertEqual(pprint.pformat(dummy_chainmap, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_chainmap, width=40, indent=4),
"""\
ChainMap(
{'foo': 'bar'},
dummy_deque.append(dummy_dict)
dummy_deque.extend(dummy_list)
dummy_deque.appendleft(dummy_set)
- self.assertEqual(pprint.pformat(dummy_deque, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_deque, width=40, indent=4),
"""\
deque([
{(1, 2, 3)},
"corge": 7 })
dummy_userdict.access_count = 5
- self.assertEqual(pprint.pformat(dummy_userdict, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_userdict, width=40, indent=4),
"""\
{
'baz': 123,
dummy_userlist = DummyUserList(["first", 2, {"key": "value"},
[4, 5, 6]])
- self.assertEqual(pprint.pformat(dummy_userlist, width=40, indent=4,
- expand=True),
+ self.assertEqual(pprint.pformat(dummy_userlist, width=40, indent=4),
"""\
[
'first',
def test_expand_dict_keys(self):
d = {"foo": 1, "bar": 2, "baz": 3, "qux": 4, "quux": 5}
self.assertEqual(
- pprint.pformat(d.keys(), width=20, indent=4, expand=True),
+ pprint.pformat(d.keys(), width=20, indent=4),
"""\
dict_keys([
'bar',
def test_expand_dict_values(self):
d = {"foo": 1, "bar": 2, "baz": 3, "qux": 4, "quux": 5}
self.assertEqual(
- pprint.pformat(d.values(), width=20, indent=4, expand=True),
+ pprint.pformat(d.values(), width=20, indent=4),
"""\
dict_values([
1,
def test_expand_dict_items(self):
d = {"foo": 1, "bar": 2, "baz": 3, "qux": 4, "quux": 5}
self.assertEqual(
- pprint.pformat(d.items(), width=20, indent=4, expand=True),
+ pprint.pformat(d.items(), width=20, indent=4),
"""\
dict_items([
('bar', 2),
def test_expand_str(self):
s = "The quick brown fox jumped over the lazy dog " * 3
self.assertEqual(
- pprint.pformat(s, width=40, indent=4, expand=True),
+ pprint.pformat(s, width=40, indent=4),
"""\
(
'The quick brown fox jumped over '