Skip to content

Add equal_to_approx matcher for approximate numeric assertions#39443

Open
SreeramaYeshwanthGowd wants to merge 1 commit into
apache:masterfrom
SreeramaYeshwanthGowd:python-equal-to-approx-18028
Open

Add equal_to_approx matcher for approximate numeric assertions#39443
SreeramaYeshwanthGowd wants to merge 1 commit into
apache:masterfrom
SreeramaYeshwanthGowd:python-equal-to-approx-18028

Conversation

@SreeramaYeshwanthGowd

Copy link
Copy Markdown

Problem

The assert_that matchers only offer exact equality through equal_to.
Pipelines that produce floating point results are awkward to assert, because
callers have to hand write an equals_fn in every test. Issue #18028 asks to
expand the set of matchers.

Solution

Add equal_to_approx, an assert_that matcher that behaves like equal_to for
ordering and membership but compares real number elements, including numbers
nested in tuples or lists, with math.isclose using configurable rel_tol and
abs_tol. All other elements are compared with ==. It is a thin wrapper over
the existing equal_to(equals_fn=...) mechanism and adds no new dependency.

Testing

Added unit tests in util_test.py covering values within and outside tolerance
(including a nested value outside tolerance), nested tuples and lists, rel_tol
and abs_tol, non numeric passthrough, and an empty PCollection.

Fixes #18028


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

equal_to only supports exact equality, so tests over floating point pipeline
output had to supply a custom equals_fn. Add equal_to_approx, which compares
real number elements (including numbers nested in tuples or lists) with
math.isclose using configurable rel_tol and abs_tol, reusing the existing
equal_to matching. Includes unit tests and a CHANGES.md entry.

Fixes apache#18028
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More sophisticated assert matchers

1 participant