Skip to content
Discussion options

You must be logged in to vote

I wrote a custom matcher to sort the sets before parsing them. Here is a simple example:

@dataclass
class TestA:
    name: str
    b: TestB

@dataclass
class TestB:
    test_set: set[str]

def sort_sets_matcher(data: Any, path: tuple[Any, ...]) -> Any:
    if isinstance(data, set):
        return sorted(list(data))

    if dataclasses.is_dataclass(data) and not isinstance(data, type):
        # The serializer will walk this dict
        return dataclasses.asdict(data)

    return data

def test_my_dataclass(snapshot: SnapshotAssertion):
    my_object = TestA(
        name="Test Name",
        b=TestB(
            test_set={"c", "a", "z", "b"} 
        )
    )

    assert my_object == snap…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Genfood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant