• Feature proposal: unittest.mock.NAN

    From Kerrick Staley@3:633/280.2 to All on Thu Jan 25 04:11:25 2024
    I think we should define a unittest.mock.NAN constant that can be used with Mock.assert_called_with() to assert that an argument passed to a Mock was
    NaN. NaNs are special in that math.nan != math.nan, so you can't just do assert_called_with(math.nan). The naming is meant to parallel unittest.mock.ANY.

    Here is a reference implementation:

    class _EqNaN:
    def __eq__(self, other):
    return math.isnan(other)

    NAN = _EqNaN()

    The alternative is that users can just define this EqNaN class themselves
    as needed in test code. I encountered the need to test for a NaN argument
    today and was surprised to find that (as far as I can tell) there is no pre-built solution to this in unittest or pytest. It feels like it should
    be included in some standard library.

    - Kerrick

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)
  • From Stefan Ram@3:633/280.2 to All on Thu Jan 25 07:34:01 2024
    Kerrick Staley <k@kerrickstaley.com> writes:
    NaNs are special in that math.nan != math.nan, so you can't just do
    assert_called_with(math.nan).

    class ProductionClass:
    .... def method(self):
    .... self.something( math.nan )
    .... def something( self, x ):
    .... pass
    ....
    real = ProductionClass()
    real.something = MagicMock()
    real.method()
    real.something.assert_called_once_with( math.nan )



    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: Stefan Ram (3:633/280.2@fidonet)
  • From Barry Scott@3:633/280.2 to All on Thu Jan 25 08:08:12 2024
    Python ideas are discussed here these days: https://discuss.python.org/
    Suggest you raise this there in the Ideas category.

    Barry


    On 24 Jan 2024, at 17:11, Kerrick Staley via Python-list =
    <python-list@python.org> wrote:
    =20
    I think we should define a unittest.mock.NAN constant that can be used =
    with
    Mock.assert_called_with() to assert that an argument passed to a Mock =
    was
    NaN. NaNs are special in that math.nan !=3D math.nan, so you can't =
    just do
    assert_called_with(math.nan). The naming is meant to parallel unittest.mock.ANY.
    =20
    Here is a reference implementation:
    =20
    class _EqNaN:
    def __eq__(self, other):
    return math.isnan(other)
    =20
    NAN =3D _EqNaN()
    =20
    The alternative is that users can just define this EqNaN class =
    themselves
    as needed in test code. I encountered the need to test for a NaN =
    argument
    today and was surprised to find that (as far as I can tell) there is =
    no
    pre-built solution to this in unittest or pytest. It feels like it =
    should
    be included in some standard library.
    =20
    - Kerrick
    --=20
    https://mail.python.org/mailman/listinfo/python-list
    =20


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: ---:- FTN<->UseNet Gate -:--- (3:633/280.2@fidonet)