Whole file
hamcrest/PyHamcrest
The author described this change as “Fix for https://github.com/hamcrest/PyHamcrest/issues/156 - if has_entry() has only a single matching key, report the mismatching value.”. It counts as a record because the check below fails on the code as it stood at 02cf6ac09 and passes on bbd147d2d, with nothing else changed between the two runs.
Projecthamcrest/PyHamcrest
Fix saved2021-03-06
Sharing licenceBSD-3-Clause · LICENSE.txt
Change size+19 −1
What the code was meant to do, written into the code itself as a save note
Fix for https://github.com/hamcrest/PyHamcrest/issues/156 - if has_entry() has only a single matching key, report the mismatching value.
The change
| 1 | - | from typing import Hashable, Mapping, TypeVar, Union | |
| 1 | + | from typing import Hashable, Mapping, MutableMapping, TypeVar, Union | |
| 2 | 2 | ||
| 3 | 3 | from hamcrest.core.base_matcher import BaseMatcher | |
| 4 | 4 | from hamcrest.core.description import Description | |
| ⋯ | |||
| 31 | 31 | description.append_text("a dictionary containing [").append_description_of( | |
| 32 | 32 | self.key_matcher | |
| 33 | 33 | ).append_text(": ").append_description_of(self.value_matcher).append_text("]") | |
| 34 | + | ||
| 35 | + | def describe_mismatch(self, item: Mapping[K, V], mismatch_description: Description) -> None: | |
| 36 | + | key_matches: MutableMapping[K, V] = {} | |
| 37 | + | if hasmethod(item, "items"): | |
| 38 | + | for key, value in item.items(): | |
| 39 | + | if self.key_matcher.matches(key): | |
| 40 | + | key_matches[key] = value | |
| 41 | + | if len(key_matches) == 1: | |
| 42 | + | key, value = key_matches.popitem() | |
| 43 | + | mismatch_description.append_text("value for ").append_description_of(key).append_text( | |
| 44 | + | " " | |
| 45 | + | ) | |
| 46 | + | self.value_matcher.describe_mismatch(value, mismatch_description) | |
| 47 | + | else: | |
| 48 | + | super().describe_mismatch(item, mismatch_description) | |
| 49 | + | ||
| 50 | + | def describe_match(self, item: Mapping[K, V], match_description: Description) -> None: | |
| 51 | + | super().describe_match(item, match_description) | |
| 34 | 52 | ||
| 35 | 53 | ||
| 36 | 54 | def has_entry( | |
The check that tells the two apart
fail→pass·tests/hamcrest_unit_test/collection/isdict_containing_test.py::IsDictContainingTest::test_describe_single_matching_key_mismatching_value
Check file tests/hamcrest_unit_test/collection/isdict_containing_test.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it02cf6ac09a5b75a4b2ae8d51fb1146e2fe35bdd1
Broken version dated2021-03-06
Modulehamcrest.library.collection.isdict_containing
Units changedIsDictContaining
Fingerprint5284e11af00b440a
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.