Better reporting for "dict subset" comparison #12860
Labels
topic: rewrite
related to the assertion rewrite mechanism
type: enhancement
new feature or API change, should be merged into features branch
Preface
Python has a way of checking that one dict is a subset of another one, as highlighed in #2376 (comment). This is quite a nifty feature that allows for partial checks when you don't want to check all the fields e.g., some of them are not stable, so you want to split the checks:
or the
dict
has too many irrelevant items and you don't want to list them all just to check a couple of interesting ones:What's the problem this feature will solve?
Described approach works perfectly fine from the functional standpoint however since there is no dedicated handling of this case, reporting fallbacks to
repr
of theItemsView
:It would be nice to have a dedicated comparison for this that shows only the difference, similar to
dict
comparison:Describe the solution you'd like
Currently, I have a naive implementation for such case that utilizes
pytest_assertrepr_compare
hook:With this hook in place, the output looks like this:
and for a mismatch:
Alternative Solutions
I think it's possible to introduce rich comparison with third-party plugins that implement the hook similar to aforementioned or we can introduce some unittest-style
assertDictIsSubset
that handles reporting, but to be honest I'm leaning towards thinking that first-party support viaassertrepr
for this would be best.Additional context
—
The text was updated successfully, but these errors were encountered: