Skip to content

Tests for operators #27

Closed
Closed

Description

@asmeurer

Every __operator__ method has a corresponding function which should have the same behavior, meaning the tests for those operators should be able to just reuse the test for the function. We should figure out a clean way to do this. Maybe it's just a question of parameterizing over the function so that something like test_add will parameterize over add(), x1.__add__ and x1.__radd__. There's also the complication of the in-place operators (x1.__iadd__), which work just like the operators except the input shape and dtypes are more restricted (the result shape and dtype must be the same as the lhs). This might just be a case of only testing op= when the inputs would be legal.

These are already split out in the type promotion tests (which are currently the only tests that test the operator methods). I don't know if those tests also need to be merged. If they can be merged simply they can be, but otherwise it is fine to keep them separate as they are now.

Activity

  1. honno commented on Oct 13, 2021

    @honno
  2. asmeurer commented on Oct 13, 2021

    @asmeurer
    MemberAuthor

    It's not necessary to test both the dunder method and the operator. Python will call the dunder method when the operator is used. The only slight hitch to that is the __r*__ methods are only called when the left operand returns NotImplemented, so we generally have to test them directly. As far as the actual pytest parameter ids, I think either way looks fine.

  3. honno commented on Oct 14, 2021

    @honno
    Member

    Ah I misunderstood this issue, I see now.

  4. honno commented on Oct 28, 2021

    @honno
    Member

    Tackling this next. My first idea is to eval expression parameters e.g.

    @pytest.mark.parametrize("expr", ["xp.add(x1, x2)", "x1 + x2"])
    @given(*hh.two_mutual_arrays(dh.numeric_dtypes))
    def test_add(expr, x1, x2):
        out = eval(expr)
        ...

    As-is this doesn't work out for inplace operators nicely. I'll experiment more and hopefully come up with something better. Maybe a custom pytest fixture or decorator which passes an out parameter (that could be x1 in inplace operations), so the test method body doesn't need to worry how it was evaluated. Inplace operations with scalars are something I'd also leave last heh.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions