Skip to content

Python sets inside dataclasses are not matched accordingly #1048

@Genfood

Description

@Genfood

Describe the bug

When asserting a set of strings nested inside a dataclass against the snapshot, the test may fail, due to the unsorted nature of sets. This is not the case for integer set or sets that are not nested inside a dataclass.

To reproduce

@dataclass
class Test:
    my_str_set: set[str]

    def __eq__(self, value: object) -> bool:
        self.my_str_set == value.my_str_set

    # my_int_set: set[int]


def test_set(snapshot: SnapshotAssertion) -> None:
    assert snapshot == Test({"1", " 2", "3", "4", "5", "6", "7"})
    # assert snapshot == Test({1, 2, 3, 4, 5, 6, 7})
    # assert snapshot == {"1", " 2", "3", "4", "5", "6", "7"}

Expected behavior

The snapshot test should work out of the box, with nested sets.

Environment (please complete the following information):

  • OS: Windows 11
  • Syrupy Version: 4.9.1
  • Python Version: 3.10.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions