From 01557900b2d41046c360c80e68cb8469d9598f94 Mon Sep 17 00:00:00 2001 From: sorata-kanda Date: Mon, 22 Dec 2025 21:37:11 +0530 Subject: [PATCH] Add assert_not_both_not_none (fixes #393) --- chex/_src/asserts.py | 14 ++++++++++++++ chex/_src/asserts_test.py | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/chex/_src/asserts.py b/chex/_src/asserts.py index 6c08c21..4c0696d 100644 --- a/chex/_src/asserts.py +++ b/chex/_src/asserts.py @@ -248,6 +248,20 @@ def assert_not_both_none(first: Any, second: Any) -> None: raise AssertionError( "At least one of the arguments must be different from `None`.") +@_static_assertion +def assert_not_both_not_none(first: Any, second: Any) -> None: + """Checks that not both arguments are non-None. + + Args: + first: A first object. + second: A second object. + + Raises: + AssertionError: If both ``first`` and ``second`` are not None. + """ + if first is not None and second is not None: + raise AssertionError( + "At most one of the arguments may be different from `None`.") @_static_assertion def assert_exactly_one_is_none(first: Any, second: Any) -> None: diff --git a/chex/_src/asserts_test.py b/chex/_src/asserts_test.py index 6d5e9da..622dcc3 100644 --- a/chex/_src/asserts_test.py +++ b/chex/_src/asserts_test.py @@ -1860,7 +1860,15 @@ def test_assert_equal_fail(self, first, second): with self.assertRaises(AssertionError): asserts.assert_equal(first, second) +class NotNoneAssertionsTest(parameterized.TestCase): + def test_assert_not_both_not_none(self): + asserts.assert_not_both_not_none(None, None) + asserts.assert_not_both_not_none(1, None) + asserts.assert_not_both_not_none(None, 1) + + with self.assertRaises(AssertionError): + asserts.assert_not_both_not_none(1, 2) class IsDivisibleTest(parameterized.TestCase): def test_assert_is_divisible(self):