diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index 3177caea..a15e1cb4 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -217,13 +217,14 @@ def uniq(container): brute force when necessary. """ seen_keys = set() + supported = [] unsupported = [] for element in container: key = _uniq_key(element) if key is _UNSUPPORTED: - for previous in unsupported: + for previous in [*supported, *unsupported]: if equal(previous, element): return False unsupported.append(element) @@ -231,8 +232,11 @@ def uniq(container): if key in seen_keys: return False + if any(equal(previous, element) for previous in unsupported): + return False seen_keys.add(key) + supported.append(element) return True diff --git a/jsonschema/tests/test_utils.py b/jsonschema/tests/test_utils.py index f716a36a..ac6c2974 100644 --- a/jsonschema/tests/test_utils.py +++ b/jsonschema/tests/test_utils.py @@ -223,3 +223,7 @@ def test_unhashable_inside_mapping_falls_back(self): self.assertTrue( uniq([{"k": Unhashable(1)}, {"k": Unhashable(2)}]), ) + + def test_equal_hashable_and_unhashable_values_are_not_unique(self): + self.assertFalse(uniq([{1}, frozenset({1})])) + self.assertFalse(uniq([frozenset({1}), {1}]))