Skip to content
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

copying falsifying example into @example() fails to reproduce test failure due to pprinter sorting dict keys #3370

Closed
andreareina opened this issue Jun 9, 2022 · 2 comments · Fixed by #3371
Labels
bug something is clearly wrong here

Comments

@andreareina
Copy link

Some code depends on the order of dict iteration so sorting the keys before printing makes reproducing a failing test more difficult.

Related to #2913 ?

Sample code:

import hypothesis as h
import hypothesis.strategies as st

@h.given(st.dictionaries(st.integers(), st.integers()))
def test(d):
    h.note(f"{d=}")
    assert sorted(d) == list(d)

Output: falsifying example report gives an input that if copied verbatim, would actually pass the test

=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.11, pytest-5.4.3, py-1.11.0, pluggy-0.13.1
rootdir: [snip]/hypothesis-dictionary-order
plugins: hypothesis-6.47.0
collected 1 item                                                                                                                                                                                          

test.py F                                                                                                                                                                                           [100%]

================================================================================================ FAILURES =================================================================================================
__________________________________________________________________________________________________ test ___________________________________________________________________________________________________

    @h.given(st.dictionaries(st.integers(), st.integers()))
>   def test(d):

test.py:5: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

d = {0: 0, 1: 0}

    @h.given(st.dictionaries(st.integers(), st.integers()))
    def test(d):
        h.note(f"{d=}")
>       assert sorted(d) == list(d)
E       assert [0, 1] == [1, 0]
E         At index 0 diff: 0 != 1
E         Use -v to get the full diff

test.py:7: AssertionError
----------------------------------------------------------------------------------------------- Hypothesis ------------------------------------------------------------------------------------------------
Falsifying example: test(
    d={0: 0, 1: 0},
)
d={1: 0, 0: 0}
========================================================================================= short test summary info =========================================================================================
FAILED test.py::test - assert [0, 1] == [1, 0]

Expected output: falsifying example should reflect the dict key order as actually passed into the test function

@Zac-HD Zac-HD added the bug something is clearly wrong here label Jun 9, 2022
@Zac-HD
Copy link
Member

Zac-HD commented Jun 9, 2022

Ooh, thanks for the report - this was OK to do on old versions of Python, but not now that dict iteration order is stable.

richardscholtens added a commit to richardscholtens/hypothesis that referenced this issue Jun 10, 2022
Order of dictionaries is now enforced in Python 3.7 and higher. This means that the original order of dictionaries matters for falsifying examples.
@richardscholtens
Copy link
Contributor

This PR was kindly supported by Ordina Pythoneers.

richardscholtens added a commit to richardscholtens/hypothesis that referenced this issue Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants