Skip to content

Commit 994b8e1

Browse files
committed
Address review
1 parent ee4fd2e commit 994b8e1

File tree

1 file changed

+116
-65
lines changed

1 file changed

+116
-65
lines changed

Lib/test/test_pprint.py

Lines changed: 116 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -622,89 +622,140 @@ def test_set_reprs(self):
622622
self.assertEqual(pprint.pformat(frozenset3(range(7)), width=20),
623623
'frozenset3({0, 1, 2, 3, 4, 5, 6})')
624624

625-
@test.support.cpython_only
626625
def test_set_of_sets_reprs(self):
626+
# This test creates a complex arrangement of frozensets and
627+
# compares the pretty-printed repr against a string hard-coded in
628+
# the test. The hard-coded repr depends on the sort order of
629+
# frozensets.
630+
#
631+
# However, as the docs point out: "Since sets only define
632+
# partial ordering (subset relationships), the output of the
633+
# list.sort() method is undefined for lists of sets."
634+
#
635+
# >>> frozenset({0}) < frozenset({1})
636+
# False
637+
# >>> frozenset({1}) < frozenset({0})
638+
# False
639+
#
640+
# In this test we list all possible invariants of the result
641+
# for unordered frozensets.
642+
#
627643
# This test has a long history, see:
628644
# - https://github.com/python/cpython/commit/969fe57baa0eb80332990f9cda936a33e13fabef
629-
# - https://bugs.python.org/issue13907
645+
# - https://github.com/python/cpython/issues/58115
630646
# - https://github.com/python/cpython/issues/111147
631647

632648
import textwrap
633649

634-
# Single-line:
635-
self.assertEqual(
636-
pprint.pformat(frozenset((frozenset(), frozenset((1,))))),
637-
'frozenset({frozenset(), frozenset({1})})',
638-
)
639-
self.assertEqual(
640-
pprint.pformat(frozenset((frozenset(), frozenset((1, 2))))),
641-
'frozenset({frozenset(), frozenset({1, 2})})',
642-
)
650+
# Single-line, always ordered:
643651
self.assertEqual(
644652
pprint.pformat(frozenset((frozenset(), frozenset((1, 2, 3))))),
645653
'frozenset({frozenset(), frozenset({1, 2, 3})})',
646654
)
647-
648-
# Multiline:
649-
expected_format = textwrap.dedent("""
650-
frozenset({frozenset(),
651-
frozenset({0,
652-
1,
653-
2,
654-
3,
655-
4,
656-
5,
657-
6,
658-
7,
659-
8,
660-
9,
661-
10,
662-
11,
663-
12,
664-
13,
665-
14,
666-
15,
667-
16,
668-
17,
669-
18,
670-
19,
671-
20,
672-
21,
673-
22,
674-
23,
675-
24})})""")
676-
self.assertEqual(
677-
pprint.pformat(
678-
frozenset((
679-
frozenset(),
680-
frozenset(range(25)),
681-
)),
682-
# len(str(frozenset(range(25)))) is longer than 80
683-
width=80,
684-
),
685-
expected_format[1:], # strip first newline
686-
)
687-
688-
# And also `frozenset`s with `dict`:
689655
self.assertEqual(
690656
pprint.pformat({
691657
frozenset((1, 2)): frozenset((frozenset(), frozenset((1, 2, 3)))),
692658
}),
693659
'{frozenset({1, 2}): frozenset({frozenset(), frozenset({1, 2, 3})})}',
694660
)
695661

696-
# and multiline:
697-
expected_format = textwrap.dedent("""
698-
{frozenset({frozenset(), frozenset({1, 2, 3})}): frozenset({frozenset(),
699-
frozenset({1,
700-
2,
701-
3})})}""")
702-
self.assertEqual(
703-
pprint.pformat({
704-
frozenset((frozenset(), frozenset((1, 2, 3)))):
705-
frozenset((frozenset(), frozenset((1, 2, 3)))),
706-
}),
707-
expected_format[1:], # strip first newline
662+
# Single-line, unordered:
663+
self.assertIn(
664+
pprint.pformat(
665+
frozenset((
666+
frozenset(("xyz", "qwerty")),
667+
frozenset(("abcd", "spam"))),
668+
),
669+
),
670+
[
671+
"frozenset({frozenset({'qwerty', 'xyz'}), frozenset({'spam', 'abcd'})})",
672+
"frozenset({frozenset({'xyz', 'qwerty'}), frozenset({'spam', 'abcd'})})",
673+
"frozenset({frozenset({'qwerty', 'xyz'}), frozenset({'abcd', 'spam'})})",
674+
"frozenset({frozenset({'xyz', 'qwerty'}), frozenset({'abcd', 'spam'})})",
675+
676+
"frozenset({frozenset({'spam', 'abcd'}), frozenset({'qwerty', 'xyz'})})",
677+
"frozenset({frozenset({'spam', 'abcd'}), frozenset({'xyz', 'qwerty'})})",
678+
"frozenset({frozenset({'abcd', 'spam'}), frozenset({'qwerty', 'xyz'})})",
679+
"frozenset({frozenset({'abcd', 'spam'}), frozenset({'xyz', 'qwerty'})})",
680+
],
681+
)
682+
683+
# Multiline, unordered:
684+
def check(res, invariants):
685+
self.assertIn(res, [textwrap.dedent(i).strip() for i in invariants])
686+
687+
check(
688+
pprint.pformat(
689+
frozenset((
690+
frozenset((
691+
"xyz very-very long string",
692+
"qwerty is also absurdly long",
693+
)),
694+
frozenset((
695+
"abcd is even longer that before",
696+
"spam is not so long",
697+
)),
698+
)),
699+
),
700+
[
701+
"""
702+
frozenset({frozenset({'abcd is even longer that before',
703+
'spam is not so long'}),
704+
frozenset({'qwerty is also absurdly long',
705+
'xyz very-very long string'})})
706+
""",
707+
708+
"""
709+
frozenset({frozenset({'abcd is even longer that before',
710+
'spam is not so long'}),
711+
frozenset({'xyz very-very long string',
712+
'qwerty is also absurdly long'})})
713+
""",
714+
715+
"""
716+
frozenset({frozenset({'spam is not so long',
717+
'abcd is even longer that before'}),
718+
frozenset({'qwerty is also absurdly long',
719+
'xyz very-very long string'})})
720+
""",
721+
722+
"""
723+
frozenset({frozenset({'spam is not so long',
724+
'abcd is even longer that before'}),
725+
frozenset({'xyz very-very long string',
726+
'qwerty is also absurdly long'})})
727+
""",
728+
729+
# -
730+
731+
"""
732+
frozenset({frozenset({'qwerty is also absurdly long',
733+
'xyz very-very long string'}),
734+
frozenset({'abcd is even longer that before',
735+
'spam is not so long'})})
736+
""",
737+
738+
"""
739+
frozenset({frozenset({'qwerty is also absurdly long',
740+
'xyz very-very long string'}),
741+
frozenset({'spam is not so long',
742+
'abcd is even longer that before'})})
743+
""",
744+
745+
"""
746+
frozenset({frozenset({'xyz very-very long string',
747+
'qwerty is also absurdly long'}),
748+
frozenset({'abcd is even longer that before',
749+
'spam is not so long'})})
750+
""",
751+
752+
"""
753+
frozenset({frozenset({'xyz very-very long string',
754+
'qwerty is also absurdly long'}),
755+
frozenset({'spam is not so long',
756+
'abcd is even longer that before'})})
757+
""",
758+
],
708759
)
709760

710761
def test_depth(self):

0 commit comments

Comments
 (0)