Skip to content

test_set_of_sets_reprs of test_pprint is silently ignored #111147

Closed

Description

@sobolevn

Bug report

Right now this test is marked as expectedFailure with a big comment about why:

@unittest.expectedFailure
#See http://bugs.python.org/issue13907
@test.support.cpython_only
def test_set_of_sets_reprs(self):
# This test creates a complex arrangement of frozensets and
# compares the pretty-printed repr against a string hard-coded in
# the test. The hard-coded repr depends on the sort order of
# frozensets.
#
# However, as the docs point out: "Since sets only define
# partial ordering (subset relationships), the output of the
# list.sort() method is undefined for lists of sets."
#
# In a nutshell, the test assumes frozenset({0}) will always
# sort before frozenset({1}), but:
#
# >>> frozenset({0}) < frozenset({1})
# False
# >>> frozenset({1}) < frozenset({0})
# False
#
# Consequently, this test is fragile and
# implementation-dependent. Small changes to Python's sort
# algorithm cause the test to fail when it should pass.
# XXX Or changes to the dictionary implementation...

With self.maxDiff = None:

» ./python.exe -m test test_pprint -m test_set_of_sets_reprs
Using random seed 3151060862
0:00:00 load avg: 1.34 Run 1 test sequentially
0:00:00 load avg: 1.34 [1/1] test_pprint
test test_pprint failed -- Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython/Lib/test/test_pprint.py", line 672, in test_set_of_sets_reprs
    self.assertEqual(pprint.pformat(cube), cube_repr_tgt)
AssertionError: '{fro[43 chars]set({1}), frozenset({0})}),\n frozenset({0}): [843 chars])})}' != '{fro[43 chars]set({0}), frozenset({1})}),\n frozenset({0}): [1017 chars])})}'
- {frozenset(): frozenset({frozenset({2}), frozenset({1}), frozenset({0})}),
-  frozenset({0}): frozenset({frozenset(), frozenset({0, 1}), frozenset({0, 2})}),
-  frozenset({1}): frozenset({frozenset(), frozenset({0, 1}), frozenset({1, 2})}),
? ^          ---                                       ---                ---
+ {frozenset(): frozenset({frozenset({2}), frozenset({0}), frozenset({1})}),
? ^                                  +++
+  frozenset({0}): frozenset({frozenset(),
+                             frozenset({0, 2}),
+                             frozenset({0, 1})}),
+  frozenset({1}): frozenset({frozenset(),
+                             frozenset({1, 2}),
+                             frozenset({0, 1})}),
+  frozenset({2}): frozenset({frozenset(),
+                             frozenset({1, 2}),
+                             frozenset({0, 2})}),
-  frozenset({0, 1}): frozenset({frozenset({1}),
?             ^  ^                          ^
+  frozenset({1, 2}): frozenset({frozenset({2}),
?             ^  ^                          ^
-                                frozenset({0}),
?                                           ^
+                                frozenset({1}),
?                                           ^
                                 frozenset({0, 1, 2})}),
-  frozenset({2}): frozenset({frozenset(), frozenset({0, 2}), frozenset({1, 2})}),
   frozenset({0, 2}): frozenset({frozenset({2}),
                                 frozenset({0}),
                                 frozenset({0, 1, 2})}),
-  frozenset({1, 2}): frozenset({frozenset({2}),
?              ---                          ^
+  frozenset({0, 1}): frozenset({frozenset({0}),
?             +++                           ^
                                 frozenset({1}),
                                 frozenset({0, 1, 2})}),
-  frozenset({0, 1, 2}): frozenset({frozenset({0, 1}),
?                                              ^  ^
+  frozenset({0, 1, 2}): frozenset({frozenset({1, 2}),
?                                              ^  ^
                                    frozenset({0, 2}),
-                                   frozenset({1, 2})})}
?                                               ---
+                                   frozenset({0, 1})})}
?                                              +++


test_pprint failed (1 failure)

== Tests result: FAILURE ==

1 test failed:
    test_pprint

Total duration: 38 ms
Total tests: run=1 (filtered) failures=1
Total test files: run=1/1 (filtered) failed=1
Result: FAILURE

There are several problems with this test:

  1. It creates an explicit relation with test_set because of an import:
    import test.test_set
    We try to revome such dependencies
  2. It does not work :)
  3. It is not very readable:
Снимок экрана 2023-10-21 в 08 49 09

Why is this test unstable? Because frozenset({0}) < frozenset({1}) is False and frozenset({1}) < frozenset({0}) is False. However, we can refator this test to use frozenset(), it is always ordered: frozenset() < frozenset({1, 2}) is True, frozenset() > frozenset({1, 2}) is False

So, my idea is to:

  • Refactor this test to still do pretty much the same
  • But reliably
  • With more readability
  • Without test_set dependency

Refs:

Linked PRs

Activity

  1. added
    type-bug
    tests
    stdlib
    on Oct 21, 2023
  2. self-assigned this
    on Oct 21, 2023
  3. added a commit that references this issue on Oct 21, 2023
  4. serhiy-storchaka commented on Oct 21, 2023

    @serhiy-storchaka
    Member

    The test was originally added in Python 2.6 in 5310b69 (logs for main do not show this). Then marked as CPython-only with adding a large comment in bpo-9131 (#53377). Then marked as expected failure in bpo-13903 (#58111). Stable part of the test was moved to other test method.

    See also related issue bpo-27495 (#71682).

    I think that @rhettinger as the original author of tests and the maintainer of these classes should decide what to do with this test. Should it be rewritten for non-deterministic order or removed?

  5. removed their assignment
    on Oct 27, 2023
  6. added a commit that references this issue on Nov 27, 2023
  7. added a commit that references this issue on Feb 11, 2024
  8. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stdlibteststype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions