Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(required-match): make fails_required_match private
- it is only called by the Hungarian helper, so it should not ship as a
  public method on every `StructuredModel` in 1.1.0
- renaming it before the release costs nothing; after it, it would be a
  breaking change
  • Loading branch information
digalaviz committed Nov 30, 2026
commit 8e877b21a7522bbe2ed11f8eed22ec18b070be0b
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def defer_pair_warnings() -> Iterator[None]:

def _fails_required_match(gt_item: Any, pred_item: Any) -> bool:
"""Whether a required field vetoes this pair; False for a non-model item."""
fails = getattr(gt_item, "fails_required_match", None)
fails = getattr(gt_item, "_fails_required_match", None)
return fails is not None and fails(pred_item)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ class is defined, so the nested refusal can only see it here. pydantic
cls._collect_required_match_fields()
return rebuilt

def fails_required_match(self, other: Any) -> bool:
def _fails_required_match(self, other: Any) -> bool:
"""Whether a required field of either object is not matched by the other.

``self`` is the ground truth. Both classes' required fields are read, so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ class Row(StructuredModel):
gt = Row(label=gt_label, note="n")
pred = Row(label=pred_label, note="n")

assert gt.fails_required_match(pred) is vetoed
assert gt._fails_required_match(pred) is vetoed


def test_nan_on_one_side_vetoes():
class Row(StructuredModel):
value: Optional[float] = exact(required_match=True)

assert Row(value=float("nan")).fails_required_match(Row(value=1.0)) is True
assert Row(value=float("nan"))._fails_required_match(Row(value=1.0)) is True


# two classes in one list
Expand All @@ -167,21 +167,21 @@ def test_a_veto_holds_whichever_class_is_ground_truth(other_cls):
required = Detection(**detection(label="person"))
loose = other_cls(**detection(label="car"))

assert required.fails_required_match(loose) is True
assert loose.fails_required_match(required) is True
assert required._fails_required_match(loose) is True
assert loose._fails_required_match(required) is True


def test_a_field_required_on_one_class_and_absent_on_the_other_vetoes_both_ways():
required = Detection(**detection())
absent = NoLabel(x=1.0, y=1.0, note="n")

assert required.fails_required_match(absent) is True
assert absent.fails_required_match(required) is True
assert required._fails_required_match(absent) is True
assert absent._fails_required_match(required) is True


@pytest.mark.parametrize("other", [None, {}, "text", 3, []])
def test_a_non_model_partner_is_a_veto_and_does_not_raise(other):
assert Detection(**detection()).fails_required_match(other) is True
assert Detection(**detection())._fails_required_match(other) is True


# the direction a required field is scored in
Expand Down Expand Up @@ -212,7 +212,7 @@ def test_a_required_field_is_scored_ground_truth_first_as_the_field_is():
)["confusion_matrix"]["fields"]["items"]["overall"]

assert field["fields"]["x"]["overall"]["tp"] == 1
assert gt.fails_required_match(pred) is False
assert gt._fails_required_match(pred) is False
assert (items["tp"], items["fn"], items["fa"]) == (1, 0, 0)


Expand Down Expand Up @@ -285,7 +285,7 @@ def test_a_threshold_of_zero_never_vetoes_two_present_values():
class Row(StructuredModel):
label: str = ComparableField(threshold=0.0, required_match=True)

assert Row(label="a").fails_required_match(Row(label="zzzzzz")) is False
assert Row(label="a")._fails_required_match(Row(label="zzzzzz")) is False


# the matcher on its own
Expand Down